-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathservo.ino
58 lines (48 loc) · 1.46 KB
/
servo.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
void servo_setup(){
servo.attach(SERVO_PIN,500,2600);
servo.write(90);
}
void servo_lock(){
Serial.println("[Servo] locking started");
for(int angle = SERVO_NEUTRAL_ANGLE; angle >= SERVO_LOCK_ANGLE; angle -= SERVO_INCREMENT){
servo.write(angle);
//delay(SERVO_DELAY);
}
for(int angle = SERVO_LOCK_ANGLE; angle <=SERVO_NEUTRAL_ANGLE; angle += SERVO_INCREMENT){
servo.write(angle);
//delay(SERVO_DELAY);
}
Serial.println("[Servo] locking finished");
servo_lock_request = false;
}
void servo_unlock(){
Serial.println("[Servo] unlocking started");
for(int angle = SERVO_NEUTRAL_ANGLE; angle <= SERVO_UNLOCK_ANGLE; angle += SERVO_INCREMENT){
servo.write(angle);
//delay(SERVO_DELAY);
}
for(int angle = SERVO_UNLOCK_ANGLE; angle >=SERVO_NEUTRAL_ANGLE; angle -= SERVO_INCREMENT){
servo.write(angle);
//delay(SERVO_DELAY);
}
Serial.println("[Servo] unlocking finished");
servo_unlock_request = false;
}
void handle_servo(){
if(servo_unlock_request){
Serial.println("Lock request: unlock");
servo_unlock();
iot_kernel.device_state = "unlocked";
iot_kernel.mqtt_publish_state();
digitalWrite(UNLOCK_LED_PIN,HIGH);
digitalWrite(LOCK_LED_PIN,LOW);
}
if(servo_lock_request){
Serial.println("Lock request: lock");
servo_lock();
iot_kernel.device_state = "locked";
iot_kernel.mqtt_publish_state();
digitalWrite(LOCK_LED_PIN,HIGH);
digitalWrite(UNLOCK_LED_PIN,LOW);
}
}