Components and supplies
Arduino Mega 2560
Standard LCD - 16x2 White on Blue
Tools and machines
3D Printer (generic)
Soldering iron (generic)
Hot glue gun (generic)
Project description
Code
Calculator_Code.ino
arduino
Edit the pins where the lcd screen and buttons are connected. The code will work fine.
1#include <LiquidCrystal.h> 2 3LiquidCrystal lcd (8, 9, 10, 11, 12, 13); 4 5 6#define max_val 999999 7float firstNum = 0; 8float secondNum = 0; 9float total = 0; 10int insertNum; 11int z = 1; 12int y = 0; 13long a; 14int opAS = 0; 15int NUM = 0; 16int OP; 17 18int one =30; 19int two = 32; 20int three = 42; 21int four =28; 22int five =26; 23int six = 46; 24int seven = 22; 25int eight =24; 26int nine =50; 27int zero = 34; //34 28int decimal = 36; 29int addition = 38; 30int subtraction = 44; 31int multiplication =48; 32int division = 52; 33int Clear = 7; 34int equal = 40; 35 36 37void readKey() { 38 39 if (digitalRead(zero) == HIGH) { 40 insertNum = 0; 41 } 42 else if (digitalRead(one) == HIGH) { 43 insertNum = 1; 44 } 45 else if (digitalRead(two) == HIGH) { 46 insertNum = 2; 47 } 48 else if (digitalRead(three) == HIGH) { 49 insertNum = 3; 50 } 51 else if (digitalRead(four) == HIGH) { 52 insertNum = 4; 53 } 54 else if (digitalRead(five) == HIGH) { 55 insertNum = 5; 56 } 57 else if (digitalRead(six) == HIGH) { 58 insertNum = 6; 59 } 60 else if (digitalRead(seven) == HIGH) { 61 insertNum = 7; 62 } 63 else if (digitalRead(eight) == HIGH) { 64 insertNum = 8; 65 } 66 else if (digitalRead(nine) == HIGH) { 67 insertNum = 9; 68 } 69 else if (digitalRead(decimal) == HIGH) { 70 insertNum = 10; 71 } 72 else if (digitalRead(addition) == HIGH) { 73 insertNum = 11; 74 } 75 else if (digitalRead(subtraction) == HIGH) { 76 insertNum = 12; 77 } 78 else if (digitalRead(multiplication) == HIGH) { 79 insertNum = 13; 80 } 81 else if (digitalRead(division) == HIGH) { 82 insertNum = 14; 83 } 84 else if (digitalRead(Clear) == HIGH) { 85 insertNum = 15; 86 } 87 else if (digitalRead(equal) == HIGH) { 88 insertNum = 16; 89 } 90else{ 91 insertNum = -1; 92} 93 94 95 96} 97 98 99 100 101 102 103 104void setup() 105{ 106pinMode(one, INPUT); 107pinMode(two, INPUT); 108pinMode(three, INPUT); 109pinMode(four,INPUT); 110pinMode(five, INPUT); 111pinMode(six, INPUT); 112pinMode(seven, INPUT); 113pinMode(eight, INPUT); 114pinMode(nine, INPUT); 115pinMode(zero, INPUT); 116pinMode(decimal, INPUT); 117pinMode(addition, INPUT); 118pinMode(subtraction, INPUT); 119pinMode(multiplication, INPUT); 120pinMode(division, INPUT); 121pinMode(Clear, INPUT); 122pinMode(equal, INPUT); 123 124 125lcd.begin(16,2); 126 127} 128 129void loop(){ 130readKey(); 131delay(200); 132 133if(NUM == 0){ 134if (insertNum >= 0 && insertNum <= 9){ 135if(y==0){ 136 137 138a = pow(10, z); 139firstNum = firstNum * (float)a + (float)insertNum; 140} 141 142 143 144if(y==1){ 145a = pow(10, z); 146firstNum = (firstNum * (float)a + (float)insertNum)/(float)a; 147z++; 148} 149 150 151 if ( firstNum > max_val){ 152 lcd.setCursor(0,0); 153 lcd.print("Num Too Large "); 154 } 155 156if ( firstNum <= max_val){ 157 lcd.print(" "); 158 lcd.setCursor(0,0); 159lcd.print(firstNum); 160} 161 162} 163 164 165 166if (insertNum == 10){ 167 y++; 168 169lcd.setCursor(0,1); 170lcd.print(" "); 171lcd.setCursor(0,1); 172lcd.print(" DECIMAL"); 173 174 if(y == 2){ 175 lcd.print(" "); 176 lcd.setCursor(0,0); 177 lcd.print("error"); 178 } 179} 180} 181 182if(NUM == 1) { 183 184if (insertNum >= 0 && insertNum <= 9){ 185 186if(y==0){ 187 188 189a = pow(10, z); 190secondNum = secondNum * (float)a + (float)insertNum; 191} 192 193 194 195if(y==1){ 196a = pow(10, z); 197secondNum = (secondNum * (float)a + (float)insertNum)/(float)a; 198z++; 199} 200 201 202 if ( secondNum > max_val){ 203 lcd.setCursor(0,0); 204 lcd.print("Num Too Large "); 205 } 206 207if ( secondNum <= max_val){ 208 lcd.clear(); 209 lcd.setCursor(0,0); 210lcd.print(secondNum); 211} 212 213} 214 215 216 217if (insertNum == 10){ 218 y++; 219 220lcd.setCursor(0,1); 221lcd.print(" "); 222lcd.setCursor(0,1); 223lcd.print(" DECIMAL"); 224 225 if(y == 2){ 226 lcd.print(" "); 227 lcd.setCursor(0,0); 228 lcd.print("error"); 229 } 230} 231 232 233 234} 235 236 237 238 239 240if(insertNum == 11){ 241 NUM = 1; 242lcd.setCursor(0,1); 243lcd.print(" +"); 244 245if( opAS >= 1){ 246 247if(OP == 1){ 248 if( opAS == 1){ 249 total = (float) firstNum + (float)secondNum; 250 } 251if (opAS >= 2){ 252 total = (float) total + (float) secondNum; 253} 254} 255if(OP == 2){ 256 if( opAS == 1){ 257 total = (float) firstNum - (float)secondNum; 258 } 259if (opAS >= 2){ 260 total = (float) total - (float) secondNum; 261} 262} 263if(OP == 3){ 264 if( opAS == 1){ 265 total = (float) firstNum * (float)secondNum; 266 } 267if (opAS >= 2){ 268 total = (float) total * (float) secondNum; 269} 270} 271if(OP == 4){ 272 if( opAS == 1){ 273 total = (float) firstNum / (float)secondNum; 274 } 275if (opAS >= 2){ 276 total = (float) total / (float) secondNum; 277} 278} 279 280lcd.setCursor(0,0); 281lcd.print(" "); 282lcd.setCursor(0,0); 283lcd.print(total); 284} 285opAS++; 286secondNum = 0; 287OP = 1; 288y = 0; 289z=1; 290} 291 292 293 294 295if(insertNum == 12){ 296 NUM = 1; 297lcd.setCursor(0,1); 298lcd.print(" -"); 299 300if( opAS >= 1){ 301 302if(OP == 1){ 303 if( opAS == 1){ 304 total = (float) firstNum + (float)secondNum; 305 } 306if (opAS >= 2){ 307 total = (float) total + (float) secondNum; 308} 309} 310if(OP == 2){ 311 if( opAS == 1){ 312 total = (float) firstNum - (float)secondNum; 313 } 314if (opAS >= 2){ 315 total = (float) total - (float) secondNum; 316} 317} 318if(OP == 3){ 319 if( opAS == 1){ 320 total = (float) firstNum * (float)secondNum; 321 } 322if (opAS >= 2){ 323 total = (float) total * (float) secondNum; 324} 325} 326if(OP == 4){ 327 if( opAS == 1){ 328 total = (float) firstNum / (float)secondNum; 329 } 330if (opAS >= 2){ 331 total = (float) total / (float) secondNum; 332} 333} 334 335lcd.setCursor(0,0); 336lcd.print(" "); 337lcd.setCursor(0,0); 338lcd.print(total); 339} 340 341opAS++; 342secondNum = 0; 343OP=2; 344y = 0; 345z = 1; 346} 347 348/* 349if(insertNum == 13){ 350 NUM = 1; 351lcd.setCursor(0,1); 352lcd.print(" *"); 353 354if( opAS >= 1){ 355 356if(OP == 1){ 357 if( opAS == 1){ 358 total = (float) firstNum + (float)secondNum; 359 } 360if (opAS >= 2){ 361 total = (float) total + (float) secondNum; 362} 363} 364if(OP == 2){ 365 if( opAS == 1){ 366 total = (float) firstNum - (float)secondNum; 367 } 368if (opAS >= 2){ 369 total = (float) total - (float) secondNum; 370} 371} 372if(OP == 3){ 373 if( opAS == 1){ 374 total = (float) firstNum * (float)secondNum; 375 } 376if (opAS >= 2){ 377 total = (float) total * (float) secondNum; 378} 379} 380if(OP == 4){ 381 if( opAS == 1){ 382 total = (float) firstNum / (float)secondNum; 383 } 384if (opAS >= 2){ 385 total = (float) total / (float) secondNum; 386} 387} 388 389lcd.setCursor(0,0); 390lcd.print(" "); 391lcd.setCursor(0,0); 392lcd.print(total); 393} 394 395opAS++; 396 397OP=3; 398secondNum = 0; 399} 400*/ 401 402if(insertNum == 13){ 403 NUM = 1; 404lcd.setCursor(0,1); 405lcd.print(" *"); 406 407if( opAS > 0){ 408 409if(OP == 1){ 410 if( opAS == 1){ 411 total = (float) firstNum + (float)secondNum; 412 } 413if (opAS >= 2){ 414 total = (float) total + (float) secondNum; 415} 416} 417if(OP == 2){ 418 if( opAS == 1){ 419 total = (float) firstNum - (float)secondNum; 420 } 421if (opAS >= 2){ 422 total = (float) total - (float) secondNum; 423} 424} 425if(OP == 3){ 426 if( opAS == 1){ 427 total = (float) firstNum * (float)secondNum; 428 } 429if (opAS >= 2){ 430 total = (float) total * (float) secondNum; 431} 432} 433if(OP == 4){ 434 if( opAS == 1){ 435 total = (float) firstNum / (float)secondNum; 436 } 437if (opAS >= 2){ 438 total = (float) total / (float) secondNum; 439} 440} 441 442lcd.setCursor(0,0); 443lcd.print(" "); 444lcd.setCursor(0,0); 445lcd.print(total); 446} 447 448opAS++; 449secondNum = 0; 450OP=3; 451y=0; 452z=1; 453} 454 455 456 457 458 459if(insertNum == 14){ 460 NUM = 1; 461lcd.setCursor(0,1); 462lcd.print(" /"); 463 464if( opAS >= 1){ 465 466if(OP == 1){ 467 if( opAS == 1){ 468 total = (float) firstNum + (float)secondNum; 469 } 470if (opAS >= 2){ 471 total = (float) total + (float) secondNum; 472} 473} 474if(OP == 2){ 475 if( opAS == 1){ 476 total = (float) firstNum - (float)secondNum; 477 } 478if (opAS >= 2){ 479 total = (float) total - (float) secondNum; 480} 481} 482if(OP == 3){ 483 if( opAS == 1){ 484 total = (float) firstNum * (float)secondNum; 485 } 486if (opAS >= 2){ 487 total = (float) total * (float) secondNum; 488} 489} 490if(OP == 4){ 491 if( opAS == 1){ 492 total = (float) firstNum / (float)secondNum; 493 } 494if (opAS >= 2){ 495 total = (float) total / (float) secondNum; 496} 497} 498 499lcd.setCursor(0,0); 500lcd.print(" "); 501lcd.setCursor(0,0); 502lcd.print(total); 503} 504 505opAS++; 506secondNum = 0; 507OP=4; 508y=0; 509z=1; 510} 511 512 513if(insertNum == 16){ 514if(NUM == 1){ 515if(OP == 1){ 516 if( opAS == 1){ 517 total = (float) firstNum + (float)secondNum; 518 } 519if (opAS >= 2){ 520 total = (float) total + (float) secondNum; 521} 522} 523if(OP == 2){ 524 if( opAS == 1){ 525 total = (float) firstNum - (float)secondNum; 526 } 527if (opAS >= 2){ 528 total = (float) total - (float) secondNum; 529} 530} 531if(OP == 3){ 532 if( opAS == 1){ 533 total = (float) firstNum * (float)secondNum; 534 } 535if (opAS >= 2){ 536 total = (float) total * (float) secondNum; 537} 538} 539if(OP == 4){ 540 if( opAS == 1){ 541 total = (float) firstNum / (float)secondNum; 542 } 543if (opAS >= 2){ 544 total = (float) total / (float) secondNum; 545} 546} 547 548lcd.clear(); 549lcd.setCursor(0,0); 550lcd.print("="); 551lcd.setCursor(1,0); 552lcd.print(total); 553} 554if(NUM == 0){ 555 lcd.clear(); 556 lcd.setCursor(0,0); 557 lcd.print(firstNum); 558} 559 560} 561 562 563 564 565if(insertNum == 15){ 566 lcd.clear(); 567total = 0; 568 569z = 1; 570y = 0; 571firstNum = 0; 572secondNum = 0; 573opAS = 0; 574NUM = 0; 575 576} 577} 578 579 580 581 582 583 584 585 586 587 588 589 590 591
Comments
Only logged in users can leave comments
Anonymous user
2 years ago
Sorry you can't use a arduino uno because if you do then there would't be anywhere to plug the lcd in.
Anonymous user
2 years ago
Here's a calculator that will work on any board https://www.hackster.io/Joprp05/simple-uno-calculator-01e6fe
FZD1416
0 Followers
•1 Projects
5
3
Basic Calculator | Arduino Project Hub
Anonymous user
6 years ago
Where's the systematic? or am I supposed to work out where to put the wires from the code? For anyone who doesn't have a mega 2560 you can use a arduino uno if you use the analog pins as input.