Created
November 28, 2018 07:40
-
-
Save zamfi/96bf4bd879b705241dd9be038573488f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Blinker { | |
public: | |
Blinker(int pin, float periodSeconds, float onSeconds = 0.5, float delayUntilStart = 0.0) { | |
this->pin = pin; | |
period = periodSeconds * 1000; | |
onTime = onSeconds * 1000; | |
offset = delayUntilStart * 1000; | |
nextOn = millis() + offset; | |
pinMode(pin, OUTPUT); | |
} | |
void poll() { | |
if (millis() > nextOn) { | |
digitalWrite(pin, HIGH); | |
nextOn = nextOn + period; | |
} else if (millis() + period - onTime > nextOn) { | |
digitalWrite(pin, LOW); | |
} | |
} | |
private: | |
unsigned long period, onTime, offset; | |
unsigned long nextOn; | |
int pin; | |
}; | |
Blinker *lightOne; | |
Blinker *lightTwo; | |
void setup() { | |
lightOne = new Blinker(10, 15, 0.5); | |
lightTwo = new Blinker(11, 20, 0.5); | |
} | |
void loop() { | |
lightOne->poll(); | |
lightTwo->poll(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment