Push button doesn't work

Hi there, im trying to program an rfid shopping cart
but my push button input doesn't work when to total up the entire merchandise.
Can anyone help me out?
below is my coding.

#include <SPI.h>
#include <MFRC522.h>
#define beep_pin 8
#include <LiquidCrystal.h>
#define t 4000//delay time
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);//arduino pin(rs=2,e=3,D4=4,D5=5,D6=6,D7=7)

int count = 0;
int a;
double total = 0;
int count_prod = 0;
const int BUTTON_PIN = 3; // Arduino pin connected to button's pin
const int BUZZER_PIN = 8; // Arduino pin connected to Buzzer's pin
//int buttonState = digitalRead(BUTTON_PIN); // read new state
int buttonState = 0;
int oldButtonState = LOW;
int ledState = LOW;
int ledPin = 7;
int totalAll (int buttonState);

#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.


void setup()
{

  //  Pin direction


  pinMode(4, OUTPUT);         //RED LED
  pinMode(8, OUTPUT);         //BUZZER
  pinMode(6, OUTPUT);         //GREEN LED
  pinMode(BUTTON_PIN, INPUT); // set arduino pin to input pull-up mode
  pinMode(BUZZER_PIN, OUTPUT);       // set arduino pin to output mode
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);


  lcd.begin(16, 2);// set up the LCD's number of columns and rows
  SPI.begin();      // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522
  Serial.println("Put your card to the reader...");
  Serial.println();
  delay(1000);
  lcd.setCursor(1, 0);//set print point
  lcd.print("AUTOMATIC BILL");//print text
  lcd.setCursor(1, 1);
  lcd.print("SHOPPING CART    ");
  delay(2000);
  lcd.clear();
  lcd.setCursor(2, 0);
  lcd.print("WELCOME TO ");
  lcd.setCursor(2, 1);
  lcd.print("JASCO JAYA `");
  delay(2000);
  lcd.clear();
}


void loop()
{
  int buttonState = digitalRead(BUTTON_PIN);
  digitalWrite(beep_pin, LOW);
  digitalWrite(ledPin, LOW);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Put your card to");
  lcd.setCursor(0, 1);
  lcd.print("the reader......");
  delay(300);



  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent())
  {
    return;
  }
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial())
  {
    return;
  }
  //Show UID on serial monitor
  Serial.print("UID tag :");
  String content = "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++)
  {
    Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
    Serial.print(mfrc522.uid.uidByte[i], HEX);
    content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
    content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  Serial.println();
  Serial.print("Message : ");
  content.toUpperCase();





  if (content.substring(1) == "F3 17 7B 19") //change here the UID of the card/cards that you want to give access
  {
    digitalWrite(beep_pin, HIGH);
    delay(200);
    digitalWrite(beep_pin, LOW);
    delay(100);
    lcd.setCursor(0, 0);
    lcd.print("CADBURY CHOC     ");
    //lcd.print(content.substring(1));
    lcd.setCursor(0, 1);
    lcd.print(" Price(Rm):6.00  ");
    lcd.setCursor(0, 1);
    digitalWrite(4, HIGH);
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
    delay(2000);
    total = total + 6.00;
    count_prod++;
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Total:");
    lcd.setCursor(0, 1);
    lcd.print(total);

    digitalWrite(4, LOW);
    digitalWrite(5, HIGH);
    digitalWrite(6, LOW);
    Serial.println("Authorized access");
    delay(1200);
  }


  if (content.substring(1) == "E6 34 6D F1") //change here the UID of the card/cards that you want to give access
  {
    digitalWrite(beep_pin, HIGH);
    delay(200);
    digitalWrite(beep_pin, LOW);
    delay(100);
    lcd.setCursor(0, 0);
    lcd.print("Yeos Lychee     ");
    //lcd.print(content.substring(1));
    lcd.setCursor(0, 1);
    lcd.print("Price(Rm): 1.20   ");
    lcd.setCursor(1, 1);
    Serial.println("Item Insert");
    delay(1200);
    digitalWrite(4, HIGH);
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
    delay(2000);
    total = total + 1.20;
    count_prod++;
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Total:");
    lcd.setCursor(0, 1);
    lcd.print(total);
    digitalWrite(4, LOW);
    digitalWrite(5, HIGH);
    digitalWrite(6, LOW);

    Serial.println("Authorized access");
    delay(1200);
  }

  if (content.substring(1) == "9A EA 4F BF") //change here the UID of the card/cards that you want to give access
  {
    digitalWrite(beep_pin, HIGH);
    delay(200);
    digitalWrite(beep_pin, LOW);
    delay(100);
    lcd.setCursor(0, 0);
    lcd.print("Toys                 ");
    //lcd.print(content.substring(1));
    lcd.setCursor(0, 1);
    lcd.print("Price(Rm): 10.00     ");
    lcd.setCursor(1, 1);
    Serial.println("Authorized access");
    delay(1200);
    digitalWrite(4, HIGH);
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
    delay(2000);
    total = total + 10.00;
    count_prod++;
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Total:");
    lcd.setCursor(0, 1);
    lcd.print(total);
    digitalWrite(4, LOW);
    digitalWrite(5, HIGH);
    digitalWrite(6, LOW);
    Serial.println("Authorized access");
    delay(1200);

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Total Price :-");

    lcd.setCursor(0, 1);
    lcd.print(total);

    lcd.clear();
    ends();


  }

  if (buttonState == HIGH) {

    digitalWrite(ledPin, LOW); //Turn the LED on
    digitalWrite(beep_pin, HIGH);
    delay(200);
    digitalWrite(beep_pin, LOW);
    delay(100);

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Total Price :-");

    lcd.setCursor(0, 1);
    lcd.print(total);
  }

}
void ends() {//Function

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("  THANKS FOR  ");
  lcd.scrollDisplayLeft();//scroll text left
  delay(2500);
  lcd.setCursor(0, 1);
  lcd.print("   VISITING ");
  lcd.scrollDisplayLeft();//scroll text left
  delay(2500);
  exit(0);

}

Does it work with a simple button test sketch?

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

When you posted your code without code tags did you receive a warning message ?

Yes it works even with simple led and button sketch.

Please post that sketch.

What is this function prototype for?

int totalAll (int buttonState);

int totalAll was for concluding the code, but my pushbutton input didnt work.

What do you mean, "didn't work"? What happens?

Are you pressing the button during one of the long delays?

1 Like

For simple led and button sketch, the led lights up when the button was pressed.
However in the coding above, before the button was pressed, the output was "Put your card to the the reader".
Supposedly, the output when the button was pressed should have ended the loop but it did not change the output.

Which you did not post as requested...

This is the sketch

At least you didn't call it a schematic...


This is the simple button led sketch that worked when I tested it.

A "sketch" is a name for an Arduino program.

When you push the button, does the beeper sound?

No, the beeper didnt make any sound

Have you tried a simple test program to test your beeper?


this is the sketch when i run the code, but when i push the button there is no input appeared.

No, that's a useless image of a sketch. Also you didn't answer my question.

yes i did, if i tap the card to the rc522 reader, the beeper works, but when the button in pressed, there is no sound.

1 Like

How long are you holding the button down?