forked from FAR-Lab/IDD-Fa19-Lab5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rei_JackInABox.ino
64 lines (54 loc) · 1.71 KB
/
Rei_JackInABox.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
// Box Lab 5
//
// If we see a voltage change on pin 2 the toggle switch on top of the useless box has
// changed position and we need to react!
// A HIGH value on pin 2 means we should activate the servo to open the useless
// box and attempt to return the switch to the "off" position.
// A LOW value on pin 2 means the switch is off and we should return to our
// inital (closed box) state.
#include <Servo.h>
#define servoPin 10
#define switchPin 2
#define closePos 10
#define openPos 110
Servo servo;
int switchState;
int previousSwitchState;
// call this when the input on pin 2 changes (LOW to HIGH *or* HIGH to LOW)
void ToggleSwitch(int switchState)
{
if (switchState == HIGH)
{
servo.write(openPos);
//Serial.println("switch state is HIGH. servo.write(openPos) called to open useless box");
}
else
{
servo.write(closePos);
//Serial.println("switch state is LOW. servo.write(closePos) called to close useless box");
}
previousSwitchState = switchState; // remember that the switch state has changed
delay(1000);
}
void setup()
{
//Serial.begin(9600);
//Serial.println("Useless Box Lab 5");
pinMode(5,OUTPUT);
digitalWrite(5,HIGH);
// start with the box closed and the switch in the off postion
switchState = LOW;
previousSwitchState = LOW;
// connect to our servo and make sure it is in the closed position
servo.attach(servoPin);
servo.write(closePos);
// we should probably pay attention to the switch
pinMode(switchPin, INPUT);
}
void loop()
{
int switchState = digitalRead(switchPin);
if (switchState != previousSwitchState)
ToggleSwitch(switchState);
delay(20);
}