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 blink until '0' is send via serial monitor, the LED will be turn off. Then, when next '1' is send , the Led ON PIN 3 will blink again. But it doesn't work for my code. This is my first time to use arduino, can anyone tell me what is wrong in my code and help me to do some correction . Below is my code:
My task for this project is when '1' is send via serial monitor, the Led ON PIN 3 need to blink until '0' is send via serial monitor, the LED will be turn off. Then, when next '1' is send , the Led ON PIN 3 will blink again. But it doesn't work for my code. This is my first time to use arduino, can anyone tell me what is wrong in my code and help me to do some correction . 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);
}
while(data == '0') // Checks whether value of data is equal to 0
{
digitalWrite(3, LOW); //If value is 0 then LED turns OFF
}
}
}