Skip to content

Instantly share code, notes, and snippets.

@suadanwar
Created April 26, 2021 06:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suadanwar/91e42d37f97f311289622980d299158c to your computer and use it in GitHub Desktop.
Save suadanwar/91e42d37f97f311289622980d299158c to your computer and use it in GitHub Desktop.
This sample code is for pelita 3d printing project using arduino.
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