-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculadora.ino
93 lines (85 loc) · 2.2 KB
/
calculadora.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
/*Igor Jorge Ferraz 3TII*/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Keypad.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
String input = "";
float num1 = 0;
float num2 = 0;
char operation = ' ';
void setup() {
Serial.begin(9600);
Wire.begin();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
delay(2000);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
Serial.println("Tecla A representa o sinal de + (Soma)");
Serial.println("Tecla B representa o sinal de - (Subtracao)");
Serial.println("Tecla C representa o sinal de * (Multiplicacao)");
Serial.println("Tecla D representa o sinal de / (Divisao)");
Serial.println("Tecla # representa o sinal de = (Igualdade)");
}
void loop() {
char key = keypad.getKey();
if (key != NO_KEY) {
if (key >= '0' && key <= '9') {
input += key;
display.clearDisplay();
display.setCursor(0, 0);
display.println(input);
display.display();
} else if (key == 'A' || key == 'B' || key == 'C' || key == 'D') {
num1 = input.toFloat();
operation = key;
input = "";
} else if (key == '#') {
num2 = input.toFloat();
float result = 0;
switch (operation) {
case 'A':
result = num1 + num2;
break;
case 'B':
result = num1 - num2;
break;
case 'C':
result = num1 * num2;
break;
case 'D':
if (num2 != 0) {
result = num1 / num2;
} else {
result = NAN;
}
break;
default:
break;
}
display.clearDisplay();
display.setCursor(0, 0);
display.print("Resultado: ");
display.println(result);
display.display();
input = "";
num1 = 0;
num2 = 0;
operation = ' ';
}
}
}