Skip to content

Commit 33946b5

Browse files
committed
Turn on the shamash by itself. Renamed variables. Documentation!
1 parent a4b5b22 commit 33946b5

File tree

1 file changed

+34
-25
lines changed

1 file changed

+34
-25
lines changed

hanukiah/hanukiah.ino

+34-25
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
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};
210
const int button = 11; // input pin
311

4-
int night = 0;
12+
// Number of candles on
13+
int numOn = 0;
514

615
void setup() {
716
Serial.begin(9600);
@@ -10,7 +19,7 @@ void setup() {
1019

1120
// set up all outputs
1221
for (int i = 0; i < 9; ++i) {
13-
pinMode(candles[i], OUTPUT);
22+
pinMode(candlePins[i], OUTPUT);
1423
}
1524
// set up input
1625
pinMode(button, INPUT);
@@ -24,44 +33,44 @@ void loop() {
2433
// if switch down (with debounce)
2534
if (digitalRead(button) == LOW) {
2635
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);
3238

3339
// 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);
3743
}
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);
4147
}
4248

43-
// Started now.
49+
// Record last click time, to turn off after an hour.
4450
started = millis();
4551

46-
// for cheapo debouncing
52+
// For cheap "debouncing"
4753
delay(200);
4854
} else if (millis() - started > ONE_HOUR_MILLIS) {
4955
// Candles go out after an hour
50-
night = 0;
56+
numOn = 0;
5157
for (int i = 0; i < 9; ++i) {
52-
digitalWrite(candles[i], LOW);
58+
digitalWrite(candlePins[i], LOW);
5359
}
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);
5763
for (int i = 0; i < iters; ++i) {
5864
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);
6169
int offness = random(50);
6270
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);
6574
delay(onness);
6675
}
6776
}

0 commit comments

Comments
 (0)