Arduino 2 button calculator


lcd.begin(16, 2);

Only needs to be used once, and that is in your setup() part of your code.
You do not need the restart it each time you send commends.

Tom... :grinning: :+1: :coffee::australia:

Well I gues take a piece of paper and a pencil and design a program shedule, step by step.
Keep in mind to structure your design into the following sections:
Input: read buttons
Processing: either do calculation or selection of opertion
Output: display the information

This doing strategie makes the coding and debugging of the sketch in edtion more easy. Try it.

thank you your response is helpful. Now I'm able to save the first number value. I just to press the second button to start looking for the second number. Right now I have it in a loop so when I hold it down it'll start the next part but I need to not constantly hold it.

so i started using case statements for the code. When i get to the "Choose a number" case i tried to put in the code that reads values from the potentiometer but it doesnt update and becomes stuck on a number. I tried it in other parts of the code and it didnt work, only when there is no case statements

#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
int buttonApin = 2;
int buttonBpin = 3;
const byte buttonPin = 3;
int sensorValue = analogRead(A0);
int firstNum;
int secondNum;
float answer;
String oper;
int buttonPushCounter = 0; // counter for the number of button presses
boolean buttonState = LOW; // current state of the button
boolean lastButtonState = LOW;

void setup() {

pinMode(buttonApin, INPUT_PULLUP);
pinMode(buttonBpin, INPUT_PULLUP);
lcd.print("Hello Calculator");

void loop() {

buttonState = digitalRead(buttonPin); // read the pushbutton input pin:
if (buttonState != lastButtonState) // compare the buttonState to its previous state
if (buttonState == LOW) // if the current state is HIGH then the button went from off to on:
buttonPushCounter++; // add one to counter
if (buttonPushCounter > 4) // if couter over 4 reset the counter to 1 to show "Choose 1st #" and not "Hello All"
buttonPushCounter = 0;

  switch (buttonPushCounter)                          // choose what to display based on buttonPushCounter value
    case 0:
      lcd.begin(16, 2);
      lcd.print("Hello All"); // show "Hello All until first button press

    case 1:
      lcd.begin(16, 2);
      lcd.print("Choose 1st #");
      lcd.setCursor(0, 1);
      int sensorValue  = analogRead(A0);
      sensorValue = map(sensorValue, 0, 1023, 0, 100);  //number range is 0 to 99
    case 2:
      lcd.print("Choose 2nd #");
    case 3:
      lcd.print("Pick Operation");
    case 4:
      lcd.print("Final answer");
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;


Hello and good morning
Did you take a piece of paper and pencil to draw a program schedule with repect to my post?

This procedure will help you to design a sketch that will work.

yea I did but it didn't work for this instance. I just need to figure out how to output the pot values while in certain case statements

case 1 seems to have a potential scope issue with respect to "sensorValue"

Please remember to use code tags when posting code

ok will do i just copied and pasted my code into it and only some were in the code format. I have the sensor value code essential the same in my other code with the lcd.print(sensorValue) being printed first. Then with this code i have it being printed in the case statement. The potentiometer works because moving it and going to the second case the number changes. But in case 1 it doesnt seem to loop within it

In case 1, lose the "int" in

int sensorValue  = analogRead(A0);