Created
April 26, 2021 06:10
-
-
Save suadanwar/91e42d37f97f311289622980d299158c to your computer and use it in GitHub Desktop.
This sample code is for pelita 3d printing project using arduino.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const int LED = 10; | |
long random_led; | |
long random_time; | |
int difer_time(); | |
void setup() { | |
pinMode(LED, OUTPUT); | |
} | |
int min_led = 200; | |
int max_led = 1024; | |
void loop() { | |
random_led = random(min_led, max_led); // random number to shine led | |
random_time = difer_time(random_led); // convert strength to time | |
analogWrite(LED, random_led); // set LED to value PWM | |
delay(random_time); // set delay depending of the strength | |
} | |
int difer_time(int s){ | |
int a, b, wait; | |
b = map(s, min_led, max_led, 1, 100); // remap strength to 100% | |
// 3 stages of random speed. If more light then longer delay | |
if (b > 80){ | |
wait = random(80, 100); // max strength | |
} | |
else if (b > 20 && b < 60){ | |
wait = random(20, 80); // midle strength. | |
} | |
else{ | |
wait = random(1, 20); // led almost turned of, time very short. | |
} | |
return wait; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment