-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathled_flasher.cpp
More file actions
43 lines (31 loc) · 866 Bytes
/
led_flasher.cpp
File metadata and controls
43 lines (31 loc) · 866 Bytes
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
#include <Arduino.h>
#include "led_flasher.h"
LEDFlasher::LEDFlasher(
uint8_t ledPin, unsigned long onDuration, unsigned long offDuration ) :
Runnable(), isOn(false), pin(ledPin), prevTimeMs(0),
onIntervalMs(onDuration), offIntervalMs(offDuration) { }
void LEDFlasher::run() {
unsigned long currentTime = millis();
unsigned long interval = currentTime - prevTimeMs;
if( isOn && interval >= onIntervalMs ) {
turnOff();
prevTimeMs = currentTime;
} else if( ! isOn && interval >= offIntervalMs ) {
turnOn();
prevTimeMs = currentTime;
}
}
void LEDFlasher::setup() {
Serial.println("LEDFlasher setup");
pinMode( pin, OUTPUT );
}
void LEDFlasher::toggle(bool state) {
digitalWrite( pin, state ? HIGH : LOW );
isOn = state;
}
void LEDFlasher::turnOff() {
toggle(false);
}
void LEDFlasher::turnOn() {
toggle(true);
}