-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtabuada_digital.ino
178 lines (149 loc) · 4.26 KB
/
tabuada_digital.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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd1(0x3f, 16, 2); // Endereço 0x3F, 16 colunas, 2 linhas (Limpo) (Pergunta)
LiquidCrystal_I2C lcd2(0x22, 16, 2); // Endereço 0x22, 16 colunas, 2 linhas (A0 e A2) (Resposta 1 | botao 6)
LiquidCrystal_I2C lcd3(0x26, 16, 2); // Endereço 0x26, 16 colunas, 2 linhas (A0) (Respota 2 | botao 7)
LiquidCrystal_I2C lcd4(0x27, 16, 2); // Endereço 0x27, 16 colunas, 2 linhas (lcd amarelo) (Resposta 3 | botao 8)
const int buttonPin1 = 6; // Botão 1
const int buttonPin2 = 7; // Botão 2
const int buttonPin3 = 8; // Botão 3
int rCorreta; // Armazena a resposta correta
void setup() {
lcd1.init(); // Inicialize o Display 1
lcd2.init(); // Inicialize o Display 2
lcd3.init(); // Inicialize o Display 3
lcd4.init(); // Inicialize o Display 4
lcd1.backlight();
lcd2.backlight();
lcd3.backlight();
lcd4.backlight();
lcd1.setCursor(0, 0);
lcd2.setCursor(0, 0);
lcd3.setCursor(0, 0);
lcd4.setCursor(0, 0);
lcd1.print("LCD 1 OK");
delay (500);
lcd2.print("LCD 2 OK");
delay (500);
lcd3.print("LCD 3 OK");
delay (500);
lcd4.print("LCD 4 OK");
delay (2000);
pinMode(buttonPin1, INPUT_PULLUP); // Configura o Botão 1
pinMode(buttonPin2, INPUT_PULLUP); // Configura o Botão 2
pinMode(buttonPin3, INPUT_PULLUP); // Configura o Botão 3
randomSeed(analogRead(0)); // Inicializa o gerador de números aleatórios
geraQuestao(); // Gera a primeira pergunta
}
void loop() {
int b1Stado = digitalRead(buttonPin1);
int b2Stado = digitalRead(buttonPin2);
int b3Stado = digitalRead(buttonPin3);
if (b1Stado == LOW || b2Stado == LOW || b3Stado == LOW) {
if (b1Stado == LOW && rCorreta == 1) {
displayResult(true);
} else if (b2Stado == LOW && rCorreta == 2) {
displayResult(true);
} else if (b3Stado == LOW && rCorreta == 3) {
displayResult(true);
} else {
displayResult(false);
}
delay(2000); // Aguarda 2 segundos antes de gerar a próxima pergunta
geraQuestao();
}
}
// Gera a pergunta e as respostas
void geraQuestao() {
int num1 = random(10);
int num2 = random(10);
rCorreta = random(1, 4);
lcd1.clear();
lcd1.setCursor(0, 0);
lcd1.print("Pergunta:");
lcd1.setCursor(0, 1);
lcd1.print(num1);
lcd1.setCursor(5, 1);
lcd1.print("x");
lcd1.setCursor(8, 1);
lcd1.print(num2);
int result = num1 * num2;
int rErrado1, rErrado2;
// Gera valores aleatórios para as respostas erradas
do {
rErrado1 = random(0, 100);
} while (rErrado1 == result);
do {
rErrado2 = random(0, 100);
} while (rErrado2 == result || rErrado2 == rErrado1);
// Exibe as respostas
switch (rCorreta) {
case 1:
lcd2.clear();
lcd2.setCursor(0, 0);
lcd2.print("Resposta 1:");
lcd2.setCursor(0, 1);
lcd2.print(result);
lcd3.clear();
lcd3.setCursor(0, 0);
lcd3.print("Resposta 2:");
lcd3.setCursor(0, 1);
lcd3.print(rErrado1);
lcd4.clear();
lcd4.setCursor(0, 0);
lcd4.print("Resposta 3:");
lcd4.setCursor(0, 1);
lcd4.print(rErrado2);
break;
case 2:
lcd2.clear();
lcd2.setCursor(0, 0);
lcd2.print("Resposta 1:");
lcd2.setCursor(0, 1);
lcd2.print(rErrado1);
lcd3.clear();
lcd3.setCursor(0, 0);
lcd3.print("Resposta 2:");
lcd3.setCursor(0, 1);
lcd3.print(result);
lcd4.clear();
lcd4.setCursor(0, 0);
lcd4.print("Resposta 3:");
lcd4.setCursor(0, 1);
lcd4.print(rErrado2);
break;
case 3:
lcd2.clear();
lcd2.setCursor(0, 0);
lcd2.print("Resposta 1:");
lcd2.setCursor(0, 1);
lcd2.print(rErrado2);
lcd3.clear();
lcd3.setCursor(0, 0);
lcd3.print("Resposta 2:");
lcd3.setCursor(0, 1);
lcd3.print(rErrado1);
lcd4.clear();
lcd4.setCursor(0, 0);
lcd4.print("Resposta 3:");
lcd4.setCursor(0, 1);
lcd4.print(result);
break;
}
}
// Exibe o resultado da pergunta
void displayResult(bool isCorrect) {
lcd1.clear();
lcd1.setCursor(0, 0);
lcd1.print("Resposta:");
if (isCorrect) {
lcd1.setCursor(0, 1);
lcd1.print("CORRETA!");
} else {
lcd1.setCursor(0, 1);
lcd1.print("ERRADA!");
}
lcd2.clear();
lcd3.clear();
lcd4.clear();
}