#### Arduino RichShield calculator- I need to make it so when I hold down the button there is no effect

So I am making an Arduino RichShield calculator and it works like this:

1. I choose the first number’s value
2. I press the button and it goes into its second state- choosing an operator(+ – * /)
3. I press the button again and I can choose the second number’s value
4. I press the button again and the result shows up
5. When I press it again it resets and I can do another equation

What I need to fix is that if I hold down the button it keeps switching states. I need to make it so that only a normal button press can change states. Here is the code:

``````    #include "Display.h"
//declaring the button and LEDs
const int PIN_BUTTON = 9;
int buttonState = 0;
const int PIN_LEDR = 4;
const int PIN_LEDG = 5;
const int PIN_LEDB = 6;
const int PIN_LEDY = 7;

//declaring potentiometer variables
const int PIN_POTMETER = 14;
int MAX_NUM = 20;
int MIN_NUM = -20;
int potValue = 0;
int potValueForCalculations = 0;

//setting up inputs/outputs for button, LEDs, potentiometer
void setup() {
pinMode(PIN_LEDR, OUTPUT);
pinMode(PIN_LEDG, OUTPUT);
pinMode(PIN_LEDB, OUTPUT);
pinMode(PIN_LEDY, OUTPUT);
pinMode(PIN_POTMETER, INPUT);
pinMode(PIN_BUTTON, INPUT_PULLUP);
Display.show("----");
delay(3000);
}

//declaring variables for mathematical operations/button counter
int buttonCounter = 0;
float inputValue1;
float inputValue2;
float result;
char mathOperator;

void loop() {
//state 1- choosing the first value
if (buttonCounter == 0) {

//mapping and showing potValueForCalculations on the display
potValueForCalculations = map(potValue, 0, 1023, MIN_NUM, MAX_NUM);
Display.show(potValueForCalculations);
delay(100);

//if we press the button we go into state 2
if (buttonState == LOW) {
inputValue1 = potValueForCalculations;
buttonCounter = 1;
delay(1000);
}
}
//state 2- choosing the sign for the mathematical operation
else if (buttonCounter == 1) {
if (potValue <= 256) {
Display.show("a");
mathOperator = 'a';
}
else if (potValue > 256 && potValue <= 512) {
Display.show("s");
mathOperator = 's';
}
else if (potValue > 512 && potValue <= 768) {
Display.show("t");
mathOperator = 't';
}
else {
Display.show("d");
mathOperator = 'd';
}
//if we press button we go into state 3
if (buttonState == LOW) {
buttonCounter = 2;
delay(500);
}
}
//state 3- choosing the second value
else if (buttonCounter == 2) {
potValueForCalculations = map(potValue, 0, 1023, MIN_NUM, MAX_NUM);
Display.show(potValueForCalculations);
delay(100);

//if we press button we go into state 4
if (buttonState == LOW) {
inputValue2 = potValueForCalculations;
buttonCounter = 3;
delay(500);
}
}

//state 4- showing the result
else if (buttonCounter == 3) {

digitalWrite(PIN_LEDG, HIGH);
digitalWrite(PIN_LEDR, LOW);

if (mathOperator == 'a') {
result = inputValue1 + inputValue2;
Display.show(result);
}
else if (mathOperator == 's') {
result = inputValue1 - inputValue2;
Display.show(result);
}
else if (mathOperator == 't') {
result = inputValue1 * inputValue2;
Display.show(result);
}
else if (mathOperator == 'd') {
//if we try to divide by 0 the red LED turns on and the display shows "Err"
if (inputValue2 == 0) {
digitalWrite(PIN_LEDR, HIGH);
digitalWrite(PIN_LEDG, LOW);
Display.show("Err");
}
else {
result = inputValue1 / inputValue2;
Display.show(result);
}
}
//if we press the button again the counter enters the reset threshold- the calculator restarts
if (buttonState == LOW) {
buttonCounter = 4;
delay(500);
}
}
else if (buttonCounter >= 4) {
digitalWrite(PIN_LEDR, LOW);
digitalWrite(PIN_LEDG, LOW);
digitalWrite(PIN_LEDB, LOW);
digitalWrite(PIN_LEDY, LOW);
Display.show("");
buttonCounter = 0;
delay(500);
}
}
``````

Source: Windows Questions C++