How to not mix RGB LED colors with 2 inputs

I have a sensor (in my practice setup it is a slide switch) and a button controlling an RGB LED. When the sensor is made, it is green, when it is not made, it is yellow.

My goal is to make the LED go solid red anytime the button is pressed. How it stands currently in my simulation, is whenever the button is pressed it mixes with the color being produced form the sensor.

(Sensor made (GREEN) w/o button (RED) -> GREEN)
(Sensor made (GREEN) w/ button (RED) -> YELLOW)

int red = 11;
int blue = 9;
int green = 6;

int sensor = 2;
int button = 12;

int readingSensor;
int readingButton;

void setup()
{
  Serial.begin(9600);
  
  pinMode(sensor, INPUT);
  pinMode(button, INPUT);
  
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);
  pinMode(red, OUTPUT);
}

void loop()
{
  readingSensor = digitalRead(sensor);
  
  if(readingSensor == HIGH){
    digitalWrite(green, 255);
    digitalWrite(blue, 0);
    digitalWrite(red, 0);
  }
  if(readingSensor == LOW){
    digitalWrite(green, 165);
    digitalWrite(blue, 0);
    digitalWrite(red, 255);
  }
  readingButton = digitalRead(button);
  
  if(readingButton == HIGH){
    digitalWrite(green, 0);
    digitalWrite(blue, 0);
    digitalWrite(red, 255);
  }
}

shouldn't reading and processing the sensor be the else condition if button is not HIGH?

Welcome to the forum

Try this (untested code)

int red = 11;
int blue = 9;
int green = 6;

int sensor = 2;
int button = 12;

int readingSensor;
int readingButton;

void setup()
{
    Serial.begin(9600);

    pinMode(sensor, INPUT);
    pinMode(button, INPUT);

    pinMode(green, OUTPUT);
    pinMode(blue, OUTPUT);
    pinMode(red, OUTPUT);
}

void loop()
{
    readingButton = digitalRead(button);

    if (readingButton == HIGH)
    {
        digitalWrite(green, 0);
        digitalWrite(blue, 0);
        digitalWrite(red, 255);
    }
    else
    {
        readingSensor = digitalRead(sensor);

        if (readingSensor == HIGH)
        {
            digitalWrite(green, 255);
            digitalWrite(blue, 0);
            digitalWrite(red, 0);
        }
        if (readingSensor == LOW)
        {
            digitalWrite(green, 165);
            digitalWrite(blue, 0);
            digitalWrite(red, 255);
        }
    }
}

Yep, that worked!!! Thank you very much Mr. HeliBob!!

I have another question for you. How could I have these printed in the serial monitor without it looping infinitely? (print once then if the input changes, it reads the most recent input)

When the sensor is met (Green) print "Turret is made"
When the button is pressed (Red) print "Turret is moving!"
When the sensor is not met (Yellow) print "Proceed with Caution"

This code below prints "Turret is moving!" repeatedly in the Serial monitor, how can I have it just print once?

int red = 11;
int blue = 9;
int green = 6;

int sensor = 2;
int button = 12;

int readingSensor;
int readingButton;

void setup()
{
    Serial.begin(9600);

    pinMode(sensor, INPUT);
    pinMode(button, INPUT);

    pinMode(green, OUTPUT);
    pinMode(blue, OUTPUT);
    pinMode(red, OUTPUT);
}

void loop()
{
    readingButton = digitalRead(button);

    if (readingButton == HIGH)
    {
        digitalWrite(green, 0);
        digitalWrite(blue, 0);
        digitalWrite(red, 255);
      Serial.print("Turret is moving!");
    }
    else
    {
        readingSensor = digitalRead(sensor);

        if (readingSensor == HIGH)
        {
            digitalWrite(green, 255);
            digitalWrite(blue, 0);
            digitalWrite(red, 0);
        }
        if (readingSensor == LOW)
        {
            digitalWrite(green, 165);
            digitalWrite(blue, 0);
            digitalWrite(red, 255);
        }
    }
}

That was the issue, still very new to this.

Thanks!

You need to detect when the button becomes pressed rather than when it is pressed
See the StateChangeDetection example in the IDE

By the way, have you got pulldown resistors in place to keep the inputs in a known state at all times ?

I do not believe I do. This project is a proof of concept, so I am making it within tinkercad before purchasing the components. I am very new to this.

I know nothing about Tinkercad, but in the real world if a pin is not held explicitly HIGH or LOW then it is floating at an unknown voltage. Could be HIGH, could be LOW, could be changing unpredictably

or do youi mean configure the inputs as INPUT_PULLUP and recognize LOW as being pressed?

I wanted to get the basics out of the way first before making suggestions, but I would recommend INPUT_PULLUP be used in practice

1 Like

You are not using digitalWrite() correctly. The second parameter should be LOW or HIGH and not a number. I think you are confusing it with analogWrite().

To my limited knowledge, I have this set up using a PWM output therefore I can use a range from 0-255; this range changes the voltage from 0V to 5V. To get the color orange, I should use R, 255, G, 165, B, 0.

This should give me 5V to the red leg, ~2.5V to the green leg, and 0V to the blue leg. When these mix through the single LED lense it looks orange... I think.

That is true, but you must use analogWrite() rather than digitalWrite()

Note too that the PWM output will always be 0V or 5V, not a value in between. The PWM signal turns on/off rapidly and it is the ratio of on and off that causes LEDs to change their brightness

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.