forked from tractionfan/ModelRailroadElectronics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCampfire_Flicker.ino
More file actions
39 lines (35 loc) · 1.47 KB
/
Campfire_Flicker.ino
File metadata and controls
39 lines (35 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/****************************************************************************
* Campfire Flicker - F.Miller 12/2018 (For ATTINY85)
* Random flickering of an LED to simulate a campfire
****************************************************************************/
const int firePin = 0; // (CHIP-5) the number of the LED pin
int ledState = LOW; // keep track of LED/Lightning state
unsigned long previousMillis = 0; // store last time LED was updated
unsigned long currentMillis; // capture current time
int randNumber; // develop a random number
void setup() {
pinMode(firePin, OUTPUT); // (0) goes to LED
}
void loop() {
// main running mode
if (ledState == LOW){
randNumber = random(1,20); // off range
} else {
randNumber = random(1,100); // On range
}
doit();
}
void doit() { // Function to flip ON/OFF after delay
delay(randNumber);
currentMillis = millis();
if(currentMillis - previousMillis >= randNumber) {
if (ledState == LOW){
ledState = HIGH;
digitalWrite(firePin, ledState); // Turn LED on
}else{
ledState = LOW;
digitalWrite(firePin, ledState); // Turn LED off
}
previousMillis = currentMillis; // reset for next time around
}
}