Go Down

Topic: Serial.read inside case statement (Read 1 time) previous topic - next topic

chutki

Code: [Select]
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




MorganS

Because Serial is

S

L

O

W

When you receive one character and "enter" the next section, the following character is still being transmitted. You have to wait 10 milliseconds for the character to arrive. The dumb way to do this is to just put a delay(10) in there.

The smart way is to have no part of your main program dependent on anything arriving at any particular time. Collect characters when (and if) they arrive. When all of the characters you expect have arrived, then you can do something based on that complete message.

Look at Serial Input Basics

While you're at it, clean up your code. void light();  inside setup() is useless. Your indentation can be cleaned up with the CTRL-T command and then it's obvious that the closing brace for loop() has become attached to light() with no gap in between. It should also be obvious that the "after blinking" is actually inside the for() loop which does the blinking.
"The problem is in the code you didn't post."

Go Up