0

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:

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

}
}
3
  • 2
    your whiles creates a infinite loop, change its for if. Commented Feb 2, 2018 at 13:20
  • 1
    This is not C++ but Arduino Commented Feb 2, 2018 at 13:25
  • 1
    The languages are very similar, however. Commented Feb 2, 2018 at 13:44

2 Answers 2

1

You have several obvious mistakes... The main one being that you have locked execution into the while loops. So the execution path gets to "while data==" and it will stay there while the data equals that value. That value though can't change as you only read the value of data at the beginning of the function loop(). The only way this could work is if loop were serviced by a timer function and shared between two threads.

Replace your whiles with ifs, and run it using a while (1){loop();}; you may find it goes then. TBH, I'd add the Delay 2000 at the while loop level and only query the serial port every 2000mS too. It's bad karma to hammer things in a flywheel loop.

Sign up to request clarification or add additional context in comments.

1 Comment

because i want to make the LED blink for itself when '1' is read and turn off LED only when '0' is read. If i use if comment, the LED cannot blink itself
0

Change the structure of your loop. Let the loop execute every 2000 ms. If there is data availabe to read from serial, read and parse it to update the 1 / 0 state. Dependent on state, either let the state of the LED toggle between on and off or leave it in off state.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.