Turn off what's on and switching

Hello All,

I have the Arduino Uno and 4ch relay board that I am controlling using serial data. I am just learning the Arduino so I am using different pieces of code I am finding online and in examples to do simple functions. However the reason I got into this is because I am trying to control 4 lights that I would like to be able to turn on individually but only 1 can be on at a time.

For example if I send the command "R" I want the red light to turn on, and if I send the command "B" I want the red light to turn off and the blue light to turn on, if I send command "G" I want the blue light to turn off and the green light to turn on, etc. And possibly if I send a "O" command whatever is on will go off.

Of course what is actually being turned on and off are the relays which are turning on lights powered by 110vac or more accurately pins 2,3,4,5 going HIGH / LOW and activating and deactivating the relays.

I hope I explained this clearly and any help the community can provide would be greatly appreciated.

Thank you all.

Here is one way. Should give the idea. You didn't say whether the relays are active high or low. I assumed active high.

void setup()
{
  Serial.begin(9600);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
}

void loop()
{
  if(Serial.available())
  {
    char c = Serial.read();
    switch (c)
    {
    case 'O':  // all off
      digitalWrite(2, LOW);// red
      digitalWrite(3, LOW);// blue
      digitalWrite(4, LOW);// green
      Serial.println("OFF");
      break;
    case 'R':
      digitalWrite(2, HIGH);// red
      digitalWrite(3, LOW);// blue
      digitalWrite(4, LOW);// green
      Serial.println("RED");
      break;
    case 'B':
      digitalWrite(2, LOW);// red
      digitalWrite(3, HIGH);// blue
      digitalWrite(4, LOW);// green
      Serial.println("BLUE");
      break;
    case 'G':
      digitalWrite(2, LOW);// red
      digitalWrite(3, LOW);// blue
      digitalWrite(4, HIGH);// green
      Serial.println("GREEN");
      break;
    }
  }
}

Hi groundfungus that worked out great! My relays are active low so I just made the changes and tested it and works perfect! I really appreciate your help and quick response, I'm looking forward to learning more about the Arduino.

Thanks once again!

misterdiff: Hi groundfungus that worked out great! My relays are active low so I just made the changes and tested it and works perfect! I really appreciate your help and quick response, I'm looking forward to learning more about the Arduino.

Thanks once again!

The Arduino IDE comes with a swag of example sketches. Review them, at least.