Skip to content

Instantly share code, notes, and snippets.

@suadanwar

suadanwar/Pelita.ino

Created Apr 26, 2021
Embed
What would you like to do?
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