To control LED using serial monitor

Thread Starter

Lemon_Foam

Joined Sep 18, 2017
39
My project is to control the LED by send '1' or '0' via serial monitor.
My task for this project is when '1' is send via serial monitor, the Led ON PIN 3 need to turn on and off every 2000ms. Then, when '0' is send via serial monitor, the LED need to be turn off until next '1' is send , so that the Led ON PIN 3 can be turn on and off every for 2000ms again. But it doesn't work for my code, can anyone tell me what is wrong in my code. Below is my code:


Code:
char data = 0;            //Variable for storing received data
void setup()
{
    Serial.begin(115200);   //Sets the baud for serial data transmission                              
    pinMode(3, OUTPUT);  //Sets digital pin 3 as output pin
}
void loop()
{
   if(Serial.available()>0 )      // Send data only when you receive data:
   {
      data = Serial.read();        //Read the incoming data send via serial monitor & store into data
    Serial.print(data);          //Print Value inside data in Serial monitor
     Serial.print("\n");       
    

            while(data == '1')          //Do looping so that when '1' send via serial monitor, the LED can blink
            {
                digitalWrite(3, HIGH);  
                delay(2000);
               digitalWrite(3, LOW);
               delay(2000);
         
            }

      while(data == '0')         //  Checks whether value of data is equal to 0
         digitalWrite(3, LOW);    //If value is 0 then LED turns OFF
        
}
}
 

djsfantasi

Joined Apr 11, 2010
9,156
Once data takes the value of ‘1’, your while loop becomes an infinite loop. ‘data’ never changes. You’ll need to check if there is serial data available and read it into ‘data’.
 

JohnInTX

Joined Jun 26, 2012
4,787
@djsfantasi beat me to it.
I'll add to be sure that once your get your command 1 or 0 from the serial port be sure to flush the input buffer by reading until Serial.available() returns 0. Then you can decode the command, enter the appropriate loop and do it while Serial.available()==0, indicating new data. That way you get rid of any stray characters on the line including CR, LF etc. Your input routine should handle invalid commands as well.

Good luck!
 

Thread Starter

Lemon_Foam

Joined Sep 18, 2017
39
because i want to make the LED blink for itself when '1' is read and turn off LED only when '0' is read. So i need a loop
 

JohnInTX

Joined Jun 26, 2012
4,787
because i want to make the LED blink for itself when '1' is read and turn off LED only when '0' is read. So i need a loop
That's true but look at the loop that flashes the LED. It looks for a change to 'data' to exit the loop but you don't read the serial port in the loop - so 'data' will never change and the loop will never exit. That means that once the LED is flashing, you can bang on the keyboard all day and it won't change.

What I suggested was one way to fix that problem without replicating the serial code in each loop.
 

Thread Starter

Lemon_Foam

Joined Sep 18, 2017
39
Once data takes the value of ‘1’, your while loop becomes an infinite loop. ‘data’ never changes. You’ll need to check if there is serial data available and read it into ‘data’.
Sir u mean i need to check the serial input in my loop? I have change my code as follow, but it still doesn't work, because the LED can't blink anymore after i send '1' via serial monitor, below is my code:

Code:
char data = 0;            //Variable for storing received data
void setup()
{
    Serial.begin(115200);   //Sets the baud for serial data transmission                              
    pinMode(3, OUTPUT);  //Sets digital pin 13 as output pin
}
void loop()
{
   if(Serial.available()>0 )      // Send data only when you receive data:
   {
      data = Serial.read();        //Read the incoming data & store into data
    Serial.print(data);          //Print Value inside data in Serial monitor
     Serial.print("\n");       
    

            while(data == '1')
            {
                digitalWrite(3, HIGH);   //If value is 1 then LED turns ON
                delay(2000);
               digitalWrite(3, LOW);
               delay(2000);
              
                   data = Serial.read();        //Read the incoming data & store into data
                   Serial.print(data);          //Print Value inside data in Serial monitor
                   Serial.print("\n");       
                 

            }   

      while(data == '0')         //  Checks whether value of data is equal to 0
      {
         digitalWrite(3, LOW);    //If value is 0 then LED turns OFF

         
                   data = Serial.read();        //Read the incoming data & store into data
                   Serial.print(data);          //Print Value inside data in Serial monitor
                   Serial.print("\n");       
                 



      }
        
        
}
}
 

MrChips

Joined Oct 2, 2009
30,707
You need to understand the difference between a "blocking" and "non-blocking" function.

In a blocking function, code execution cannot continue until the function is successfully completed.
In a non-blocking function, you can return a status condition and continue code execution even though the function was not succesful.

This was your original concept of a blinking LED loop.
Code:
loop:
  turn on LED
  delay
  turn off LED
  delay
  go to loop
Now, if you insert a blocking function (such as waiting for a character) in the loop, the loop cannot proceed and your LED will not blink.
You need to use a non-blocking function and test for a status condition as follows:
Code:
loop:
  turn on LED
  delay
  turn off LED
  delay
  test if character received using non-blocking function
  if no character received go to loop
  else exit loop
 

djsfantasi

Joined Apr 11, 2010
9,156
I was giving you general advice. I’ve learned that was confusing. MrChips has described an important programming construct that you need to understand. JohnInTX gave you specifics as to how you might use a non blocking function. You need to understand how you can use Serial.available() to see if your state of the LEDs has changed. Note it is not as simple as using one line of code.
 

Thread Starter

Lemon_Foam

Joined Sep 18, 2017
39
Alright, i get it already, thx all
I was giving you general advice. I’ve learned that was confusing. MrChips has described an important programming construct that you need to understand. JohnInTX gave you specifics as to how you might use a non blocking function. You need to understand how you can use Serial.available() to see if your state of the LEDs has changed. Note it is not as simple as using one line of code.
 
Top