0

Sir while entering the values on the serial monitor, I ask the user to enter the value for red led voltage(0-255) then again ask for yellow at the red voltage asking it produces the user to put the value, after that it asks for yellow led input but the time to enter the value for yellow led input is very less and so it does not get entered and the futher program starts. So only red led works where as Yellow does not.

int REDledpin=9;        //RED led on pin 9 of the uno board.
int YELledpin=7;        //YELLOW led on pin 8 of the uno board.
int REDonleddelay=500;  //The red led will be on for 500 ms.
int REDoffleddelay=500; //The red led will be off for 500 ms.
int YELonleddelay=600;  //The YELLOW led will be on for 600 ms.
int YELoffleddelay=600; //The YELLOW led will be off for 600 ms.  
int countredled=5;        //The RED LED count variable
int countyellowled=5;     //The YELLOW LED count variable
String red="Red led is blinking";
String Yellow="Yellow led is blinking";
int redintensity;
int yellowintensity;

void setup() {
  Serial.begin(9600);
  pinMode(REDledpin,OUTPUT);
  pinMode(YELledpin,OUTPUT);

  Serial.println("How much voltage you want to apply to the red LED (0-255)?");
  while(Serial.available()==0){}
  redintensity= Serial.parseInt();


  Serial.println("How much voltage you want to apply to the yellow LED (0-255)?");
  while(Serial.available()==0){}
  yellowintensity = Serial.parseInt(); 

}

void loop() {
Serial.println(red);  
for(int j=1;j<=countredled;j=j+1)
  {
    Serial.print("The red LED blink star: ");
    Serial.println(j);
    analogWrite(REDledpin,redintensity);
    delay(REDonleddelay);
    analogWrite(REDledpin,0);
    delay(REDonleddelay);
  }

Serial.println(" ");
Serial.println(Yellow);
  for(int j=1;j<=countyellowled;j=j+1)
  {
   Serial.print("The yellow LED blink stars");
   Serial.println(j);
   digitalWrite(YELledpin,yellowintensity); 
   delay(YELonleddelay);
   digitalWrite(YELledpin,0);
   delay(YELoffleddelay);
  }
 Serial.println(" ");
}
  • add while (Serial.read() != -1); after parseInt() to read the line termination characters – Juraj Mar 16 at 13:49
1

@Juraj was on the right track however the end-of-line characters for example may not be buffered and the problem could continue.

The correct approach is the wait for the end-of-line character specifically. Let's take the easy way out and use Serial.readStringUntil

#define EOL_CHAR '\n'

void setup() {
  Serial.begin(9600);
  pinMode(REDledpin,OUTPUT);
  pinMode(YELledpin,OUTPUT);

  Serial.println("How much voltage you want to apply to the red LED (0-255)?");
  while(Serial.available()==0){}
  redintensity= Serial.parseInt();

  Serial.readStringUntil(EOL_CHAR); // clear serial buffer to end of line character

  Serial.println("How much voltage you want to apply to the yellow LED (0-255)?");
  while(Serial.available()==0){}
  yellowintensity = Serial.parseInt(); 

}

This all depends on what you're Serial Monitor is sending as a new line; it could be CR (\r), LF (\n) or CRLF (\r\n), so change the EOL_CHAR value accordingly depending on your serial monitor setup.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.