-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBotao_Liga_LED
More file actions
75 lines (58 loc) · 1.54 KB
/
Botao_Liga_LED
File metadata and controls
75 lines (58 loc) · 1.54 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
const int pinLed = 8;
const int btn = 13;
int pressionadoAnterior;
int ligado;
int btnState = 0;
int guardaState = 0;
long previousMillis = 0; // Variável de controle do tempo
long redLedInterval = 0; // Tempo de espera para o próximo comando do botão
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() {
unsigned long currentMillis = millis(); //Tempo atual em ms
btnState = digitalRead(btn);
if (btnState == 1) {
if (currentMillis - previousMillis > redLedInterval) {
previousMillis = currentMillis; // Salva o tempo atual
Led();
guardaState = guardaState + 1; // aqui começa o guardaState
// abrir();
}
else {
Serial.println("Aguarde");
}
}
else
{
// alguma mensagem ? acho que nao precisa deste else
}
Serial.println(btnState);
Serial.flush();
}
void Led() {
Serial.print("btnStatus: ");
Serial.println(guardaState);
if (guardaState == 1) {
//ligado = 1 - ligado;
ligarLampada(1);
}
else {
desligarLampada(0);
}
}
void ligarLampada(int sim) {
digitalWrite(pinLed, HIGH);
guardaState = guardaState - 2;
redLedInterval = 3000; // define o tempo de intervalo que a lampada ficara acesa até o proximo comando
// reportar o estado do led via MQTT
}
void desligarLampada(int nao) {
digitalWrite(pinLed, nao);
// reportar o estado do led via MQTT
}