-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal_code.ino
More file actions
286 lines (259 loc) · 9.11 KB
/
Copy pathfinal_code.ino
File metadata and controls
286 lines (259 loc) · 9.11 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include <LiquidCrystal_I2C.h> //Header file for LCD from https://www.arduino.cc/en/Reference/LiquidCrystal
#include <Wire.h>
#include <Keypad.h>
// Rotary encoder pins
#define ENCODER_CLK 10
#define ENCODER_DT 9
#define ENCODER_BTN 8
// Keypad pins
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
{ '1', '2', '3', '+' },
{ '4', '5', '6', '-' },
{ '7', '8', '9', '.' },
{ '*', '0', '/', '=' }
};
uint8_t colPins[COLS] = { 4, 5, 6, 7 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { 0, 1, 2, 3 }; // Pins connected to R1, R2, R3, R4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
// Define display pins
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
pinMode(ENCODER_CLK, INPUT);
pinMode(ENCODER_DT, INPUT);
pinMode(ENCODER_BTN, INPUT_PULLUP);
lcd.init();
lcd.backlight();
}
char inputs[16] = ""; // input of user
int inputIdx = 0; // which index are we inputting right now
int lastClk = HIGH; // makes sure that rotary encoder is not called too many times
bool reset = false; // this is true once we have calculated a result
long int lastPress = 0; // how long since we pressed the rotary encoder button
void updateCursor() { // blinking cursor
if (millis() / 250 % 2 == 0 ) {
lcd.cursor();
} else {
lcd.noCursor();
}
}
void clearAll(){ // clears the variables once results have been calculated
memset(inputs,'\0',strlen(inputs));
inputIdx = 0;
lastClk = HIGH;
reset = true;
lastPress = 0;
lcd.setCursor(0,0);
}
void loop() {
// handles rotary encoder button press, deletes a digit when pressed
if (digitalRead(ENCODER_BTN) == LOW && millis() - lastPress > 300) { // button was pressed
lastPress = millis();
memmove(inputs+inputIdx, inputs+inputIdx+1, strlen(inputs) - inputIdx);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(inputs);
lcd.setCursor(inputIdx,0);
}
// handles rotary encoder rotation
int newClk = digitalRead(ENCODER_CLK);
if (newClk != lastClk) { // There was a change on the CLK pin
lastClk = newClk;
int dtValue = digitalRead(ENCODER_DT);
if (newClk == LOW && dtValue == LOW) { // clockwise, move to right
if (inputs[inputIdx] != '\0'){ // don't move cursor when we are at rightmost digit
inputIdx++;
lcd.setCursor(inputIdx, 0);
}
}
if (newClk == LOW && dtValue == HIGH) { // counterclockwise, move to left
if (inputIdx > 0){ // don't go to negative indexes
inputIdx--;
lcd.setCursor(inputIdx, 0);
}
}
}
updateCursor(); // blinking cursor
// handles keypad input
char key = keypad.getKey();
if (key != NO_KEY) {
if (key == '='){ // calculate results
char currNum[16] = "";
int currNumIdx = 0;
double nums[8] = {0,0,0,0,0,0,0,0};
int numsIdx = 0;
char operators[8] = {'e','e','e','e','e','e','e','e'};
int opsIdx = 0;
bool doBreak = false;
for (int i = 0; i <= 16; i++) { // parse string input to numbers and operators
char currInput = inputs[i];
switch (currInput){
case '.': case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
currNum[currNumIdx] = currInput;
currNumIdx++;
break;
case '+': case '-': case '*': case '/':
if (currNumIdx != 0){ // there is a number that hasn't been added to nums array yet
currNumIdx = 0;
nums[numsIdx] = strtod(currNum, (char **)NULL);
memset(currNum,"",sizeof(currNum));
numsIdx++;
}
operators[opsIdx] = currInput;
opsIdx++;
break;
default: // no more inputs, end of string
if (currNumIdx != 0){ // there is a number that hasn't been added to nums array yet
currNumIdx = 0;
nums[numsIdx] = strtod(currNum, (char **)NULL);
memset(currNum,"",sizeof(currNum));
numsIdx++;
}
doBreak = true;
break;
}
if(doBreak){break;}
}
// do the mathematical calculations for our input
for (int phase = 0; phase < 2; phase++) { // 0 means multiplication phase, 1 means sum phase
for (int i = 0; i < opsIdx; i++) { // loop through operations
double firstnum = nums[i];
double secondnum = nums[i+1];
double newResult = 0.0;
switch(phase){
case 0: // we only multiply and divide in phase 0
switch(operators[i]){
case '*':
newResult = firstnum * secondnum;
nums[i] = newResult;
nums[i+1] = newResult;
// This following section updates results
if (i < numsIdx){ // avoid going over list bounds
for (int j = i+1; j <= numsIdx; j++) {
if (operators[j] == 'e'){
nums[j+1] = newResult;
}
else{
break;
}
}
}
if (i > 0){
for (int j = i-1; j >= 0; j--) {
if (operators[j] == 'e'){
nums[j] = newResult;
}
else{
break;
}
}
}
operators[i] = 'e'; // this operation has been calculated
break;
case '/':
newResult = firstnum / secondnum;
nums[i] = newResult;
nums[i+1] = newResult;
// This following section updates results
if (i < numsIdx){ // avoid going over list bounds
for (int j = i+1; j <= numsIdx; j++) {
if (operators[j] == 'e'){
nums[j+1] = newResult;
}
else{
break;
}
}
}
if (i > 0){
for (int j = i-1; j >= 0; j--) {
if (operators[j] == 'e'){
nums[j] = newResult;
}
else{
break;
}
}
}
operators[i] = 'e'; // this operation has been calculated
break;
}
break;
case 1: // we only add and subtract in phase 1
switch(operators[i]){
case '+':
newResult = firstnum + secondnum;
nums[i] = newResult;
nums[i+1] = newResult;
// This following section updates results
if (i < numsIdx){ // avoid going over list bounds
for (int j = i+1; j <= numsIdx; j++) {
if (operators[j] == 'e'){
nums[j+1] = newResult;
}
else{
break;
}
}
}
if (i > 0){
for (int j = i-1; j >= 0; j--) {
if (operators[j] == 'e'){
nums[j] = newResult;
}
else{
break;
}
}
}
operators[i] = 'e'; // this operation has been calculated
break;
case '-':
newResult = firstnum - secondnum;
nums[i] = newResult;
nums[i+1] = newResult;
// This following section updates results
if (i < numsIdx){ // avoid going over list bounds
for (int j = i+1; j <= numsIdx; j++) {
if (operators[j] == 'e'){
nums[j+1] = newResult;
}
else{
break;
}
}
}
if (i > 0){
for (int j = i-1; j >= 0; j--) {
if (operators[j] == 'e'){
nums[j] = newResult;
}
else{
break;
}
}
}
operators[i] = 'e'; // this operation has been calculated
break;
}
break;
}
}
}
lcd.setCursor(0, 1);
lcd.print('=');
lcd.print(String(nums[0]));
clearAll();
}
else{ // any other button was pressed (add digits to input string)
if(reset){ // after results calculation we clear the screen
lcd.clear();
reset = false;
}
lcd.print(key);
inputs[inputIdx] = key;
inputIdx++;
}
}
}