-
Notifications
You must be signed in to change notification settings - Fork 1
/
lamp_controller.cpp
51 lines (43 loc) · 1.23 KB
/
lamp_controller.cpp
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
#include <Arduino.h>
#include "lamp_controller.h"
LampController::LampController(int switch_in, int lamp_out) :
switch_in(switch_in), lamp_out(lamp_out) {}
void LampController::begin() {
pinMode(lamp_out, OUTPUT);
digitalWrite(lamp_out, HIGH);
lampState = HIGH;
pinMode(switch_in, INPUT_PULLUP);
switchLastState = digitalRead(switch_in);
}
void LampController::update() {
int newSwitchState = digitalRead(switch_in);
if (switchLastState != newSwitchState && lastSwitchChange + 500 < millis()) {
lastSwitchChange = millis();
toggleLamp();
switchLastState = newSwitchState;
}
if (lampState == HIGH && antiNoise && lastPulse + 4000 < millis()) {
digitalWrite(lamp_out, LOW);
delay(pulseLen);
digitalWrite(lamp_out, HIGH);
lastPulse = millis();
}
}
void LampController::setState(bool newState) {
if (newState) {
digitalWrite(lamp_out, HIGH); // relay OFF
lampState = HIGH;
} else {
digitalWrite(lamp_out, LOW); // relay ON
lampState = LOW;
}
}
void LampController::toggleLamp() {
setState(!getState());
}
void LampController::setAntiNoise(bool newState) {
antiNoise = newState;
}
void LampController::setAntiNoisePulseLen(int pulseLen) {
this->pulseLen = pulseLen;
}