-
Notifications
You must be signed in to change notification settings - Fork 0
/
servo_bluetooth.ino
59 lines (49 loc) · 1.37 KB
/
servo_bluetooth.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
#include <Servo.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(11,12);
Servo myservo;
void setup() {
Serial.begin(38400);
Serial.println("connection on line 38400");
mySerial.begin(38400);
myservo.attach(8);
}
int pos = 0;
int unlockDistance = 83;
int lockDistance = 150;
char command = 0;
void loop() {
if(mySerial.available()){
Serial.println("caught something");
command = mySerial.read();
Serial.println(command);
if(command == '0') {
unlock();
} else if (command == '1') {
lock();
}
}
if(Serial.available()){
Serial.println("caught something");
command = Serial.read();
Serial.println(command);
if(command == '0') {
unlock();
} else if (command == '1') {
lock();
}
}
}
void lock() {
for (pos = unlockDistance; pos < lockDistance; pos += 2) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(30); // waits 15 ms for the servo to reach the position
}
}
void unlock() {
for (pos = lockDistance; pos > unlockDistance; pos -= 2) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(30); // waits 15 ms for the servo to reach the position
}
}