|
| 1 | +const int shamash = 2; // output pin |
| 2 | +const int candles[] = {3, 4, 5, 6, 7, 8, 9, 10}; |
| 3 | +const int button = 11; // input pin |
| 4 | + |
| 5 | +int night = 0; |
| 6 | + |
| 7 | +void setup() { |
| 8 | + // set up all outputs |
| 9 | + pinMode(shamash, OUTPUT); |
| 10 | + for (int i = 0; i < 8; ++i) { |
| 11 | + pinMode(candles[i], OUTPUT); |
| 12 | + } |
| 13 | + // set up input |
| 14 | + pinMode(button, INPUT); |
| 15 | +} |
| 16 | + |
| 17 | +unsigned long started = 0L; |
| 18 | +#define ONE_HOUR_MILLIS 3600000L |
| 19 | + |
| 20 | +void loop() { |
| 21 | + // 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); |
| 29 | + } |
| 30 | + |
| 31 | + // Are there better ways to do this? Yes. Do I care? No. |
| 32 | + // Turn on the the appropriate # of candles. |
| 33 | + for (int i = 0; i < night; ++i) { |
| 34 | + digitalWrite(candles[i], HIGH); |
| 35 | + } |
| 36 | + // Turn off the rest. |
| 37 | + for (int i = night; i < 8; ++i) { |
| 38 | + digitalWrite(candles[i], LOW); |
| 39 | + } |
| 40 | + |
| 41 | + // Started now. |
| 42 | + started = millis(); |
| 43 | + |
| 44 | + // for cheapo debouncing |
| 45 | + delay(200); |
| 46 | + } else if (millis() - started > ONE_HOUR_MILLIS) { |
| 47 | + // Candles go out after an hour |
| 48 | + night = 0; |
| 49 | + digitalWrite(shamash, LOW); |
| 50 | + for (int i = 0; i < 8; ++i) { |
| 51 | + digitalWrite(candles[i], LOW); |
| 52 | + } |
| 53 | + } else if (night > 0) { |
| 54 | + // Fake-flicker a candle |
| 55 | + int candle = random(night); |
| 56 | + int iters = random(5); |
| 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); |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments