#include //lets you use the LCD LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // The different pins the shield relies on. const int a0 = a0; //the pin that detects buttons int val = 0; // when you’re finished inputting your first number int number = 0; //your first number int number1 = 0; // your second number int val2 = 0; //When you have input your second number int total = 0; //The Answer to your math int op = 1023; //Reading A0 to determine what function on the calculator you are using char opc = 'NONE'; // This is what your function will be stored as byte divide[8] = { // this is the division symbol B00000, B00100, B00000, B11111, B00000, B00100, B00000, }; void setup() { lcd.begin(16, 2); //sets up the lcd for use lcd.print("Calculator V3"); // this is my third update of the calculator delay(1000); lcd.createChar(0, divide); //this makes up the division symbol for whenever we use lcd.write(byte(0)); lcd.clear(); //clears the screen } void loop() { while(op > 1000){ //the button labeled select has an output of 1023 and thats the highest one. So unless you press anything, this will show. These are your options. //The LCD has a really interesting way of knowing which button you’re pressing with only using one pin. Tell me if you want me to explain it. lcd.print("x "); // multiplication lcd.setCursor(4, 0); //sets where the next print will be lcd.print("- "); //subtraction lcd.setCursor(0, 1); lcd.print(" + "); //addition lcd.setCursor(2, 0); lcd.write(byte(0)); //this writes our division symbol delay(10); op = analogRead(a0); //reads analog pin 0, which is what the buttons are all connected to lcd.clear(); } if (op == 722){ // if it reads 722 (select), it prints the following message while(4){ // this is an infinite loop, as 4 has no "true" or "false" lcd.clear(); // lcd.print("L = x, U = "); // lcd.write(byte(0)); // lcd.setCursor(0, 1); // lcd.print("D = +, R = -"); // delay(100); //waits 1/10 of a second goes back to while in a loop }} if (op == 309){ // if you press down, youre formula will be addition opc = '+'; } if (op == 0){ //if you press right, your formula will be subtraction opc = '-'; } if (op == 480){ //if you press left, your formula will be multiplication opc = 'x'; } if (op == 132){ //if you press up, your formula will be division opc = byte(0); } lcd.print(number); // this prints "Number", which, as of yet, is 0 delay(10); // wait a hundredth of a second val = analogRead(a0); // read analog pin 0 lcd.clear(); //clear number from the screen lcd.print(number); //print the new number, which is still 0 as nothing has been inputted if (val == 132){ // if you press up, number goes up by one number = number + 1; } if (val == 0){ //if you press right, number goes up by 10 number = number + 10; } if (val == 309){ // if you press down, number goes down by 1 number = number - 1; } if (val == 480){ //if you press left, number goes down 10 number = number - 10; } delay(100); //waits one tenth of a second if (val == 722){ // if you press select, the program continues, otherwise, it will go back to where you can change your number again. this all happens in under half a second. while(1){ lcd.clear(); //clears the screen lcd.print(number); //prints the number lcd.print(" "); //space lcd.print(opc); //prints the operation you chose lcd.print(" "); // prints a space again lcd.print(number1); //this is like when you chose your first number, only this is for the second number in your operation delay(100); val2 = analogRead(a0); lcd.clear(); lcd.print(number); lcd.print(" "); lcd.print(opc); lcd.print(" "); lcd.print(number1); if (val2 == 132){ number1 = number1 + 1; } if (val2 == 0){ number1 = number1 + 10; } if (val2 == 480){ number1 = number1 - 10; } if (val2 == 309){ number1 = number1 - 1; //These change your second number, by 1's and 10's, but the second number, not the first } if (val2 == 722){ lcd.clear(); if (op == 309){ total = number + number1; } if (op == 0){ total = number - number1; } if (op == 480){ total = number * number1; } if (op == 132){ total = number / number1; } //these combine your numbers via the operation you chose while(2){ //infinite loop displaying your answer lcd.print(number); lcd.print(" "); lcd.print(opc); lcd.print(" "); lcd.print(number1); //prints your formula on the top line lcd.setCursor(0, 1); lcd.print(total); //prints the total on the second line if (op == 132){ lcd.print(" R:"); //if its division, these lines print the remainder lcd.print(number % number1); } delay(100); lcd.clear(); }} }} lcd.clear(); } //Press reset to make another calculation