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:
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
}
}