aboutsummaryrefslogtreecommitdiffstats
path: root/lighting/strobe.ino
blob: eccc1c45b6c1486573a1b4f3d2b6611dda141024 (plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// PWM: 3, 5, 6, 9, 10, and 11. Provide 8-bit PWM output with the analogWrite() function.
byte LED_A = 3;
byte LED_B = 5;

#define STROBE_STATE_SLOW 1
#define STROBE_STATE_FAST 2
#define STROBE_STATE_NONE 3

class Strobe {
    byte slow, fast;
    byte state;
    byte led;
    boolean on;
    unsigned long next = 0;

public:
    boolean stopped = false;

    Strobe(byte l, byte s, byte f) {
        state = STROBE_STATE_SLOW;
        led = l;
        slow = s;
        fast = f;
        on = false;
    }

    boolean run() {
        if (next > 0 && (millis() < next)) {
            return false;
        }

        switch(state) {
        case STROBE_STATE_SLOW:
            on = !on;
            analogWrite(led, on ? 255 : 0);
            if(on) {
                next = millis() + 10;
                return false;
            } else {
                next = millis() + 300 + random(0, 300);
            }
            if(--slow <= 0) {
                state = STROBE_STATE_FAST;
            }
            return false;
        case STROBE_STATE_FAST:
            on = !on;
            analogWrite(led, on ? 255 : 0);
            if(on) {
                next = millis() + 10;
                return false;
            } else {
                next = millis() + 75 + random(0, 75);
            }
            if(--fast <= 0) {
                state = STROBE_STATE_NONE;
                on = false;
            }
            return false;
        default:
            if(!on) {
                analogWrite(led, 255);
                on = true;
                stopped = true;
                return true;
            }
        }
    }
};

void setup() {
    pinMode(2, INPUT_PULLUP);
    Serial.begin(9600);
}

void flash(byte led, byte duration)
{
    analogWrite(led, 255);
    delay(duration);
    analogWrite(led, 0);
}

void fade(byte led, byte stepDuration, byte stepSize, boolean in) {
    if(in) {
        for (int fadeValue = 0 ; fadeValue <= 255; fadeValue+=stepSize) {
            analogWrite(led, fadeValue);
            delay(stepDuration);
        }
    } else {
        for (int fadeValue = 255 ; fadeValue >=0;  fadeValue-=stepSize) {
            analogWrite(led, fadeValue);
            delay(stepDuration);
        }
    }
}

void fade(byte led, byte stepDuration, boolean in)
{
    fade(led, stepDuration, 1, in);
}

unsigned long lastMillis;

void loop() {
    delay(1000);

    Strobe s1 = Strobe(3, random(1,4), random(2, 6));
    Strobe s2 = Strobe(5, random(1,4), random(2, 6));

    while(!(s1.stopped && s2.stopped)) {
        if(lastMillis > millis()) {
            lastMillis = millis();
            return;
        }
        s1.stopped ? : s1.run();
        s2.stopped ? : s2.run();
        lastMillis = millis();
    }


    delay(2500);

    analogWrite(LED_A, 0);
    analogWrite(LED_B, 0);

    Serial.println(lastMillis);
    Serial.println(millis());
}