-
Notifications
You must be signed in to change notification settings - Fork 0
/
lux.ino
executable file
·78 lines (71 loc) · 1.62 KB
/
lux.ino
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Define pin variables.
int relay = D0;
// Add a physical button
int button = D2;
// Add a status light
int led = D7;
// This stores the current state of the light.
bool light = false;
void setup()
{
// Setup pin modes.
pinMode(relay, OUTPUT);
pinMode(button, INPUT_PULLUP);
pinMode(led, OUTPUT);
// Define a function to toggle the relay.
Particle.function("relay", relayToggle);
// Start with lights off.
digitalWrite(relay, HIGH);
digitalWrite(led, LOW);
// Add a cloud variable to get the current status.
Particle.variable("lightState", light);
}
// Define the loop function.
void loop() {
// Add the button logic.
int buttonState;
buttonState = digitalRead(button);
if(buttonState == LOW && light) {
lightsOff();
} else if (buttonState == LOW && !light) {
lightsOn();
}
delay(250);
}
// Cloud exposed function to operate the relay.
// A command variable can be passed.
// If no variable is given, the function will toggle.
// If the command is 'on' or 'off' it will turn on/off accordingly
int relayToggle(String command) {
if (command == "on") {
lightsOn();
return 1;
} else if (command == "off") {
lightsOff();
return 0;
} else {
if(light) {
lightsOff();
return 0;
} else {
lightsOn();
return 1;
}
}
}
// Function to turn the lights off.
void lightsOff() {
digitalWrite(relay, HIGH);
digitalWrite(led, LOW);
light = false;
}
// Function to turn the lights on.
void lightsOn() {
digitalWrite(relay, LOW);
digitalWrite(led, HIGH);
light = true;
}
// This function returns the current state.
bool getLightState() {
return light;
}