Not sure what the problem is

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 :slight_smile:
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 ){

}
blinks = Serial.parseInt() ;
Serial.print (msg2);
Serial.print (blinks);
Serial.print (msg3);
for (countdown = 3; countdown>0; countdown-=1){
Serial.print (countdown);
delay(countdownDelay);

}
for (j=1;j<=blinks;j+=1){
digitalWrite (2,HIGH);
delay (blinkDelay);
digitalWrite (2,LOW);
delay (blinkDelay);
}
}

The Result:
How many blinks?
The LED will blink 3 times in... 321How many blinks?
The LED will blink 0 times in... 321How many blinks?

What is the serial monitor line-ending setting?

Please remember to use code tags when posting code

1 Like

Try this :

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 );
  blinks = Serial.parseInt() ;
  if (blinks > 0)
  {
    Serial.print (msg2);
    Serial.print (blinks);
    Serial.print (msg3);
    for (countdown = 3; countdown > 0; countdown -= 1)
    {
      Serial.print (countdown);
      delay(countdownDelay);
    }
    for (j = 1; j <= blinks; j += 1)
    {
      digitalWrite (2, HIGH);
      delay (blinkDelay);
      digitalWrite (2, LOW);
      delay (blinkDelay);
    }
  }
}

@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 @TheMemberFormerlyKnownAsAWOL.

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).


Figure-1:


Figure-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.

Nope.
Already there .

Yes! It is there in the global area. (I could probably see it should the OP use code tags!)