Strange effect

Discussion in 'Arduino IDE' started by borism, Oct 27, 2014.

  1. borism

    borism New Member

    Joined:
    Oct 21, 2014
    Messages:
    4
    Likes Received:
    0
    this code works fine:

    Code:
    void setup(){
    
     Serial.begin(115200);
    }
    
    void loop()
    {
      if(Serial.available()>0) {
          char character = Serial.read();
          if(character=='1')
          {
            Serial.println("STATUS ON");
          }
          else if(character=='0')
          {
            Serial.println("STATUS OFF");
          } 
         
      }
    }
    this code does not work at all (added last else)
    Code:
    void setup(){
    
     Serial.begin(115200);
    }
    
    
    void loop()
    {
      if(Serial.available()>0) {
          char character = Serial.read();
          if(character=='1')
          {
            Serial.println("STATUS ON");
          }
          else if(character=='0')
          {
            Serial.println("STATUS OFF");
          } 
          else Serial.println("UNKNOWN");
      }
    }
    Can anyone explain why?

    Regards,

    Boris
     
  2. biazzotto

    biazzotto New Member

    Joined:
    May 10, 2014
    Messages:
    2
    Likes Received:
    0
    Hi Boris,

    Could you please try the code below?

    Code:
    void setup(){
     Serial.begin(115200);
    }
    
    void loop()
    {
      if(Serial.available()>0) {
          char character = Serial.read();
          if(character=='1')
          {
            Serial.println("STATUS ON");
          }
          else if(character=='0')
          {
            Serial.println("STATUS OFF");
          } 
          else
          {
            Serial.println("UNKNOWN");
          } 
    }
    I can't try this right now, because I don't have my UDOO with me at the moment...

    Reference: http://arduino.cc/en/Reference/else
     
  3. Simsy

    Simsy New Member

    Joined:
    Jan 12, 2015
    Messages:
    7
    Likes Received:
    0
    What did you mean by not work at all ? Compile but not run, not compile?
     

Share This Page