Skip to content

Instantly share code, notes, and snippets.

@zamfi
Created November 28, 2018 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zamfi/96bf4bd879b705241dd9be038573488f to your computer and use it in GitHub Desktop.
Save zamfi/96bf4bd879b705241dd9be038573488f to your computer and use it in GitHub Desktop.
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