char data = 0; //Variable for storing received data
void setup()
{
Serial.begin(9600); //Sets the baud for serial data transmission
pinMode(13, OUTPUT); //Sets digital pin 13 as output pin
void light ();
}
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");
switch(data) {
case '1':
light();
break;
case '0' :
digitalWrite(13, LOW);
break ;
default:
break ;
}
}
}
void light ()
{
Serial.println("entering");
while (Serial.available()>=2)
{
Serial.print("eneterd into");
{
char times[3]= {'\0'};
for(byte x=0;x<2;x++) {
times[x]= Serial.read();
}
Serial.println("Received time:");
Serial.print(times);
Serial.println("\n");
int time_blink = atoi(times);
Serial.println("Time in integer :");
Serial.print(time_blink);
for ( int i = time_blink; i > 0; i--) {
digitalWrite(13, HIGH); //If value is 1 then LED turns ON
delay(100); // Checks whether value of data is equal to 0
digitalWrite(13, LOW); //If value is 0 then LED turns OFF
delay(100);
digitalWrite(13, LOW);
Serial.println("--------Afrer blinkking turned off------------");
}
}
}
}
hello all,
According to my code, when i send 0 the led should turn off. When i send 1,LED should blink for a particular time,which will be given through serial. But here, when i press 1, the code 'entering' only gets printed. When i send a two digit number,it just prints on the screen,but there is no blinking of LED (ofcourse it is not entering the loop). Can anybody help ? Why is serial.avaiable not working? What Am i missing?
Thanks in advance