Hi, this is a training excersise and I have followed it to the letter(I think) according to the instruction video, but I can't get line 31 to work at all. Can anyone see what I"m missing
Lesson_6.ino (2.33 KB)
Hi, this is a training excersise and I have followed it to the letter(I think) according to the instruction video, but I can't get line 31 to work at all. Can anyone see what I"m missing
Lesson_6.ino (2.33 KB)
Please put the code in the post; can't open .ino files on mobiles.
Please use code tags. Reading some of the locked posts at the top of this forum would help also.
Your code works for me... both leds blink for the numbers I type in.
fishboneDiagram:
Your code works for me... both leds blink for the numbers I type in.
The input for the yellow led doesn't wait for a response. the program continues and the yellow led doesn't light
Serial.println("How many times do you want the redlight blink?");//prompt user for input
while (Serial.available() == 0) {}//wait for input
numRedBlinks = Serial.parseInt();//read input
Your code :
int redLEDPin = 9; //declare redLEDPin as int and set it to 9
int yellowLEDPin = 10; //declares yellowLEDPin as integer and set it to 10
int redOnTime = 100; //this is red ontime
int redOffTime = 900; //this is red offtime
int yellowOnTime = 900; //this is yellow on time
int yellowOffTime = 100; //this is yellow off time
int numRedBlinks ;//number times red light blinks
int numYellowBlinks ;//number times yellow light blinks
String redMessage = "The red light is blinking"; //declaring a string variable
String yellowMessage = "The yellow light is blinking"; //declaring a string variable
void setup() {
Serial.begin(9600);//baud rate
String wm1 = "Welcome to "; //declare string variable and assign a value
String wm2 = "my program"; //declare string variable and assign a value
String wm3;//declare variable no value
wm3 = wm1 + wm2; //concatonating wm1 and wm2 into wm3
Serial.println (wm3);
delay (3000);//wait
pinMode(redLEDPin, OUTPUT);
pinMode(yellowLEDPin, OUTPUT);
Serial.print("");//leave a line
}
void loop() {
Serial.println("How many times do you want the redlight blink?");//prompt user for input
while (Serial.available() == 0) {}//wait for input
numRedBlinks = Serial.parseInt();//read input
Serial.println("How many times do you want the yellow light to blink?");//prompt user for input
while (Serial.available() == 0) {} //wait for user input
numYellowBlinks = Serial.parseInt(); //read input
Serial.println(redMessage );//says the red light is blinking
for (int j = 1; j <= numRedBlinks; j = j + 1) {//counter
Serial.print(" You are on blink #: ");
Serial.println(j);//prints number
digitalWrite(redLEDPin, HIGH); //turns red on
delay (redOnTime);//wait
digitalWrite (redLEDPin, LOW); //turns red off
delay (redOffTime);//wait
}
Serial.println(" ");//prints blank line
Serial.println(yellowMessage);//says the yellow light is blinking
for (int j = 1 ; j <= numYellowBlinks; j = j + 1) { //counter
Serial.print (" You are on blink#: ");//prints
Serial.println(j);//prints number
digitalWrite(yellowLEDPin, HIGH);//turns yellow light on
delay(yellowOnTime);//wait
digitalWrite (yellowLEDPin, LOW);//turns yellow light off
delay(yellowOffTime);//wait
}
Serial.println(" ");//prints blank line
}
Regards,
bidouilleelec
Hello OrionSolotar
OrionSolotar:
while (Serial.available() == 0) {}//wait for input
Why do you say that it's not working ?
What do you see abnormal?
Have you set the serial monitor to "No Line Ending" ( beside the baud rate) ?
Regards,
bidouilleelec
Configure serial monitor so it does not send carriage return and/or line feed (right bottom, I think it's called line ending)'
Does that solve the problem?
PS
Try to stay away from String in favour if nul-terminated character arrays. Ir does nit really matter in your case but with complicated code and plenty of String concatenation, you will eventually run into unexplainable run-time issues.
Hello sterretje
sterretje:
Configure serial monitor so it does not send carriage return and/or line feed (right bottom, I think it's called line ending)'Does that solve the problem?
Yes, it does.
Regards,
bidouilleelec
Sorry @bidouilleelec
The question was to OP; our posts crossed.
The line ending was the problem. I have spent 12 hours rewriting this and now its fixed. Thank you all
Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.
...R
the line ending is not really the problem as it would not create an issue with Serial.parseInt();/
and actually it does allow this call to return faster without waiting for the timeout.
The real problem though is that with line ending, there would be still input in the Serial buffer (the '\n' and possibly '\r' if you had both selected) after you read the int and thus at the next attempt you would try to read an int that might not be there yet.
--> this is symptomatic of poorly designed Serial parsing that is trying to second guess the timing of an asynchronous protocol.
I would suggest to study Serial Input Basics to handle this in the right way
EDIT: Robin was faster
Regardless of the setting option of the 'line ending tab' of the Serial Monitor (Fig-1), the OP wants that his sketch should behave as in Fig-2; unfortunately, it does not follow Fig-2.
Figure-1:
When Serial.parseInt() is evaluated with 'line ending tab' at 'Newline' position, the character 0x0A (ASCII code of Newline character) still remains in the serial FIFO buffer which must be taken out by executing this code: (void)Serial.read() just after the Serial.parseInt() code (2 times) so that program control remains within this loop: while(Serial.available() == 0){} until a character arrives from the InputBox of the Serial Monitor. If 'No line ending' option is selected, (void)Serial.read() is a dummy execution.
void loop() {
Serial.println("How many times do you want the redlight blink?");//prompt user for input
while (Serial.available() == 0) {}//wait for input
numRedBlinks = Serial.parseInt();//read input
(void)Serial.read();
Serial.println("How many times do you want the yellow light to blink?");//prompt user for input
while (Serial.available() == 0) {} //wait for user input
numYellowBlinks = Serial.parseInt(); //read input
(void)Serial.read();
........................
}
(void) is harmless but unnecessary.
But, Serial.read() method returns a value which I want to discard -- how to do it without saying (void)?
vaj4088:
(void) is harmless but unnecessary.
it does actually have a role in the general case and could be considered a best practice (although often ignored).
Depending on how sensitive your compiler is (warning levels), you could get a warning about ignoring the returned value of read()
. this (void)
signifies your intent to the compiler. (some functions could be compiled using the
__attribute__((warn_unused_result))
which then would get the warning triggered) and since C++17 you can use the [nobbc][[nodiscard]][/nobbc]
attribute..
for exemple if you try to compile
[[nodiscard]] int myFunction() {
return 42;
}
void setup()
{
Serial.begin(115200);
myFunction(); // compiler will not be happy and raise a warning
}
void loop() {}
and have warnings on, you'll see
.../sketch_feb09a.ino: In function 'void setup()':
.../sketch_feb09a.ino:8:13: warning: ignoring return value of 'int myFunction()', declared with attribute nodiscard [-Wunused-result]
myFunction();
~~~~~~~~~~^~
.../sketch_feb09a.ino:1:19: note: declared here
[[nodiscard]] int myFunction() {
^~~~~~~~~~
but if you try to compile using (void)
[[nodiscard]] int myFunction() {
return 42;
}
void setup()
{
Serial.begin(115200);
(void) myFunction(); // compiler will be happy
}
void loop() {}
the compiler will not bark at you.
GolamMostafa:
(void)Serial.read() is a dummy execution.
Not sure why you say dummy. It does do something, it tries to remove on byte from the Serial buffer if there is one... this code is not fool proof and using Robin's tutorial is a solid way of handling this correctly.
GolamMostafa:
But, Serial.read() method returns a value which I want to discard -- how to do it without saying (void)?
so seems you did not do it for the right reason
You don't need to do anything, the compiler will discard it for you
Whatever its type, it gets discarded if there is nothing such as
x =
. Casting to void changes the type but has nothing to do with the discard.
J-M-L:
Not sure why you say dummy.
If 'No line ending' option is selected, there is no non-digit (assume only 5 has been sent from the InputBox of Serial Monitor) being transmitted from the Serial Monitor; the Serial.parseInt() method is terminated due to timeout. The parseInt() method has taken out 5 from the buffer, and there is no more character left in the buffer; but, the (void)Serial.read() is still executed -- what is it going to take out from the buffer? Nothing and this is the reason for calling it a dummy execution.