Paul McWhorter lesson 18 issue serial com double print

I'm using this code from the Paul McWhorter lessons number 18 and i've followed his code minus my own variable names and it seems to double print after the first input, however his in the video doesn't. I've checked and double checked the code syntax and i cannot get it to stop double printing after the first input it's strange. I need to get this resolved for later projects and figure out why my arduino is not functioning as Paul McWhorter arduino. any help is appreciated!

Video link to view issue:

Here is the Code from the lesson:
int number;//assign "random1" for variable value later
String question="How Many Blink Times?";

int j;//loop counter
int bt=500;//blink time
int Rled=6;//assign "Rled" to pin D6

void setup()
{
Serial.begin(115200);//intialize serial com at 115200 baud
pinMode(Rled,OUTPUT);//assign "Rled" D6 pin as an OUTPUT
}

void loop() //Ask first, wait, then read to get data from serial com
{
Serial.println(question);//Ask for input from user/sensor

while(Serial.available()==0)//WHILE serial.avail do nothing
{

}

number=Serial.parseInt();//now "number" is equal to the serial INPUT line

for(j=0;j<number;j++)
{
digitalWrite(Rled,HIGH);//turn on the led
delay(bt);//delay between blinks
digitalWrite(Rled,LOW);//turn off the led
delay(bt);//delay between blinks
}
}

If you will look at the documentation for Serial.available(), I think you will find the correct way to know if there is no data in the buffer to be read.

that still doesn't answer why my unit isn't doing what his is so it doesn't solve my issue by even .00001%

AGAIN THE ISSUE IS WHY FOLLOWING HIS EXACT CODE DID NOT REPRODUCE THE SAME RESULTS HE SHOWED? since my first post wasn't clear enough i'm guessing... rewriting it as this solved all my issues, BUT WHY DID I HAVE TO WRITE IT DIFFERENT THAN WHAT I WAS SHOWN ON THE LESSON? Shouldn't his code of worked no issues on my unit?

int number;//assign "random1" for variable value later
String question="How Many Blink Times?";

int j;//loop counter
int bt=500;//blink time
int Rled=6;//assign "Rled" to pin D6

void setup()
{
Serial.begin(115200);//intialize serial com at 115200baud
pinMode(Rled,OUTPUT);//assing "Rled" D6 pin as an OUTPUT
Serial.println(question);
}

void loop() //Ask first, wait, then read to get data from serial com
{
while(Serial.available())
{
Serial.println(question);//Ask for input from user/sensor
break;
}

while(Serial.available()==0)//WHILE serial.avail do nothing and wait
{

}

number=Serial.parseInt();//now "number" is equal to the serial INPUT line

for(j=0;j<number;j++)
{
digitalWrite(Rled,HIGH);//turn on the led
delay(bt);//delay between blinks
digitalWrite(Rled,LOW);//turn off the led
delay(bt);//delay between blinks
}
}

Then his exact code, as published, must have mistakes. Certainly not the first code to be published with errors. You will have to insert debug Serial,print() statements to identify, step by step exactly what the logic is and find the error.

HE USED THIS CODE NO ISSUES.... have you even seen paul mcwhorter vids? he shows his published codes functioning on camera while zoomed in on the code showing letter for letter each line so you can follow along........

HIS VIDEO SHOWS HIS CODE WORKING PERFECTLY!
HIS EXACT CODE ON MY SYSTEM PRODUCES A DOUBLE PRINT!
THERE ARE NO ERRORS IN HIS CODE IF IT'S WORKING IN HIS VIDEO! I'VE COPIED IT LETTER FOR LETTER IN THE VIDEO! HE LITTERALLY ZOOMS IN ON THE CODE FOR YOU TO COPY IT ALONG WITH HIM.............. THEN SHOWS SAID CODE FUNCTIONING IN THE SAME VIDEO IN VIDEO........

Arduino Tutorial 18: Reading Numbers from the Serial Monitor | Technology Tutorials (toptechboy.com)

when i use his exact code letter for letter i have a double print. WHY? IT has nothing to do with my serial.available if my Rewritten code functioned using it........... I'm making sure i don't have a bug in my atmel328 pic. Why is it printing : Serial.println(question) at the beginning twice?

I'm not a master programmer or anything but i've played with c++ for a few years before compilers could be downloaded onto phones and MSDOS and shell systems were "the up and coming thing". I'm now getting back into it

also if i try it as:
while(Serial.available())//WHILE serial.avail do nothing and wait

it enters in 1 count command and then offers no more input commands

Just attempted it as

if(Serial.available()>0)
{
number=Serial.read();
}

as per the serial.available syntax page and now it just infinitely loops the question without waiting for an input........

So this is the same lesson 18 Paul McWhorter video code and it double prints also on my unit but not on his:

//BASIC SERIAL COM INPUT COMMANDS
int number;//assign "number" for variable value later
String msg1="What number do you pick?";
String msg2="Your Number Is: ";

void setup()
{
Serial.begin(9600);//intialize serial com at 9600 baud
}

void loop() //Ask first, wait, then read to get data from serial com
{
//THIS PRINTS THE "msg1" String 1 time each loop then breaks;
Serial.println(msg1);//Ask for input from user/sensor

while(Serial.available()==0)//WHILE serial.avail do nothing
{

}

number=Serial.parseInt();//now "number" is equal to the serial INPUT line

Serial.print(msg2);
Serial.println(number);

}

referencing post #7 ^^^^ if i rewrite the same code as this then it prints perfectly with no double printing: again i'm just trying to understand why his codes are not working on my unit when i'm plain as day copying his code letter for letter

//BASIC SERIAL COM INPUT COMMANDS
int number;//assign "number" for variable value later
String msg1="What number do you pick?";
String msg2="Your Number Is: ";

void setup()
{
Serial.begin(9600);//intialize serial com at 9600 baud
Serial.println(msg1);//print out initial "string" aka msg1
}

void loop() //Ask first, wait, then read to get data from serial com
{
//THIS PRINTS THE "msg1" String 1 time each loop then breaks;
while(Serial.available())//start a loop to print in "question" string
{
Serial.println(msg1);//Ask for input from user/sensor
break;
}
Serial.setTimeout(100000000);

while(Serial.available()==0)//WHILE serial.avail do nothing
{

}

number=Serial.parseInt();//now "number" is equal to the serial INPUT line

Serial.print(msg2);
Serial.println(number);

}

Does this McWhorter bloke use the serial monitor?
(No, I'm not going to sit through another show-and-tell video)
What is the line ending he uses?

Please remember to use code tags when posting code.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.