Help working out this bug??

Hi Guys! So i’ve made this program to interface my Arduino with an RFID reader and Serial LCD display. The point is to start the timer when an RFID chip is read through the reader. The program is pretty much done, however there is a final bug which I cannot seem to iron out and I really can’t figure out what it is! Hopefully someone here can help me work this out.
Here is the code:

int done = 5;
int Min = 0;
int ledPin = 8;
void setup() {
Serial.begin(9600);
}
void loop() {
RFIDread();
mainScreen();
}
void RFIDread(){
  if(Serial.available() > 0){
    clearScreen();
    Timer();
  }
}
void Timer(){
  for(int sec = 59; sec > -1 && Min > -1; sec--){
  if(Min < 10){
    Serial.print(0);
    Serial.print(Min);
    Serial.print(":");
     } else Serial.print(Min);
   if(sec < 10){ 
    Serial.print(0);
    Serial.print(sec);
    }else{
      Serial.print(sec);
    }
  delay(100);
  if(sec == 0) {
    sec = 59;
    Min--;
    }
   clearScreen();
  }
  done = 5;
 }
 void selectLineOne(){  //puts the cursor at line 0 char 0.
   Serial.print(0xFE, BYTE);   //command flag
   Serial.print(128, BYTE);    //position
 }
 void selectLineTwo(){  //puts the cursor at line 0 char 0.
   Serial.print(0xFE, BYTE);   //command flag
   Serial.print(192, BYTE);    //position
 }
 void clearScreen(){
     Serial.print(0xFE, BYTE);   //command flag
     Serial.print(0x01, BYTE);   //clear command.
 }
 void mainScreen(){
  while(done == 5){
  clearScreen();
  Serial.print("Start the Timer!");
  selectLineTwo();
  Serial.print("Start it now!!!");
  selectLineOne();
  done = 0;
  }
 }

The problem is that after running through the Timer segment of the code once it returns to the mainScreen segment and seems to be stuck in an infinite loop of displaying the text on the LCD. It seems to run through the segment of code infinitely:

  while(done == 5){
  clearScreen();
  Serial.print("Start the Timer!");
  selectLineTwo();
  Serial.print("Start it now!!!");
  selectLineOne();
  done = 0;

Does anyone know why it would do this? and only on the second run through? Thanks for any help!

You check Serial.available, but never do a Serial.read. So that line is always going to be true once there is anything in the serial buffer. Which means clearScreen() always gets called and that means done will be set to 5, causing your "infinite loop."

So in fact, pretty much all of your code is being executed, not just the lines you suspected.

Why are you looping in that second section of code? Nothing apart form the final line changes the loop condition, so I don't get why you're looping in the first place? How do you know it's stuck in that loop? Have you tried some Serial.println()'s to see where the program is up to?

Cheers,

Heh, yes - what James said!! :-)

But I still don't know why you're looping in the mainScreen function...

Sorry maybe this is my NOOBness talking!! Hahaha. Ok so if I did something like :

void RFIDread(){
  if(Serial.available() > 0){
    val = Serial.read();
    if(val > 0){
    clearScreen();
    Timer();
    }
  }

, Then in the timer section reset val to 0? It seems a bit clumsy is there a better way of achieving this? Basically I just want it to check the Serial in a new unit of time after every cycle.

Also, again I am utterly new at this, how else can the mainScreen segment be set up other than a loop? How can I make it run through once and quit printing over and over?

if are you just going to throw the contents away, then use Serial.flush().

I wouldn't check if you received the value "0", that is probably a valid serial character.

Great. Thank you so much, everything works now!! :slight_smile: :slight_smile: :slight_smile:

As thanks (and courtesy to others), why not remove the unnecessary (and potentially offensive) language from your last post?