Last active
October 1, 2015 16:06
-
-
Save shuheilocale/5c419bb0a6dce533bde8 to your computer and use it in GitHub Desktop.
Arduino random blink
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_PIN= 3; | |
int cur_millis, wait_millis, brightness; | |
void setup() { | |
Serial.begin(9600); | |
cur_millis = millis(); | |
wait_millis = random(1000); | |
brightness = random(255); | |
} | |
void loop() { | |
int ellapse_millis = millis() - cur_millis; | |
if(ellapse_millis > wait_millis ){ | |
wait_millis = random(1000); | |
brightness = random(255); | |
cur_millis = millis(); | |
} | |
unsigned long coefficient = millis() % 2000; | |
coefficient = ( coefficient <= 1000 ? coefficient : 2000 - coefficient ); | |
int out_brightness = (int)( coefficient / 1000.0 * brightness ); | |
Serial.println(out_brightness); | |
analogWrite( LED_PIN, out_brightness ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment