Hey , I'm trying to create a program to blink an LED by reading a number from the Serial screen.
The LED does blink the amount of time given by the user but afterwards it resets the int definition of the number of blinks and prints it one more time, I would like to cancek that, yet I don't know how.
Thanks in advance for the helpers
The code:
int blinks;
int j;
String msg1 = ("How many blinks?");
String msg2 = ("The LED will blink ");
int countdown;
int countdownDelay = 1000;
String msg3 = (" times in... ");
int blinkDelay = 300;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode (2,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println (msg1);
while (Serial.available () == 0 ){
@amito55 1. Your sketch is alright (tested) except that you need to put the keyword int before blinks identifier and respect the remark made in Post#2.
2. Look at the remark, shown above, made in Post#2 by @anon73444976.
You should select "No line ending" option for the "Line ending tab" of Serial Monitor (Fig-1). As a result, your sketch will give result as you have wanted (Fig-2).
3. If you choose any other option other than "No line ending", then the following events occur and you see an unexpected line on the Serial Monitor. (1) Assume you want 5 blinks; you enter 5 in the InputBox with "Newline" option and then click on the Send button. As a result 0x35 (ASCII code of digit 5) and 0x0A (ASCII Code of Newline) travel from Serial Monitor to UNO.
(2) The Serial.parseInt() function extracts 5 from 0x35 and stops parsing; because, the next one is a non-digit charcater. The code 0x0A for the "Newline" charcater remains in the Serial Buffer of UNO.
(3) After blinking the LED for 5 times, the program excutes the following codes and sees a charcater (0x0A) in the buffer which when parsed gives 0. That's why you observe 0 in the SerialMonitor.
while (Serial.available () == 0 )
{
}
(4) If you choose "No line ending" option, then no code is transmitted after 0x35; as a result, your sketch gives result as expected.