0

I have an RGB LED, connected to my UNO R3 and would like to turn on the written color in the serial by following a table (1 for red, 2 for blue, 3 for green...) and I also want to make that if this LED is already turnt ON, I want it to turn OFF. The problem I have is that when I use Serial.read() it always returns a -1 value. Why is that? I leave you the code here and in case you see something wrong please tell me. Thank you very much.

#define RED 7
#define GREEN 4
#define BLUE 2
  String ledcolour[] = {"RED", "BLUE", "GREEN", "YELLOW", "PURPLE", "WHITE"}; //array to match the number typed to the LED colour 
  char ledstatus[] = {0, 0, 0, 0, 0, 0}; //0 if the LED is OFF and 1 if it's ON
  String ledvaluefix[] = {"HIGH", "LOW"}; //if the ledstatus[] value is 0 it will write HIGH and if it's already ON it will write LOW
  char ledstatusfix[] = {1,0}; //to update in ledstatus[] if the LED is ON or OFF
  int led = Serial.read();
void setup() {
  pinMode(RED,OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE,OUTPUT);  
  Serial.begin(9600);
  Serial.println("Type the LED's colour you want to turn on following this table:");
  Serial.println("RED=1");
  Serial.println("BLUE=2");
  Serial.println("GREEN=3");
  Serial.println("YELLOW=4");
  Serial.println("PURPLE=5");
  Serial.println("WHITE=6");  
}

void loop() {
  if (Serial.available()){
    Serial.println(led);
    loop();
   if (led <= 6 && led >=1){ //Checks if value is within 0-6
    if (led <=3){ //Classifies in primary or mixed colours (understand primary as just having to turn ON one single LED)
      if (ledstatus[led-1] == 0){//checks if the LED was OFF
        digitalWrite(led-1,HIGH);//turns the typed LED to HIGH
        char ledstatus[led-1] = {1};//updates the array to set the LED is now ON
        confirmer();//sends a serial confirmation message
      }else{//if it was already ON
        digitalWrite(led-1,LOW);//turns the typed LED to LOW
        char ledstatus[led-1] = {0};//updates the array to set the LED is now OFF
      }
    }else{//if it's not a primary colour
      Serial.println("YOU CHOSE " + ledcolour[led-1] + ", WHICH IS A MIXED COLOUR");
    }
    }else{//if the value is not valid (not between 0 and 6)
      Serial.println("SORRY, THE INTRODUCED VALUE IS NOT VALID");
      Serial.println("PLEASE, TRY AGAIN");
    }
  }
}
 int confirmer(){
    Serial.println("YOU CHOSE THE " + ledcolour[led-1] + " LED");//sends the serial message confirming it's been turned ON
   }
1
  • 1
    Why on earth are you calling loop from loop? That's nasty and leads to infinite recursion and crashing. – Majenko Nov 4 '17 at 23:10
0

This

 int led = Serial.read();

is execute before setup() and loop(), just one time. All initializations outside any function are execute before the start of the program. You have not execute Serial.begin yet.

You probably want to read inside the loop ...

Edit:

Second problem: you are calling loop recursively

void loop()
{
    if(Serial.available()) {
        Serial.println(led);
        loop();

When a byte in available in the serial port, you will call loop. As you never read the serial port, Serial.available will still return > 0, and you will enter in a never ending recursion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.