Help with Serial.parseInt

So let me start by saying this is my first post here but it will certainly not be my last! I should also say that i am a student and this is for school.

I am having an issue with a program that takes a value 1-7 from the serial monitor and displays the day of the week
that corresponds to that number. We are required to use a switch statement for the days of the week and we get extra credit
for using if statements for the weekends and for invalid inputs. So this is the problem, it works great except that it always prints my error message for invalid inputs after it prints the correct output. I have rearranged the code more than once to try to stop this but its almost like the serial monitor is sending 2 values every time. Is this a coding issue? Below is my code and the output from the serial monitor.

Thanks in advance!


int d;

// setup program parameters
void setup(){
  Serial.begin(9600);    // start serial communication
  Serial.println("you will be prompted for numbers between 1 and 7");
  Serial.println("    that represents the day of the week");  // give user instructions
  Serial.println("Please enter a number between 1 and 7.");

// loop that calculates the day
void loop(){
  while (Serial.available() == 0){
  if (Serial.available() > 0){
     d = Serial.parseInt();

      if (d<1||d>7){
        Serial.println("This is not an allowable day number");
      else if (d==1||d==7){
        Serial.println("It's the weekend. Enjoy!");
      switch (d){
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:


This is not an allowable day number
This is not an allowable day number
This is not an allowable day number
This is not an allowable day number
This is not an allowable day number
It’s the weekend. Enjoy!
This is not an allowable day number

Homework item: read the "How to use this forum - please read." stickie about how to post code.

OK You're so close I'll tell you what's happening.

When the user enters a number it sends a string down the serial line PLUSa new line character

Your code will extract the number and do what it has to do.

The loop then runs again and finds that newline character still in the buffer. So it sees this as available data. Your code will then process this newline character and give the error.

Here's the bit you need to add

So where you have

d = Serial.parseInt();

you should then follow it immediately with


This will remove any dross that is still sitting in the buffer.

Now when the loop starts again, it will have a nice clean empty buffer.

or… you can choose to transmit the number without the trailing char:

note you can select what follows when you press “send”

Screen Shot 2014-10-01 at 10.09.51 PM.png

Another alternative:

  if (Serial.available() > 0) {
    d = Serial.readBytesUntil('\n', buff, 2);
    d = buff[0] - '0';

The readBytesUntil() method of the Serial object reads characters into buff until it reads a newline ('\n') or 2 characters, whichever comes first. It returns the number of characters read. Suppose the user enters the digit character 1. Then

d = buff[0] - '0'; d = '1' - '0'; d = 49 - 48; // Since a '1' in ASCII is 49 and 0 is 48 d = 1;

which is a cheap way to convert a digit character into an integer value. One more thing for you to figure out. If the user enters a 0, you still execute the switch/case block, which you should not do. How can you fix that?

Awesome! Thanks for the help!