Issue with Serial - Arduino just skips when I'm supposed to input

Hello, everyone. Sorry if there is a topic with a similar issue already answered, but I couldn’t find it.
So what happens is I have this code I just wrote which requires me to input some numbers. the Arduino allows me to input the first one but when it asks me to type the second one, it just skips the command and resumes to calculate the rest of the operations. I had the exact same issue with another code that I wrote that allowed me to control an LED via Serial Monitor as well. I also experimented with my ESP32 and had the same issue, which got me surprised.

The worst part is that it works just fine on Tinkercad. I thought it could be the Arduino board that I’m using, but for the same problem to happen on both my Arduino and ESP32 seems very unlikely.

Don’t worry about the Math, the issue will happen before you even reach it. If anyone could test this code with their own Arduino and tell me what happens, or even better answer me why this problem even exists, would be a huge help!

Thank you!

The code is as follows:

int year, thousand, hundred, ten, unit;
int lastDigitsID;
int dangerIndicator;

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println("Input the last two digits of your ID (use 16 as an example)");
  while(Serial.available()==0);
  lastDigitsID=Serial.parseInt(); 

  /*the problem is on this next part, the Serial Monitor will not allow me to write anything,
  it will just skip and resume to calculate the rest*/
  
  Serial.println("Input the year you wish to consult: ");
  while(Serial.available()==0);
  year=Serial.parseInt();
  
  thousand=year/1000;

  hundred=year%1000;
  hundred=thousand/100;

  ten=year%1000;
  ten=ten%100;
  ten=ten/10;
  
  unit=year%1000;
  unit=unit%100;
  unit=unit%10;

  
  if (year<2001){

    thousand=sqrt(thousand);
    hundred=sqrt(hundred);
    ten=sqrt(ten);
    unit=sqrt(unit);

    dangerIndicator=thousand-hundred-ten-unit-lastDigitsID;

     Serial.print("The danger indicator for the year " );
     Serial.println(year);
     Serial.print("regarding the ID ending in " );
     Serial.print(lastDigitsID);
     Serial.print(" equals to: ");
     Serial.println(dangerIndicator);
     
  }else{
    
     dangerIndicator=pow(thousand,3)+pow(hundred,3)+pow(ten,3)+pow(unit,3)+lastDigitsID;
     Serial.print("The danger indicator for the year " );
     Serial.println(year);
     Serial.print("regarding the ID ending in " );
     Serial.print(lastDigitsID);
     Serial.print(" equals to: ");
     Serial.println(dangerIndicator);
   }
}

My bet is that you have the Line Ending in the Serial monitor set to something other than “No line ending” so that after entry a Carriage Return, Line Feed or both are added to your entry

1 Like

I cannot believe it’s just that simple. Thank you very much! That did it!

I am glad it worked.
It is quite a common problem

1 Like

Check out Arduino Software Solutions for a variety of sketches to read inputs from Serial with their pros and cons