/
valentine_iot.ino
44 lines (39 loc) · 981 Bytes
/
valentine_iot.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
#include <Servo.h>
/*
* Valeria's Valentine's Day Box
* =============================
* Northshore Christian Academy
* 4th Grade
* February 14th, 2017
*/
Servo myServo;
int const toggleSwitch = 2;
int const greenLight = 12;
int const redLight = 13;
int const openAngle = 0;
int const closedAngle = 95;
int onOff;
void setup() {
myServo.attach(9);
pinMode(toggleSwitch, INPUT);
pinMode(greenLight, OUTPUT);
pinMode(redLight, OUTPUT);
}
void loop() {
onOff = digitalRead(toggleSwitch);
if (onOff == LOW) {
// The Box should close.
myServo.write(closedAngle);
// Make light Red.
digitalWrite(redLight, HIGH);
digitalWrite(greenLight, LOW);
} else if (onOff == HIGH) {
// The Box should open.
myServo.write(openAngle);
// Make light Green.
digitalWrite(greenLight, HIGH);
digitalWrite(redLight, LOW);
}
// Wait 100ms before checking again.
delay(100);
}