Serial.available() only working half of the time!

Hey everyone,

I am relatively new to Arduino, and I am currently trying to create a "credit card" payment system using RFID tags. Each of the RFID tags should correspond to an indvdual"bank account", and when the chip is scanned, the user should be asked to enter the price of the purchase. The Arduino should then deduct this price from the amount of money in the bank account and Serial.print() it.

The first time you scan the chip it works fine; however, the second time you scan the card it skips the serial.avaiable() function and does not allow you to enter the price. If you scan the chip again, then it does work. I do not know what I am doing wrong!!!

My code is attached...
Thank you in advance :slight_smile:

DumpInfo.ino (3.47 KB)

DumpInfo.ino (3.47 KB)

post he code directly in the forum using code tags, can't read the .ino from a cell phone easily

(although given the name I suspect it's the standard DumpInfo from the MFRC522 library.. why would you post that in that case ??)

My guess without looking at the code: Wrong or not accounted for line ending in serial monitor. Try none.

What have you got the Line ending set to in the Serial monitor ?
If you have a line ending then does your code allow for that or is it interpreted as a second user input ?

#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
char price = 0;
char MoneyLeft = 10;
MFRC522 mfrc522(SS_PIN, RST_PIN); 
String value = "A9 A4 DE 8B";// Create MFRC522 instance
int cost = 0;
int c = 0;
int account[] = {10, 20};
String serialnumbers[] = {"A9 A4 DE 8B", "0A FA 82 16"};

void setup() {
	Serial.begin(9600);		// Initialize serial communications with the PC
	while (!Serial);		// Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
	SPI.begin();			// Init SPI bus
	mfrc522.PCD_Init();		// Init MFRC522
	mfrc522.PCD_DumpVersionToSerial();	// Show details of PCD - MFRC522 Card Reader details
	Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));
}

void loop() {
	// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.

  // 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();
 
  content.toUpperCase();
  for (c = 0; c <= 1; c++){
       if (content.substring(1) == serialnumbers[c]) //change here the UID of the card/cards that you want to give access
  {
    
       Serial.print("How much does this cost?");
       while (Serial.available() == 0){}
        int price = Serial.parseInt();
      int money = account[c];
      money -= price;
      account[c] = money;
    Serial.println();
    Serial.println(money);
    Serial.print("Successful payment");
    Serial.println();
    
    delay(500);
  }
  }
}

And the Line ending setting ?

New line

alexoort:
New line

Change it to No line ending

It works!!!!!!
Thank you so much, everyone!!! :slight_smile:

It works!!!!!!

Do you understand what the problem was ?

haha lol no... :confused:

       Serial.print("How much does this cost?");
       while (Serial.available() == 0){}

The problem is that if you have a Line ending set then the line ending character(s) are still in the input buffer when the second line above executes, hence you do not get a chance to answer the question because data is available. Having no line ending ensures that there is nothing in the input buffer so nothing is available and the program waits until an entry has been made.