How to cancel the last button pressed?

Hello, I wrote a code for 8 buttons. Every time you press a button it will add up a value which is specified to a button. Now I want to program one of the button such that, it will cancel the last button pressed/cancel the last transaction made. The project is similar to a deposit machine. I tried if, else if but didn't work. What should I do? I have attached my code below.

//This is a code for buttons

#include <SPI.h>
int b1 = 5;
int b2 = 6;
int b3 = 9;
int b4 = 10;
int b5 = 11;
int b6 = 12;
int b7 = 13;
int b8 = A0;

int button1State = 0;
int button2State = 0;
int button3State = 0;
int button4State = 0;
int button5State = 0;
int button6State = 0;
int button7State = 0;
int button8State = 0;

double coin = 0.1;
double half = 0.5;
int one = 1;
int five = 5;
int ten = 10;
int twenty = 20
int confirm = 0;
int del = 0;

double total1 = 0.0;
double total2 = 0.0;
double total3 = 0.0;
double total4 = 0.0;
double total5 = 0.0;
double total6 = 0.0;
double total = 0.0;
double ftot = 0.0;

const int buzzer = A1;

void setup()
{
  pinMode(b1, INPUT);
  pinMode(b2, INPUT);
  pinMode(b3, INPUT);
  pinMode(b4, INPUT);
  pinMode(b5, INPUT);
  pinMode(b6, INPUT);
  pinMode(b7, INPUT);
  pinMode(b8, INPUT);
  pinMode(buzzer, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  // read the state of the pushbutton value
  button1State = digitalRead(b1);
  button2State = digitalRead(b2);
  button3State = digitalRead(b3);
  button4State = digitalRead(b4);
  button5State = digitalRead(b5);
  button6State = digitalRead(b6);
  button7State = digitalRead(b7);
  button8State = digitalRead(b8);
  // check if pushbutton is pressed.  if it is, the
  // button1State is HIGH
  if (button1State == HIGH)
  {
    total1 = total1 + one;
    Serial.print("Pay  ");
    Serial.print("1");
    Serial.println("?");
    total = total1+total2+total3+total4+total5+total6;
    Serial.print("Total = ");
    Serial.println(total);
    ftot = ftot + total;
    tone(buzzer, 1000);
    delay(500);
    noTone(buzzer);
    delay(500);
  }
  else
  {
    digitalWrite(buzzer,LOW);
  }
  delay(10); // Delay a little bit to improve simulation performance

  if (button2State == HIGH)
  {
    total2 = total2 + five;
    Serial.print("Pay ");
    Serial.print("5");
    Serial.println("?");
    total = total1+total2+total3+total4+total5+total6;
    Serial.print("Total = ");
    Serial.println(total);
    ftot = ftot + total;
    tone(buzzer, 1000);
    delay(500);
    noTone(buzzer);
    delay(500);
  }
  else
  {
    digitalWrite(buzzer,LOW);
  }
  delay(10); // Delay a little bit to improve simulation performance

  if (button3State == HIGH)
  {
    total3 = total3 + ten;
    Serial.print("Pay  ");
    Serial.print("10");
    Serial.println("?");
    total = total1+total2+total3+total4+total5+total6;
    Serial.print("Total = ");
    Serial.println(total);
    ftot = ftot + total;
    tone(buzzer, 1000);
    delay(500);
    noTone(buzzer);
    delay(500);
  }
  else
  {
    digitalWrite(buzzer,LOW);
  }
  delay(10); // Delay a little bit to improve simulation performance

  if (button4State == HIGH)
  {
    total4 = total4 + twenty;
    Serial.print("Pay ");
    Serial.print("20");
    Serial.println("?");
    total = total1+total2+total3+total4+total5+total6;
    Serial.print("Total = ");
    Serial.println(total);
    ftot = ftot + total;
    tone(buzzer, 1000);
    delay(500);
    noTone(buzzer);
    delay(500);
  }
  else
  {
    digitalWrite(buzzer,LOW);
  }
  delay(10); // Delay a little bit to improve simulation performance

  if (button5State == HIGH)
  {
    total5 = total5 + coin;
    Serial.print("Pay  ");
    Serial.print("0.1");
    Serial.println("?");
    total = total1+total2+total3+total4+total5+total6;
    Serial.print("Total = ");
    Serial.println(total);
    ftot = ftot + total;
    tone(buzzer, 1000);
    delay(500);
    noTone(buzzer);
    delay(500);
  }
  else
  {
    digitalWrite(buzzer,LOW);
  }
  delay(10); // Delay a little bit to improve simulation performance

  if (button6State == HIGH)
  {
    total6 = total6 + half;
    Serial.print("Pay ");
    Serial.print("0.5");
    Serial.println("?");
    total = total1+total2+total3+total4+total5+total6;
    Serial.print("Total = ");
    Serial.println(total);
    ftot = ftot + total;
    tone(buzzer, 1000);
    delay(500);
    noTone(buzzer);
    delay(500);
  }
  else
  {
    digitalWrite(buzzer,LOW);
  }
  delay(10); // Delay a little bit to improve simulation performance

**if (button7State == HIGH && total1>0)**
**  {**
**    total1 = total1 - one;**
**    total = total1 + total2 + total3 + total4+ total5+total6;**
**    Serial.println("Amount deleted.");**
**    Serial.println(total);**
**    tone(buzzer, 1000);**
**    delay(500);**
**    noTone(buzzer);**
**    delay(500);**
**  }**
** **
**  else if (button7State == HIGH && total2>0)**
**  {**
**    total2 = total2 - five;**
**    total = total1 + total2 + total3 + total4+ total5+total6;**
**    Serial.println("Amount deleted.");**
**    Serial.println(total);**
**    tone(buzzer, 1000);**
**    delay(500);**
**    noTone(buzzer);**
**    delay(500);**
**  }**
** **
**  else if(button7State == HIGH && total3>0)**
**  {**
**    total3= total3 - ten;**
**    total = total1 + total2 + total3 + total4+ total5+total6;**
**    Serial.println("Amount deleted.");**
**    Serial.println(total);**
**    tone(buzzer, 1000);**
**    delay(500);**
**    noTone(buzzer);**
**    delay(500);**
**  }  **
** **
**  else if(button7State == HIGH && total4>0)**
**  {**
**    total4 = total4 - twenty;**
**    total = total1 + total2 + total3 + total4+ total5+total6;**
**    Serial.println("Amount deleted.");**
**    Serial.println(total);**
**    tone(buzzer, 1000);**
**    delay(500);**
**    noTone(buzzer);**
**    delay(500);**
**  }**
** **
**  else if(button7State == HIGH && total5>0)**
**  {**
**    total5 = total5 - coin;**
**    total = total1 + total2 + total3 + total4+ total5+total6;**
**    Serial.println("Amount deleted.");**
**    Serial.println(total);**
**    tone(buzzer, 1000);**
**    delay(500);**
**    noTone(buzzer);**
**    delay(500);**
**  }**
** **
**  else if(button7State == HIGH && total6>0)**
**  {**
**    total6 = total6 - half;**
**    total = total1 + total2 + total3 + total4+ total5+total6;**
**    Serial.println("Amount deleted.");**
**    Serial.println(total);**
**    tone(buzzer, 1000);**
**    delay(500);**
**    noTone(buzzer);**
**    delay(500);**
**  }**
**  delay(10); // Delay a little bit to improve simulation performance**


  if (button8State == HIGH)
  {
    total = 0; total1 = 0; total2 = 0; total3 = 0; total4 = 0; total5 = 0; total6 = 0;
    Serial.println("Thank You!");
    tone(buzzer, 1000);
    delay(500);
    noTone(buzzer);
    delay(500);
  }
  delay(10); // Delay a little bit to improve simulation performance
}

Welcome to the forum

Is this the code where you attempted to add the cancellation code ? If not, then please post it and explain the problem

By the way, the code wold be much neater if you used arrays

I would probably have gone about it a different way, but within the paradigm you chose I would probably have a variable called "thisTransaction" in each of the if's, like for example:

if (button4State == HIGH)
  {
    thisTransaction = twenty;
    total4 = total4 + thisTransaction;

Then in the code for my "undo" button I'd subtract thisTransaction from the total.

Something like that anyway- that's a bit off the top of my head.

I think you'd better maintain the standard format of if, else if and else loops.

Thank you,

I have edited the code. Added the code I used to subtract/undo the last pressed button. If I press more than one button and then press the button for subtracting, it subtracts the value which was pressed first and then second not the last one. I want to cancel the button pressed most recently.

thank you,

I tried this and it didn't work.

Hi,
See if this code meets your need:

//This is a code for buttons

#include <SPI.h>
int b1 = 5;
int b2 = 6;
int b3 = 9;
int b4 = 10;
int b5 = 11;
int b6 = 12;
int b7 = 13;
int b8 = A0;

double coin = 0.1;
double half = 0.5;
int one = 1;
int five = 5;
int ten = 10;
int twenty = 20;
int confirm = 0;
int del = 0;

double total1 = 0.0;
double total2 = 0.0;
double total3 = 0.0;
double total4 = 0.0;
double total5 = 0.0;
double total6 = 0.0;
double total = 0.0;
double ftot = 0.0;

const int buzzer = A1;

bool flagBt1 = false;
bool flagBt2 = false;
bool flagBt3 = false;
bool flagBt4 = false;
bool flagBt5 = false;
bool flagBt6 = false;
bool flagBt7 = false;
bool flagBt8 = false;
//--------------------------------------------------------------------------
void myTone()
{
  tone(buzzer, 1000);
  delay(500);
  noTone(buzzer);
  delay(500);
}
//--------------------------------------------------------------------------
void setup()
{
  pinMode(b1, INPUT);
  pinMode(b2, INPUT);
  pinMode(b3, INPUT);
  pinMode(b4, INPUT);
  pinMode(b5, INPUT);
  pinMode(b6, INPUT);
  pinMode(b7, INPUT);
  pinMode(b8, INPUT);
  pinMode(buzzer, OUTPUT);
  Serial.begin(9600);
}
//--------------------------------------------------------------------------
void loop()
{
  // check if pushbutton is pressed.  if it is, the
  // button1State is HIGH

  if (flagBt1 == false)
  {
    flagBt1 = true;
    if (digitalRead(b1) == HIGH)
    {
      total1 = total1 + one;
      Serial.print("Pay  ");  Serial.print("1");  Serial.println("?");
      total = total1 + total2 + total3 + total4 + total5 + total6;
      Serial.print("Total = ");  Serial.println(total);
      ftot = ftot + total;
      myTone();
    }
    else
    {
      digitalWrite(buzzer, LOW);
    }
    delay(10); // Delay a little bit to improve simulation performance
  }

  if (flagBt2 == false)
  {
    flagBt2 = true;
    if (digitalRead(b2) == HIGH)
    {
      total2 = total2 + five;
      Serial.print("Pay ");  Serial.print("5");  Serial.println("?");
      total = total1 + total2 + total3 + total4 + total5 + total6;
      Serial.print("Total = ");  Serial.println(total);
      ftot = ftot + total;
      myTone();
    }
    else
    {
      digitalWrite(buzzer, LOW);
    }
    delay(10); // Delay a little bit to improve simulation performance
  }

  if (flagBt3 == false)
  {
    flagBt3 = true;
    if (digitalRead(b3) == HIGH)
    {
      total3 = total3 + ten;
      Serial.print("Pay  ");  Serial.print("10");  Serial.println("?");
      total = total1 + total2 + total3 + total4 + total5 + total6;
      Serial.print("Total = ");  Serial.println(total);
      ftot = ftot + total;
      myTone();
    }
    else
    {
      digitalWrite(buzzer, LOW);
    }
    delay(10); // Delay a little bit to improve simulation performance
  }

  if (flagBt4 == false)
  {
    flagBt4 = true;
    if (digitalRead(b4) == HIGH)
    {
      total4 = total4 + twenty;
      Serial.print("Pay "); Serial.print("20");   Serial.println("?");
      total = total1 + total2 + total3 + total4 + total5 + total6;
      Serial.print("Total = ");  Serial.println(total);
      ftot = ftot + total;
      myTone();
    }
    else
    {
      digitalWrite(buzzer, LOW);
    }
    delay(10); // Delay a little bit to improve simulation performance
  }

  if (flagBt5 == false)
  {
    flagBt5 = true;
    if (digitalRead(b5) == HIGH)
    {
      total5 = total5 + coin;
      Serial.print("Pay  "); Serial.print("0.1"); Serial.println("?");
      total = total1 + total2 + total3 + total4 + total5 + total6;
      Serial.print("Total = ");  Serial.println(total);
      ftot = ftot + total;
      myTone();
    }
    else
    {
      digitalWrite(buzzer, LOW);
    }
    delay(10); // Delay a little bit to improve simulation performance
  }

  if (flagBt6 == false)
  {
    flagBt6 = true;
    if (digitalRead(b6) == HIGH)
    {
      total6 = total6 + half;
      Serial.print("Pay ");  Serial.print("0.5");   Serial.println("?");
      total = total1 + total2 + total3 + total4 + total5 + total6;
      Serial.print("Total = ");  Serial.println(total);
      ftot = ftot + total;
      myTone();
    }
    else
    {
      digitalWrite(buzzer, LOW);
    }
    delay(10); // Delay a little bit to improve simulation performance
  }

  if (flagBt7 == false)
  {
    flagBt7 = true;
    if (digitalRead(b7) == HIGH && total1 > 0)
    {
      total1 = total1 - one;
      total = total1 + total2 + total3 + total4 + total5 + total6;
      Serial.println("Amount deleted.");
      Serial.println(total);
      myTone();
    }

    else if (digitalRead(b7) == HIGH && total2 > 0)
    {
      total2 = total2 - five;
      total = total1 + total2 + total3 + total4 + total5 + total6;
      Serial.println("Amount deleted.");
      Serial.println(total);
      myTone();
    }

    else if (digitalRead(b7) == HIGH && total3 > 0)
    {
      total3 = total3 - ten;
      total = total1 + total2 + total3 + total4 + total5 + total6;
      Serial.println("Amount deleted.");
      Serial.println(total);
      myTone();
    }

    else if (digitalRead(b7) == HIGH && total4 > 0)
    {
      total4 = total4 - twenty;
      total = total1 + total2 + total3 + total4 + total5 + total6;
      Serial.println("Amount deleted.");
      Serial.println(total);
      myTone();
    }

    else if (digitalRead(b7) == HIGH && total5 > 0)
    {
      total5 = total5 - coin;
      total = total1 + total2 + total3 + total4 + total5 + total6;
      Serial.println("Amount deleted.");
      Serial.println(total);
      myTone();
    }

    else if (digitalRead(b7) == HIGH && total6 > 0)
    {
      total6 = total6 - half;
      total = total1 + total2 + total3 + total4 + total5 + total6;
      Serial.println("Amount deleted.");
      Serial.println(total);
      myTone();

    }
    delay(10); // Delay a little bit to improve simulation performance
  }

  if (flagBt8 == false)
  {
    flagBt8 = true;
    if (digitalRead(b8) == HIGH)
    {
      total = 0; total1 = 0; total2 = 0; total3 = 0; total4 = 0; total5 = 0; total6 = 0;
      Serial.println("Thank You!");
      myTone();
    }
    delay(10); // Delay a little bit to improve simulation performance
  }
}

I uploaded the code you shared in the arduino ide. But serial monitor is blank. Pressing the buttons is not showing any result in the serial monitor.

Have you selected 9600 baud rate when opening the serial monitor?

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