1
- const int shamash = 2 ; // output pin
2
- const int candles[] = {3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 };
1
+ const int candles[] = {2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 };
3
2
const int button = 11 ; // input pin
4
3
5
4
int night = 0 ;
6
5
7
6
void setup () {
7
+ Serial.begin (9600 );
8
+
9
+ Serial.println (" Hello hanukiah" );
10
+
8
11
// set up all outputs
9
- pinMode (shamash, OUTPUT);
10
- for (int i = 0 ; i < 8 ; ++i) {
12
+ for (int i = 0 ; i < 9 ; ++i) {
11
13
pinMode (candles[i], OUTPUT);
12
14
}
13
15
// set up input
14
16
pinMode (button, INPUT);
17
+ randomSeed (analogRead (12 ));
15
18
}
16
19
17
20
unsigned long started = 0L ;
18
21
#define ONE_HOUR_MILLIS 3600000L
19
22
20
23
void loop () {
21
24
// if switch down (with debounce)
22
- if (digitalRead (button) == HIGH) {
23
- night = (night + 1 ) % 9 ; // yes 9, so that it maxes at 8
24
-
25
- if (night == 0 ) {
26
- digitalWrite (shamash, LOW);
27
- } else {
28
- digitalWrite (shamash, HIGH);
25
+ if (digitalRead (button) == LOW) {
26
+ Serial.println (" button is pushed" );
27
+ night = (night + 1 ) % 9 ;
28
+ if (night == 1 ) {
29
+ night = 2 ;
29
30
}
31
+ Serial.print (" night is now " ); Serial.println (night);
30
32
31
33
// Are there better ways to do this? Yes. Do I care? No.
32
34
// Turn on the the appropriate # of candles.
@@ -46,19 +48,22 @@ void loop() {
46
48
} else if (millis () - started > ONE_HOUR_MILLIS) {
47
49
// Candles go out after an hour
48
50
night = 0 ;
49
- digitalWrite (shamash, LOW);
50
- for (int i = 0 ; i < 8 ; ++i) {
51
+ for (int i = 0 ; i < 9 ; ++i) {
51
52
digitalWrite (candles[i], LOW);
52
53
}
53
54
} else if (night > 0 ) {
54
55
// Fake-flicker a candle
55
- int candle = random (night);
56
- int iters = random (5 );
56
+ int iters = random (4 );
57
57
for (int i = 0 ; i < iters; ++i) {
58
- digitalWrite (candles[candle], LOW);
59
- int offness = random (100 );
60
- delay (offness);
61
- digitalWrite (candles[candle], HIGH);
58
+ long candle = random (4000 );
59
+ if (candle < night) {
60
+ digitalWrite (candles[candle], LOW);
61
+ int offness = random (50 );
62
+ delay (offness);
63
+ digitalWrite (candles[candle], HIGH);
64
+ int onness = random (300 );
65
+ delay (onness);
66
+ }
62
67
}
63
68
}
64
69
}
0 commit comments