Complete n00b looking for help - Controlling Multiple RGBLeds with Reed Switches

Hi All

I’ve just dipped my toes into the wonderful world of electronics. Primarily for use on Dioramas and Table Top Gaming terrain.

I’m using an Uno, and trying to have 3 things happen:

Function 1: 5 x RGB Led’s, all on the same line, fading between Green and Yellow.

Function 2: 2 x Blue LED’s, each powered on by 2 x magnetic reed switches.

Function 3: 1 x RGB LED that’s colour is dependant on the state of the above switches (Green if both switches are powered on, orange if only one switch is powered on and red if both switches are off).

For my test circuit I’ve only used a single RGB LED for Function 1, these will all be connected along a single line. Diagram attached.

Now the code, and this is the part where I’m super noob!

int ooze_red_light_pin= 11;
int ooze_blue_light_pin = 10;
int ooze_green_light_pin = 9;
int red_light_pin= 6;
int blue_light_pin = 5;
int green_light_pin = 3;
int switch_1_pin = 1;
int switch_2_pin = 2;

void setup() {
  pinMode(ooze_red_light_pin, OUTPUT);
  pinMode(ooze_green_light_pin, OUTPUT);
  pinMode(ooze_blue_light_pin, OUTPUT);
  pinMode(red_light_pin, OUTPUT);
  pinMode(green_light_pin, OUTPUT);
  pinMode(blue_light_pin, OUTPUT);
  pinMode(switch_1_pin, OUTPUT);
  pinMode(switch_2_pin, OUTPUT);
}
void loop() {
  digitalWrite(switch_1_pin, HIGH);
  digitalWrite(switch_2_pin, HIGH);
  // If both pins are high, set green
  if(switch_1_pin == HIGH && switch_2_pin == HIGH) 
  {
    RGB_color_2(0, 255, 0); //Green
  }
  // else if one pin is high, set orange maybe
  else(switch_1_pin == HIGH || switch_2_pin == HIGH 
  {
    RGB_color_2(127,255,0); // Some colour??
  }
  // if neither of the top, make it red
  else
  {
    RGB_color_2(255, 0, 0); //Red
  }
  delay(100);
}
  RGB_color_1(0, 255, 0); // Green
  delay(150);
  RGB_color_1(65, 255, 0); // Greener Yellow
  delay(150);
  RGB_color_1(127, 255, 0); // Green/Yellow
  delay(150);
  RGB_color_1(190, 255, 0); // Yellower Green
  delay(150);
  RGB_color_1(255, 255, 0); // Yellow
  delay(150);
  RGB_color_1(190, 255, 0); // Yellower Green
  delay(150);
  RGB_color_1(127, 255, 0); // Green/Yellow
  delay(150);
  RGB_color_1(65, 255, 0); // Greener Yellow
  delay(100);
}
void RGB_color_1(int red_light_value, int green_light_value, int blue_light_value)
 {
  analogWrite(ooze_red_light_pin, red_light_value);
  analogWrite(ooze_green_light_pin, green_light_value);
  analogWrite(ooze_blue_light_pin, blue_light_value);
}
void RGB_color_2(int red_light_value, int green_light_value, int blue_light_value)
 {
  analogWrite(red_light_pin, red_light_value);
  analogWrite(green_light_pin, green_light_value);
  analogWrite(blue_light_pin, blue_light_value);
}

First off: Is what I’m trying to do with the switches even possible? Or do I need to alter the circuit?

Any help would be greatly appreciated :slight_smile:

The switches aren't going to work as you have them at the moment. You want them to control leds for functions 2 and 3. You will need to set them up as inputs, read their state and light the leds they control appropriately.

It doesn't make sense to have the switches in line with the blue leds in other words.

Thanks for the speedy reply :slight_smile:

I’ve adjusted the diagram, attached.

I’m still unsure of how to write the code for it though?

Try starting with some simpler code. You have done the same thing as every other beginner and tried to write the whole thing up front.

Just read the state of the switches using digitalRead and depending on what you get, turn LEDs on or off.

