Stuck in IF STATEMENTS

Hi! I'm stuck in my RFID program. It doesn't move on when I pressed the setButton after I incremented/decremented the addbal variable using also 2 if statements. I am not pro in handling Arduino UNO. What am I doing wrong?

//LCD
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN 9           // Configurable, see typical pin layout above
#define SS_PIN 10          // Configurable, see typical pin layout above

MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance

//LCD library 
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

int decrementButton = 2;
int incrementButton = 3;
int setButton = 4;

int decButtonState = 0;
int incButtonState = 0;
int setButtonState = 0;

int addbal = 0; 
int prestate = 0;
void setup() 
{
  //LCD
  lcd.begin(16,2); //16x2
  lcd.clear();
  
  Serial.begin(9600);        // Initialize serial communications with the PC
  SPI.begin();               // Init SPI bus
  mfrc522.PCD_Init();        // Init MFRC522 card
  Serial.println(F("Beep Card Rebalance"));
  lcd.clear();
  lcd.print("Beep Card Reload");
  lcd.setCursor(0,1);
  lcd.print("Input balance...");
  delay(2000);
  pinMode(decrementButton, INPUT);
  pinMode(incrementButton, INPUT);
  pinMode(setButton, INPUT);
}

void loop() 
{
  decButtonState = digitalRead(decrementButton);
  incButtonState = digitalRead(incrementButton);
  setButtonState = digitalRead(setButton);
  lcd.clear();
  lcd.print("Input balance:  ");
  
  if(incButtonState == HIGH && prestate == 0)
  {
     addbal = addbal + 10;
     lcd.setCursor(0,1);
     lcd.print("> ");
     lcd.print(addbal);
     lcd.print("   ");
     delay(100);

     prestate = 1;
  }
  else if(decButtonState == HIGH && prestate == 0)
  {
    if(addbal <= 0)
    {
      addbal = 0;  
    }
    addbal = addbal - 10;
    lcd.setCursor(0,1);
    lcd.print("> ");
    lcd.print(addbal);
    lcd.print(" ");
    prestate = 1;
    delay(100);
  }
  else if(incButtonState == LOW && decButtonState == LOW) 
  {
    prestate = 0;
  }
  
  else if(setButtonState == HIGH)
  {
    byte block;
    MFRC522::StatusCode status;
    byte len;
    
    // Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
    MFRC522::MIFARE_Key key;
    for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF;
  
    // Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
    if ( ! mfrc522.PICC_IsNewCardPresent()) 
    {
      return;
    }
  
    // Select one of the cards
    if ( ! mfrc522.PICC_ReadCardSerial()) 
    {
      return;
    }
  
    Serial.println(F("**Card Detected:**"));
    byte buffer1[18];
    block = 4;
    len = 18;
  
    //--- GET BALANCE ---//
    status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, 4, &key, &(mfrc522.uid)); //line 834 of MFRC522.cpp file
    if (status != MFRC522::STATUS_OK) 
    {
      Serial.print(F("Authentication failed: "));
      Serial.println(mfrc522.GetStatusCodeName(status));
      return;
    }
  
    status = mfrc522.MIFARE_Read(block, buffer1, &len);
    if (status != MFRC522::STATUS_OK) 
    {
      Serial.print(F("Reading failed: "));
      Serial.println(mfrc522.GetStatusCodeName(status));
      return;
    }
  
    //---PRINT BALANCE---//
    String balance="";
    for (uint8_t i = 0; i < 16; i++)
    {
      if (buffer1[i] != 32)
      {
          balance+=(char)buffer1[i];
      }
    }
  
     //--- checking and deducting balance ---//
    int balanceInt=balance.toInt();
    Serial.print("Balance: ");
    Serial.println(balanceInt);
  
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    ////////////////////////////////////////////////ADDED//////////////////////////
    byte buffer[34];
    byte bal;
    bal = Serial.readBytesUntil('#', (char *) buffer, 20) ; // read first name from serial
    for (byte i = bal; i < 20; i++) buffer[i] = ' ';     // pad with spaces
  
    
    //---PRINT BALANCE---//
    String addedBalance="";
    for (uint8_t i = 0; i < 16; i++)
    {
      if (buffer1[i] != 32)
      {
          addedBalance+=(char)buffer[i];
      }
    }
    addbal = addedBalance.toInt();
    balanceInt = balanceInt + addbal;
    byte buffer3[18];
    for (int i=0;i<balance.length();i++)
    {
      buffer3[i]=String(balanceInt)[i];
    }
    /////////////////////////////////////////////////////////////////////////////
    block = 4;
    //Serial.println(F("Authenticating using key A..."));
    status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
    if (status != MFRC522::STATUS_OK) 
    {
      Serial.print(F("PCD_Authenticate() failed: "));
      Serial.println(mfrc522.GetStatusCodeName(status));
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Put back card..");
      delay(1000);
      lcd.clear();
      return;
    }
  
    // Write block
    status = mfrc522.MIFARE_Write(block, buffer3, 16);
    if (status != MFRC522::STATUS_OK) 
    {
      Serial.print(F("MIFARE_Write() failed: "));
      Serial.println(mfrc522.GetStatusCodeName(status));
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Put back card..");
      delay(1000);
      lcd.clear();
      return;
    }
    else Serial.println(F("MIFARE_Write() success: "));
  
    block = 5;
    //Serial.println(F("Authenticating using key A..."));
    status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
    if (status != MFRC522::STATUS_OK) 
    {
      Serial.print(F("PCD_Authenticate() failed: "));
      Serial.println(mfrc522.GetStatusCodeName(status));
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Put back card..");
      delay(1000);
      lcd.clear();
      return;
    }
  
    // Write block
    status = mfrc522.MIFARE_Write(block, &buffer3[16], 16);
    if (status != MFRC522::STATUS_OK) 
    {
      Serial.print(F("MIFARE_Write() failed: "));
      Serial.println(mfrc522.GetStatusCodeName(status));
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Put back card..");
      delay(1000);
      lcd.clear();
      return;
    }
    else Serial.println(F("MIFARE_Write() success: "));
    lcd.clear();
    lcd.print("Transaction done!");
    delay(1000);
    lcd.clear();
    lcd.print("Balance:Php");
    lcd.print(balanceInt);
    lcd.clear();
    lcd.print("Please remove");
    delay(2000);
    lcd.clear();
    
    Serial.println(" ");
    mfrc522.PICC_HaltA(); // Halt PICC
    mfrc522.PCD_StopCrypto1();  // Stop encryption on PCD
  }

  delay(100);
}

