Arduino 2 button calculator

Hello, I’m trying to make a two button calculator with the arduino. I’m using the potentiometer as a dial to select a number. I’m trying to code it as one button(A button) acts like a enter and the other button (B button) when it’s pressed goes to the next step. I’m having a hard time trying to make the A button select a number when using the potentiometer.
#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

int buttonApin = 2;
int buttonBpin = 3;

float firstNum;
float secondNum;
float answer;

String oper;

void setup() {
int sensorValue = analogRead(A0);
pinMode(buttonApin, INPUT_PULLUP);
pinMode(buttonBpin, INPUT_PULLUP);

void loop() {
while ((digitalRead(buttonApin) == HIGH)){ // reads an integer value from the pot and displays it
lcd.begin(16, 2);
lcd.print(“Select a number”);
int sensorValue = analogRead(A0);
if (digitalRead(buttonApin) == LOW) { // trying to make this loop as the A button selector

while ((digitalRead(buttonBpin) == LOW)){
lcd.begin(16, 2);
lcd.print(“Pick an Operator”);
lcd.print(" + - * / ")


Your post was MOVED to a more suitable location.

Could you also review How to get the best out of this forum as it will help you find your way around.

How do you select the operator ?


if (digitalRead(buttonApin) == LOW) { // trying to make this loop as the A button selector
  firstOperand = sensorValue;

You may also want to look up ‘polish notation’.

nice idea. I like it. :+1:

i was going to make it so after selecting the first number, the operators would show up. Then a button push would correlate to an operator ie 1 push is add 2 pushes is subtract etc

what do you mean firstoperand? like string the sensor value to the operation?

Simply that, if you’re going to add two numbers, one of them has to be first. The other is second.

ok thank makes sense. But that means id have to have an int and string together, because the operator would be a string.

How should the operation be done if two numbers, e.g. 45+35= result, are to be calculated. Which buttons should be operated how and how should the slider be operated?

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.


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... :grinning: :+1: :coffee: :australia:

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

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()
  sensorValue = analogRead(A0);
  firstNumber = map(sensorValue, 0, 1023, 0, 99);  //valid number range is 0 to 99

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. :grinning:

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
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.print("first number");

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;

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() ).