Use INPUT_PULLUP for the pinMode for the switches.

D0 and D1 are used for the serial port which is useful for debugging. I'd suggest you move the switch away from D1.

Ok, I've moved the switches to Pins 2 and 4.

I've managed to get the switches to operate the blue lights as intended, i.e when each switch is powered on, the LED lights, and when powered off, it turns off.

Furthermore, I've managed to get the second LED to shine Red on boot up with both switches in the off position. Then when I turn on one of the switches, it turns yellow, as intended.

However, if I again turn that switch off, it doesnt revert back to it's red state, or if I turn both on, go to green. It effectively becomes stuck on the first "changed" state.

The colour shifting LED is operating as intended.

Code as follows

int ooze_red_light_pin= 11;
int ooze_blue_light_pin = 10;
int ooze_green_light_pin = 9;
int blue_led_1_pin = 7;
int blue_led_2_pin = 8;
int red_light_pin = 6;
int blue_light_pin = 5;
int green_light_pin = 3;
int switch_1_pin = 2;
int switch_2_pin = 4;

void setup() {
  Serial.begin(9600);
  Serial.print("Sketch:   ");   Serial.println(__FILE__);
  Serial.print("Uploaded: ");   Serial.println(__DATE__);
  Serial.println(" ");
  pinMode(ooze_red_light_pin, OUTPUT);
  pinMode(ooze_green_light_pin, OUTPUT);
  pinMode(ooze_blue_light_pin, OUTPUT);
  pinMode(blue_led_1_pin, OUTPUT);
  pinMode(blue_led_2_pin, OUTPUT);
  pinMode(red_light_pin, OUTPUT);
  pinMode(green_light_pin, OUTPUT);
  pinMode(blue_light_pin, OUTPUT);
  pinMode(switch_1_pin, INPUT_PULLUP);
  pinMode(switch_2_pin, INPUT_PULLUP);  
  digitalWrite(blue_led_1_pin,LOW);
  digitalWrite(blue_led_2_pin,LOW); 
}
void loop() {
  if (digitalRead(switch_1_pin) == HIGH) 
    {
       digitalWrite(blue_led_1_pin, LOW);
    }
    else
    {
       digitalWrite(blue_led_1_pin, HIGH);
    }
  if ( digitalRead(switch_2_pin) == HIGH) 
    {
       digitalWrite(blue_led_2_pin, LOW);
    }
    else
    {
       digitalWrite(blue_led_2_pin, HIGH);
    }
  if (digitalRead(switch_1_pin) == HIGH && digitalRead(switch_2_pin) == HIGH)
    {
       digitalWrite(red_light_pin, HIGH);
    }
    else if (digitalRead(switch_1_pin == LOW) || digitalRead(switch_2_pin) == LOW)
    {
       digitalWrite(green_light_pin, HIGH);
       digitalWrite(red_light_pin, HIGH);
    }
    else 
    {
       digitalWrite(green_light_pin, HIGH);
    }
  RGB_color_1(0, 255, 0); // Green
  delay(150);
  RGB_color_1(65, 255, 0); // Greener Yellow
  delay(150);
  RGB_color_1(127, 255, 0); // Green/Yellow
  delay(150);
  RGB_color_1(190, 255, 0); // Yellower Green
  delay(150);
  RGB_color_1(255, 255, 0); // Yellow
  delay(150);
  RGB_color_1(190, 255, 0); // Yellower Green
  delay(150);
  RGB_color_1(127, 255, 0); // Green/Yellow
  delay(150);
  RGB_color_1(65, 255, 0); // Greener Yellow
  delay(150);
}
void RGB_color_1(int red_light_value, int green_light_value, int blue_light_value)
 {
  analogWrite(ooze_red_light_pin, red_light_value);
  analogWrite(ooze_green_light_pin, green_light_value);
  analogWrite(ooze_blue_light_pin, blue_light_value);
}

The first issue is simple. There is nothing in your code that ever turns the green light off. So if you start in red mode and then change, the green led comes on and stays on so even if both switches are released, it'll stay yellow.

