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);
}
}
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);
}
}
}
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
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