After input loop does not continue

Hi to all I'm new to all this and so please forgive stupid questions.

I have a application to record time in and time out for emplyees. I have and rfid sd card to save data to and a rtc for time plus two buttons on IN one OUT.

I'm still working on the code but in general its working I can read the card data and time and save them to the SD. But because I do not know, for now, how to write a retrive data from the cards and calculate the time I am savng the time with a IN or OUT at the end and then do the math in excel.

My problem is I have three steps,
readRFID();
logCard();
verifyCheckIn();

verify check in i may take out seeing i do not know if someone is checking in or out. (that part was for cjheckin time)
So I thought about using a button one for In the other for Out.
This is where I am stuck after looking fo days I'm still not sure how to do it. In the code below once it ask for check in or out even if i press the button it does not continue the loop.
I still would like to and a display but still need to get this part to work frist.
I was thinking instaed of two function to add IN or OUT I could use a variable to reduce lines coding.
Thanks for any help.

#include <Adafruit_GFX.h>
#include <gfxfont.h>
#include <Adafruit_SSD1306.h>
#include <splash.h>
#include <MFRC522.h> // for the RFID
#include <SPI.h> // for the RFID and SD card module
#include <SD.h> // for the SD card
#include <RTClib.h> // for the RTC
#include <Wire.h>


// set button pin numbers:
const int buttonIN = A1;     // the number of the pushbutton pin
const int buttonOUT = A2;     // the number of the pushbutton pin

//variables will change
int buttonINState = 0;
int buttonOUTState = 0 ;

// define pins for RFID
#define CS_RFID 10
#define RST_RFID 9
// define select pin for SD card module
#define CS_SD 2

// Create a file to store the data
File myFile;

// Instance of the class for RFID
MFRC522 rfid(CS_RFID, RST_RFID); 

// Instance of the class for RTC
RTC_DS3231 rtc;

// Define check in time
const int checkInHour = 9;
const int checkInMinute = 00;


// Variable to hold the tag's UID
String uidString;

//Variable to hold user check in
int userCheckInHour;
int userCheckInMinute;

// Pins for LEDs and buzzer
const int redLED = 6;
const int greenLED = 7;
const int buzzer = 3;

void setup() {

  // initialize the pushbutton pin as an input:
  pinMode(buttonIN, INPUT); 
  pinMode(buttonOUT, INPUT); 

  // Set LEDs and buzzer as outputs
  pinMode(redLED, OUTPUT);  
  pinMode(greenLED, OUTPUT);
  pinMode(buzzer, OUTPUT);
  
  // Init Serial port
  Serial.begin(9600);
  while(!Serial); 
  
  // Init SPI bus
  SPI.begin(); 
  
  // Init MFRC522 
  rfid.PCD_Init(); 

  // Setup for the SD card
  Serial.print("Initializing SD card...");
  if(!SD.begin(CS_SD)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  // Setup for the RTC  
  if(!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while(1);
  }
  else {
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }

}

void loop() {  

  //read the state of the buttons value:
  buttonINState = digitalRead(buttonIN);
  buttonOUTState = digitalRead(buttonOUT);
  
  //look for new cards
  if(rfid.PICC_IsNewCardPresent()) {
    readRFID();
    logCard();
    verifyCheckIn();
  }
  delay(10);
}

void readRFID() {

  rfid.PICC_ReadCardSerial();
  Serial.println("Tag UID: ");
  Serial.println("Premere bottone IN per ingresso");
  Serial.println ("Premere bottono OUT per uscita");
    
  //check if the button is pressed
  while (digitalRead(buttonINState) == HIGH){}
 
  if (buttonINState == LOW){  
  uidString = String(rfid.uid.uidByte[0]) + " " + String(rfid.uid.uidByte[1]) + " " + 
  String(rfid.uid.uidByte[2]) + " " + String(rfid.uid.uidByte[3]) + " IN";
  Serial.println(uidString);
  }
  
  //check if the second button is pressed
  else if (buttonOUTState == LOW){  
  uidString = String(rfid.uid.uidByte[0]) + " " + String(rfid.uid.uidByte[1]) + " " + 
  String(rfid.uid.uidByte[2]) + " " + String(rfid.uid.uidByte[3]) + " OUT";
  Serial.println(uidString);
  }
  
  // Sound the buzzer when a card is read
  tone(buzzer, 2000); 
  delay(100);        
  noTone(buzzer);
  
  delay(100);
}

void logCard() {
  // Enables SD card chip select pin
  digitalWrite(CS_SD,LOW);
  
  // Open file
  myFile=SD.open("DATA.txt", FILE_WRITE);

  // If the file opened ok, write to it
  if (myFile) {
    Serial.println("File opened ok");
    myFile.print(uidString);
    myFile.print(", ");   
    
    // Save time on SD card
    DateTime now = rtc.now();
    myFile.print(now.year(), DEC);
    myFile.print('/');
    myFile.print(now.month(), DEC);
    myFile.print('/');
    myFile.print(now.day(), DEC);
    myFile.print(',');
    myFile.print(now.hour(), DEC);
    myFile.print(':');
    myFile.println(now.minute(), DEC);
    
    // Print time on Serial monitor
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.println(now.minute(), DEC);
    Serial.println("sucessfully written on SD card");
    myFile.close();

    // Save check in time;
    userCheckInHour = now.hour();
    userCheckInMinute = now.minute();
  }
  else {
    Serial.println("error opening data.txt");  
  }
  // Disables SD card chip select pin  
  digitalWrite(CS_SD,HIGH);
}

void verifyCheckIn(){
  if((userCheckInHour < checkInHour)||((userCheckInHour==checkInHour) && (userCheckInMinute <= checkInMinute))){
    digitalWrite(greenLED, HIGH);
    delay(2000);
    digitalWrite(greenLED,LOW);
    Serial.println("You're welcome!");
  }
  else{
    digitalWrite(redLED, HIGH);
    delay(2000);
    digitalWrite(redLED,LOW);
    Serial.println("You are late...");
  }
}
while (digitalRead(buttonINState) == HIGH){}

I don't think you mean this.

Steve

Yes, this is more likely to work as intended:

buttonInState = digitalRead(buttonIn);
while (buttonInState == HIGH){
// wait for it to go low
buttonInState = digitalRead(buttonIn);
}
// It's low, proceed
  //if (buttonINState == LOW) 
  uidString = String(rfid.uid.uidByte[0]) + " " + String(rfid.uid.uidByte[1]) + " " + 
  String(rfid.uid.uidByte[2]) + " " + String(rfid.uid.uidByte[3]) + " IN";
  Serial.println(uidString);
  //}

I don't think if there is needed, you'll sit in the while() until it goes low

Probably want to enable the internal pullup resistors here as well:

  // initialize the pushbutton pin as an input:
  pinMode(buttonIN, INPUT); 
  pinMode(buttonOUT, INPUT);

Thanks for the replys. I am learning from tutorials so maybe didn't understand the while very well.

CrossRoads I do have the in viod setup is that OK?

// initialize the pushbutton pin as an input:
pinMode(buttonIN, INPUT);
pinMode(buttonOUT, INPUT);

CrossRoads when i use yours i get a 'buttonInState' was not declared in this scope errore.
It wa declared at the beggining so what am i doing wrong?

You declared

int buttonINState = 0;

Your error is about "buttonInState"

Check the spelling.

I did find that after I wrote the post. Just to remind me best to keep all lower case.

Synapsis:
best to keep all lower case.

Well yes I suppose that's one way of preventing errors, but long lowercasethingsareverydifficulttoread, comparedToThisWhichIsMuchEasier.

:slight_smile: Ture