The Program doesn't wait till the input is given

Hello
I'm a newbie to Arduino, this is my first program. The problem is that the program doesn't wait till the input is given. I referred to a few examples but it wasn't helpful. Please point out the mistake done and explain it, it will be really helpful : D

The program is :

/**
 * Write a program to accept the delay time and number of times the 13th pin led should blink 
 * from the user
 */
 int dt,num=0,i=1;
 const int led=13;
void setup() {
  Serial.begin(9600);
  pinMode ( led, OUTPUT);
  delaytime();// Line 28
  ledblink();//Line 34
}

void loop() {
  blinking();
}
void blinking()
{ 
  for (;i<=num;i++)
  {
    digitalWrite(led,HIGH);
    delay(dt);
    digitalWrite(led,LOW);
    delay(dt);
  }
  if (i==5)
  {exit(0);}
}
void delaytime()
{ 
  Serial.println("Enter the delay time ");
  if(Serial.available()==0){}
  dt=Serial.parseInt();
}
void ledblink()
{ 
  Serial.println("Enter how many times should the led blink");
  if ( Serial.available()== 0){}
  num=Serial.parseInt();
}
  

And this is the output on the Serial monitor
image

Your post was MOVED to its current location as it is more suitable.

You probably want a "while" instead of "if".

Please use the flag icon to attract the attention of a moderator, and ask them to move this topic elsewhere - it is not a tutorial.

Edit: that was quick!

1 Like

Already moved

Hello TheMemberFormerlyKnownAsAWOL
Your solution did solve my problem! But the led isn't blinking XC. Can you please explain why we use "while" instead of "if" and why isn't the led blinking?

P.S. Please copy-paste my code and edit it accordingly so that the led blinks and program stop when led blinks according to the user's input data. I'm sorry if this causes you any trouble, but can you please help me?

Thanks for moving it, uhh... but can you tell what is a moderator? Since you're one of it, can you tell me more about it...

Moderators are forum members who volunteer to help maintain good order in the forum by moving inappropriately posted topics, deleting spam posts, deleting duplicate accounts and generally keeping the forum tidy.

We also have the ability to suspend users whose behaviour is unacceptable, such as profanity or other offensive language, but that does not happen too much on this forum

There are different moderators for each International section of the forum in order to deal with the different languages used

Because a "while" loops as long as the condition is true.
An "if" just evaluates the condition once.

The LED is blinking, just very briefly.

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