Your post was MOVED to its current location as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

The obvious question is how are the buttons wired ?

Any pulldown resistors in place to keep the input at a LOW state when buttons are not pressed ?

the logic of the code is difficult to verify because code to detect button presses using prestate as well as the actions for each buttons are intertwined

conventional use of buttons connects then between the pin and ground and configures the pins as INPUT_PULLUP

consider how the following is organized

// check multiple buttons and toggle LEDs

enum { Off = HIGH, On = LOW };

byte pinsLed [] = { 10, 11, 12 };
byte pinsBut [] = { A1, A2, A3 };
#define N_BUT   sizeof(pinsBut)

byte butState [N_BUT];

// -----------------------------------------------------------------------------
int
chkButtons ()
{
    for (unsigned n = 0; n < sizeof(pinsBut); n++)  {
        byte but = digitalRead (pinsBut [n]);

        if (butState [n] != but)  {
            butState [n] = but;

            delay (10);     // debounce

            if (On == but)
                return n;
        }
    }
    return -1;
}

// -----------------------------------------------------------------------------
void
loop ()
{
    switch (chkButtons ())  {
    case 2:
        digitalWrite (pinsLed [2], ! digitalRead (pinsLed [2]));
        break;

    case 1:
        digitalWrite (pinsLed [1], ! digitalRead (pinsLed [1]));
        break;

    case 0:
        digitalWrite (pinsLed [0], ! digitalRead (pinsLed [0]));
        break;
    }
}

// -----------------------------------------------------------------------------
void
setup ()
{
    Serial.begin (9600);

    for (unsigned n = 0; n < sizeof(pinsBut); n++)  {
        pinMode (pinsBut [n], INPUT_PULLUP);
        butState [n] = digitalRead (pinsBut [n]);
    }

    for (unsigned n = 0; n < sizeof(pinsLed); n++)  {
        digitalWrite (pinsLed [n], Off);
        pinMode      (pinsLed [n], OUTPUT);
    }
}

Figured it out. Tweaked some codes inside the if setButton statement and it worked magically idk why lol. Thank you all

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