The second issue is more subtle. If one switch or the other is pressed, show yellow. However that or will be true too if they are both pressed which means that there is no way to get to the green light state. You need some more complex logic there.

Even when you do of course, there's nothing to turn the red led off either.

Strangely enough I managed to solve it! All I had to do was replace the digitalWrites in && and || sections with RGB statements:

Working code as follows:

int ooze_red_light_pin= 11;
int ooze_blue_light_pin = 10;
int ooze_green_light_pin = 9;
int blue_led_1_pin = 7;
int blue_led_2_pin = 8;
int red_light_pin = 6;
int blue_light_pin = 5;
int green_light_pin = 3;
int switch_1_pin = 2;
int switch_2_pin = 4;

void setup() {
  Serial.begin(9600);
  Serial.print("Sketch:   ");   Serial.println(__FILE__);
  Serial.print("Uploaded: ");   Serial.println(__DATE__);
  Serial.println(" ");
  pinMode(ooze_red_light_pin, OUTPUT);
  pinMode(ooze_green_light_pin, OUTPUT);
  pinMode(ooze_blue_light_pin, OUTPUT);
  pinMode(blue_led_1_pin, OUTPUT);
  pinMode(blue_led_2_pin, OUTPUT);
  pinMode(red_light_pin, OUTPUT);
  pinMode(green_light_pin, OUTPUT);
  pinMode(blue_light_pin, OUTPUT);
  pinMode(switch_1_pin, INPUT_PULLUP);
  pinMode(switch_2_pin, INPUT_PULLUP);  
  digitalWrite(blue_led_1_pin,LOW);
  digitalWrite(blue_led_2_pin,LOW); 
}
void loop() {
  if (digitalRead(switch_1_pin) == HIGH) 
    {
       digitalWrite(blue_led_1_pin, LOW);
    }
    else
    {
       digitalWrite(blue_led_1_pin, HIGH);
    }
  if ( digitalRead(switch_2_pin) == HIGH) 
    {
       digitalWrite(blue_led_2_pin, LOW);
    }
    else
    {
       digitalWrite(blue_led_2_pin, HIGH);
  }
  if (digitalRead(switch_1_pin) == HIGH && digitalRead(switch_2_pin) == HIGH)
    {
       RGB_color_2(255, 0, 0);
    }
    else if (digitalRead(switch_1_pin) == LOW && digitalRead(switch_2_pin) == HIGH)
    {
       RGB_color_2(255, 75, 0);
    }
    else if (digitalRead(switch_1_pin) == HIGH && digitalRead(switch_2_pin) == LOW)
    {
       RGB_color_2(255, 75, 0);
    }
    else if (digitalRead(switch_1_pin) == LOW && digitalRead(switch_2_pin) == LOW)
    {
       RGB_color_2(0, 255, 0);
  }
  RGB_color_1(0, 255, 0); // Green
  delay(150);
  RGB_color_1(65, 255, 0); // Greener Yellow
  delay(150);
  RGB_color_1(127, 255, 0); // Green/Yellow
  delay(150);
  RGB_color_1(190, 255, 0); // Yellower Green
  delay(150);
  RGB_color_1(255, 255, 0); // Yellow
  delay(150);
  RGB_color_1(190, 255, 0); // Yellower Green
  delay(150);
  RGB_color_1(127, 255, 0); // Green/Yellow
  delay(150);
  RGB_color_1(65, 255, 0); // Greener Yellow
  delay(150);
}
void RGB_color_1(int red_light_value, int green_light_value, int blue_light_value)
 {
  analogWrite(ooze_red_light_pin, red_light_value);
  analogWrite(ooze_green_light_pin, green_light_value);
  analogWrite(ooze_blue_light_pin, blue_light_value);
}
void RGB_color_2(int red_light_value, int green_light_value, int blue_light_value)
 {
  analogWrite(red_light_pin, red_light_value);
  analogWrite(green_light_pin, green_light_value);
  analogWrite(blue_light_pin, blue_light_value);
}

Much easier! Why have RGB functions and ignore them :wink: