// 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()); }