1
- const int candles[] = {2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 };
1
+ /* *
2
+ Hanukiah sketch. Press the button to turn on another light.
3
+
4
+ The lights flicker every once in a while.
5
+
6
+ Does not require PWM!
7
+ */
8
+
9
+ const int candlePins[] = {2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 };
2
10
const int button = 11 ; // input pin
3
11
4
- int night = 0 ;
12
+ // Number of candles on
13
+ int numOn = 0 ;
5
14
6
15
void setup () {
7
16
Serial.begin (9600 );
@@ -10,7 +19,7 @@ void setup() {
10
19
11
20
// set up all outputs
12
21
for (int i = 0 ; i < 9 ; ++i) {
13
- pinMode (candles [i], OUTPUT);
22
+ pinMode (candlePins [i], OUTPUT);
14
23
}
15
24
// set up input
16
25
pinMode (button, INPUT);
@@ -24,44 +33,44 @@ void loop() {
24
33
// if switch down (with debounce)
25
34
if (digitalRead (button) == LOW) {
26
35
Serial.println (" button is pushed" );
27
- night = (night + 1 ) % 9 ;
28
- if (night == 1 ) {
29
- night = 2 ;
30
- }
31
- Serial.print (" night is now " ); Serial.println (night);
36
+ numOn = (numOn + 1 ) % 10 ; // max is 9
37
+ Serial.print (" numOn is now " ); Serial.println (numOn);
32
38
33
39
// Are there better ways to do this? Yes. Do I care? No.
34
- // Turn on the the appropriate # of candles .
35
- for (int i = 0 ; i < night ; ++i) {
36
- digitalWrite (candles [i], HIGH );
40
+ // Turn off all .
41
+ for (int i = 0 ; i < 9 ; ++i) {
42
+ digitalWrite (candlePins [i], LOW );
37
43
}
38
- // Turn off the rest .
39
- for (int i = night ; i < 8 ; ++i) {
40
- digitalWrite (candles [i], LOW );
44
+ // Turn on the the appropriate # of candles .
45
+ for (int i = 0 ; i < numOn ; ++i) {
46
+ digitalWrite (candlePins [i], HIGH );
41
47
}
42
48
43
- // Started now .
49
+ // Record last click time, to turn off after an hour .
44
50
started = millis ();
45
51
46
- // for cheapo debouncing
52
+ // For cheap " debouncing"
47
53
delay (200 );
48
54
} else if (millis () - started > ONE_HOUR_MILLIS) {
49
55
// Candles go out after an hour
50
- night = 0 ;
56
+ numOn = 0 ;
51
57
for (int i = 0 ; i < 9 ; ++i) {
52
- digitalWrite (candles [i], LOW);
58
+ digitalWrite (candlePins [i], LOW);
53
59
}
54
- } else if (night > 0 ) {
55
- // Fake-flicker a candle
56
- int iters = random (4 );
60
+ } else if (numOn > 0 ) {
61
+ // Fake-flicker some candles
62
+ int iters = random (5 );
57
63
for (int i = 0 ; i < iters; ++i) {
58
64
long candle = random (4000 );
59
- if (candle < night) {
60
- digitalWrite (candles[candle], LOW);
65
+ // Slows down the flickering
66
+ if (candle < numOn) {
67
+ // Turn one off for up to 50 ms
68
+ digitalWrite (candlePins[candle], LOW);
61
69
int offness = random (50 );
62
70
delay (offness);
63
- digitalWrite (candles[candle], HIGH);
64
- int onness = random (300 );
71
+ // Turn it back on for up to 200 ms
72
+ digitalWrite (candlePins[candle], HIGH);
73
+ int onness = random (200 );
65
74
delay (onness);
66
75
}
67
76
}
0 commit comments