# Arduino 2 button calculator

I would like to make it as such. Run the code, “Select an Number” with pot values underneath would appear. Push button A to select a value. Pressing button B would show “Pick an operator” shows up with operators underneath. Picking the operator would done by case statements. If button A is pressed once it means add, twice means subtract, three times means multiply and four times means divide. After getting the operator, pressing button B again would show “Pick a 2nd number” would appear with pot values underneath. After pressed button A to select the second number, it would do the calculation and output the answer.

Hi,

Why don't you now use the pot as the +, - ,x, / selector. You used it to enter a value so getting it to select one of four should be a doddle.

Tom...

ok thank makes sense but im just having a hard time figuring out the code so that when i press the button it saves the integer value. If i can get that the rest of the project will be easy

Having looked at your code I'm still struggling a bit to follow what is happening. You seem to have missed out some important steps, unless I haven't understood it properly.

First: you need to decide what range of input numbers the calculator can handle. For example, 0 to 99? 0 to 999? Do you want to include negative numbers?

Anyway, once you've decided that, you need to understand that the A/D converter with the potentiometer on it will create an integer value in the range 0 to 1023. So you need to convert that range to your input number range. The map() function would be useful for this:

``````sensorValue = analogRead(A0);
firstNumber = map(sensorValue, 0, 1023, 0, 99);  //valid number range is 0 to 99
lcd.print(firstNumber);
``````

So, firstNumber ends up with the first number stored in it, and it gets printed out on the display as you change the potentiometer.

There is a LOT more for you to work on yet, but does that help to get started?

Now, some advice (if I may). You are trying to do too much at once. I can tell from your code that you are a beginner to Arduino and 'C' (there are several mistakes in your code), so the best thing to do is build up this program in tiny steps.

First, just create a program that does only what I have written above (it isn't complete - so don't cut-and-paste it!). In pseudocode, it would look something like this:

``````int sensorValue = 0;

void setup()
{
// put your setup code here, to run once:
}

void loop()
{
firstNumber = map(sensorValue, 0, 1023, 0, 99);  //valid number range is 0 to 99
lcd.setCursor(0,1);
lcd.print(firstNumber);
Serial.println(firstNumber);
delay(100);
}``````

Once that is working, sort out the obvious bug in it (to do with overwriting the previous number).

Then, add code to check for the button press each time round the loop. Think first about whether you want to check for "Is the button down?" or "Has the button been pressed since the last check?". It's a very important difference! Also, think about whether you want to respond to pressing the button or releasing the button. What if the user presses the button, and then turns the potentiometer before releasing it?

Another point: do you really need two buttons, one to store and one to move on? I don't think so. Store the number each time round the loop. Use a button to move on to the next step. And @TomGeorge's suggestion to use the potentiometer to select the operator is excellent and makes for a very intuitive interface.

Anyway, you get the idea: don't try to make it all work at once. Get one part working first, then add to it bit by bit.

It's more complicated than you think! But it is an excellent starter project.

1 Like

no you're probably right, I most likely left out important steps. As of right know the the pot reads values from 0-1024 so the calculator would be able to compute those values.
I started it as trying to layout everything I need for the calculator i.e. get first value then next step etc. etc. so that I'd be able to work on each step.
And yes this is my first time using C, I'm familiar with java and i see some parallels to it but first time creating anything in C.
I feel like using two buttons would help me further understand how circuits and code go hand in hand but yes one button would be a lot easier.

That's a good approach for the design. For the implementation, because you are new, break your design into small stand-alone steps, otherwise you can get in a right mess trying to debug the "final" code all in one go.

just implemented the 1-100 values part of the code and it works well. Now to implement saving the pot value. thinking about a code like this
A = potvalue;
if(pass == 1)
A = potvalue
else
B = potvalue;

That's a perfectly good way of doing it, although you don't need the first line, because you read potvalue into A or B straight afterwards anyway.

There are many other ways to achieve this, some of which are probably better, but don't worry about that. Get it working first, then improve it. (This approach is best for beginners, I don't advocate it for experienced users who will often aim for making it the best possible structure first time round.)

so i tried fixing it so that its
if (digitalRead(buttonApin) == HIGH)
firstNum = sensorValue;

lcd.begin(16, 2);
lcd.setCursor(0,1);
lcd.print("first number");
lcd.print(firstNum);

but when I press the button down, the screen doesnt update

So now you need to start debugging. The first thing I'd do is put a print statement after reading the sensor value:

``````firstNum = sensorValue;
println(sensorValue);
``````

Then you will see if it is picking up the button press, and what it is reading from the sensor.

You do realise this code will repeat continuously until you release the button? (Assuming it's inside your loop() ).

Hi,

``````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...

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:
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;
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() {

Serial.begin(9600);
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
lcd.clear();
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

break;
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
lcd.print(sensorValue);
delay(800);
break;
case 2:
lcd.print("Choose 2nd #");
break;
case 3:
lcd.print("Pick Operation");
break;
case 4:
break;
}
}
// 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);
``````

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.