-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAula9_Motor_Servo_Projeto_Portao.ino.ino.ino
More file actions
90 lines (72 loc) · 1.9 KB
/
Aula9_Motor_Servo_Projeto_Portao.ino.ino.ino
File metadata and controls
90 lines (72 loc) · 1.9 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
int pos2;
const int pinLed = 8;
const int btn = 2;
int pressionadoAnterior;
int ligado;
int btnState;
void setup() {
Serial.begin(9600);
while (!Serial) {}
myservo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.println("Iniciando");
pinMode(pinLed, OUTPUT);
pinMode(btn, INPUT);
}
void loop() {
btnState = digitalRead(btn);
if(btnState == 1){
Led();
//ligarLampada(btnState);
abrir();
}
}
void Led() {
//int btnState = digitalRead(btn);
Serial.print("btnStatus: ");
Serial.println(btnState);
if (btnState && (pressionadoAnterior == LOW)) {
ligado = 1 - ligado;
ligarLampada(ligado);
}
Serial.print("btnStatus: ");
Serial.println(btnState);
pressionadoAnterior = btnState;
}
void ligarLampada(int sim) {
digitalWrite(pinLed, sim);
Serial.print("ligarLampada: ");
Serial.println(sim);
// reportar o estado do led via MQTT
}
void abrirPortao(int sim) {
Serial.println("Abrindo portão e sim = 1");
abrir();
}
// abre o portao via servo
//abrir();
// enviar mensagem mqtt
void abrir() {
Serial.println("Abrindo portão");
delay(1000);
for (pos = 0; pos <= 180; pos++) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
//Serial.println(pos);
delay(50); // waits 15ms for the servo to reach the position
}
Serial.println("Portão aberto");
Led();
}
void fechar() {
Serial.println("Fechando...");
Serial.println(pos);
for (pos2 = pos; pos2 >= 0; pos2--) {
myservo.write(pos2); // tell servo to go to position in variable 'pos'
delay(50);
}
Serial.println(pos2);
}