forked from Oguelo/ProjetosArduino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontadorMoedas.cpp
More file actions
93 lines (82 loc) · 2.41 KB
/
contadorMoedas.cpp
File metadata and controls
93 lines (82 loc) · 2.41 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
91
92
93
#include <LiquidCrystal.h>
// Definições das portas LCD
LiquidCrystal lcd(4, 5, 6, 7, 8, 9, 10);
// Definições das variáveis que irão guardar a quantidade de moedas
int contUm = 0;
int contDois = 0;
int contTres = 0;
// Variáveis que irão guardar o estado anterior dos sensores
int lastStateDez = LOW;
int lastStateCinquenta = LOW;
int lastStateCem = LOW;
// Constantes para os pinos dos sensores e LEDs
const int sensorDezPin = 13;
const int sensorCinquentaPin = 12;
const int sensorCemPin = 11;
const int ledUmPin = 1;
const int ledDoisPin = 2;
const int ledTresPin = 3;
// Debounce delay para evitar leituras falsas nos sensores
const unsigned long debounceDelay = 50;
void setup() {
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("-0,10:");
lcd.setCursor(8, 0);
lcd.print("-0,50:");
lcd.setCursor(0, 1);
lcd.print("-1,00:");
pinMode(sensorDezPin, INPUT);
pinMode(sensorCinquentaPin, INPUT);
pinMode(sensorCemPin, INPUT);
pinMode(ledUmPin, OUTPUT);
pinMode(ledDoisPin, OUTPUT);
pinMode(ledTresPin, OUTPUT);
}
void loop() {
int sensorDezState = digitalRead(sensorDezPin);
int sensorCinquentaState = digitalRead(sensorCinquentaPin);
int sensorCemState = digitalRead(sensorCemPin);
// Debounce nos sensores
if (sensorDezState != lastStateDez) {
delay(debounceDelay);
sensorDezState = digitalRead(sensorDezPin);
}
if (sensorCinquentaState != lastStateCinquenta) {
delay(debounceDelay);
sensorCinquentaState = digitalRead(sensorCinquentaPin);
}
if (sensorCemState != lastStateCem) {
delay(debounceDelay);
sensorCemState = digitalRead(sensorCemPin);
}
// Contagem das moedas
if (sensorDezState == HIGH && lastStateDez == LOW) {
digitalWrite(ledUmPin, HIGH);
contUm++;
lcd.setCursor(6, 0);
lcd.print(contUm);
delay(1000);
digitalWrite(ledUmPin, LOW);
}
if (sensorCinquentaState == HIGH && lastStateCinquenta == LOW) {
digitalWrite(ledDoisPin, HIGH);
contDois++;
lcd.setCursor(14, 0);
lcd.print(contDois);
delay(1000);
digitalWrite(ledDoisPin, LOW);
}
if (sensorCemState == HIGH && lastStateCem == LOW) {
digitalWrite(ledTresPin, HIGH);
contTres++;
lcd.setCursor(6, 1);
lcd.print(contTres);
delay(1000);
digitalWrite(ledTresPin, LOW);
}
// Atualizar o estado anterior dos sensores
lastStateDez = sensorDezState;
lastStateCinquenta = sensorCinquentaState;
lastStateCem = sensorCemState;
}