-
Notifications
You must be signed in to change notification settings - Fork 1
/
Arduino_Servo_Controller_Sketch.ino
64 lines (50 loc) · 1.25 KB
/
Arduino_Servo_Controller_Sketch.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
#include <Servo.h> //Servo library
Servo servo; //initialize a servo object for the connected servo
int angle = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // set up Serial library at 9600 bps
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.setTimeout(100);
pinMode(LED_BUILTIN, OUTPUT);
enableServo();
closeDoor();
disableServo();
}
void loop() {
if (Serial.available() > 0) {
// read the incoming byte:
Serial.setTimeout(100);
int incomingNumber = Serial.parseInt();
Serial.setTimeout(10);
Serial.parseInt();
enableServo();
if(incomingNumber == 1) {
openDoor();
} else {
closeDoor();
}
}
}
void enableServo() {
servo.attach(9);
delay(10);
}
void disableServo() {
servo.detach();
}
void openDoor() {
Serial.println("Opening door");
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
servo.write(80);
delay(1500);
}
void closeDoor() {
Serial.println("Closing door");
digitalWrite(LED_BUILTIN, LOW); // turn the LED on (HIGH is the voltage level)
servo.write(180);
delay(1500);
disableServo();
}