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
}