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

Time:09-16

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() {
  buttonState = digitalRead(PIN_BUTTON);
  //state 1- choosing the first value
  if (buttonCounter == 0) {
    
    //mapping and showing potValueForCalculations on the display
    potValue = analogRead(PIN_POTMETER);
    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) {
    potValue = analogRead(PIN_POTMETER);
    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) {
    potValue = analogRead(PIN_POTMETER);
    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);
  }
}

CodePudding user response:

Instead of checking for buttonState being LOW, you should check for buttonState changing to LOW.

It could look roughly like this:

int prevButtonState = 0;
int newButtonState = 0;

void loop() {
  newButtonState = digitalRead(PIN_BUTTON);
  if( newButtonState == LOW && prevButtonState != LOW) {
  // ...
  }
  prevButtonState = newButtonState;
}

N.B. Depending on the hardware involved, you sometimes also need to account for the button "bouncing" a bit when released. You can do that by checking that the button stays low for some amount of time before treating it as "pressed".

  • Related