I am trying to make a project right now just for fun and I'm a beginner at Arduino. I'm trying to make a project that allows a potentiometer to change the color of a RGB LED only when a push button is held down. So when the push button is in the HIGH position the RGB LED is off. I hope to then incorporate this mini project into a bigger one sooner. So far I have the potentiometer changing the color of the RGB LED when turned in both directions. That part is fully functional. The only issue is the push button. I can't seem to figure out where to put the code that makes the RGB LED only work when the push button is in the LOW position (pushed down). Can someone please spot out where in my code I need to insert this. Thanks!
lab_3.ino (1.18 KB)
My problem is with where to put the if statement and how I should be writing it. here is my code incase you missed the download:
const int RED_PIN = 9;
const int GREEN_PIN = 10;
const int BLUE_PIN = 11;
int sensorPin = 0;
const int button = 13;
int buttonState = 0;
int DISPLAY_TIME = 100;
void setup()
{
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
pinMode(A0, INPUT);
Serial.begin(9600);
pinMode(button, INPUT);
}
void loop()
{
buttonState = digitalRead(button);
int sensorValue;
sensorValue = analogRead(sensorPin);
Serial.println(sensorValue);
showRGB(sensorValue);
}
void showRGB(int color)
{
int redIntensity;
int greenIntensity;
int blueIntensity;
if (color <= 255)
{
redIntensity = 255 - color;
greenIntensity = color;
blueIntensity = 0;
}
else if (color <= 511)
{
redIntensity = 0;
greenIntensity = 255 - (color - 256);
blueIntensity = (color - 256);
}
else
{
redIntensity = (color - 512);
greenIntensity = 0;
blueIntensity = 255 - (color - 512);
}
analogWrite(RED_PIN, redIntensity);
analogWrite(BLUE_PIN, blueIntensity);
analogWrite(GREEN_PIN, greenIntensity);
}
I tried doing this:
if (buttonState == LOW) {
showRGB (sensorValue);
}
but nothing happened. I put that right where "showRGB (sensorValue);" is now in the "void loop ()"
I'm trying to make an if statement that will cause the RGB LED to stay off when the button isn't being pressed.
Then when I press and hold the button I want the potentiometer to be able to change the color of the RGB LED. And when I let go of the button the RGB LED is off again.
I don't know what a pullup resistor is or what the input is either.
my bad I meant to say the internal pullup not the input pullup.
But anyways you just opened my eyes with that last post and I go it!! Thanks!! Now I have finally completes my mini-project.
Here's the finished code if anyone cares:
//Quinn Nolan
const int RED_PIN = 9;
const int GREEN_PIN = 10;
const int BLUE_PIN = 11;
int sensorPin = 0;
const int button = 13;
int buttonState = 0;
int DISPLAY_TIME = 100;
void setup()
{
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
pinMode(A0, INPUT);
Serial.begin(9600);
pinMode(button, INPUT);
}
void loop()
{
buttonState = digitalRead(button);
int sensorValue;
sensorValue = analogRead(sensorPin);
Serial.println(sensorValue);
if (buttonState == LOW) {
showRGB(sensorValue);
}
else {
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
}
}
void showRGB(int color)
{
int redIntensity;
int greenIntensity;
int blueIntensity;
if (color <= 255)
{
redIntensity = 255 - color;
greenIntensity = color;
blueIntensity = 0;
}
else if (color <= 511)
{
redIntensity = 0;
greenIntensity = 255 - (color - 256);
blueIntensity = (color - 256);
}
else
{
redIntensity = (color - 512);
greenIntensity = 0;
blueIntensity = 255 - (color - 512);
}
analogWrite(RED_PIN, redIntensity);
analogWrite(BLUE_PIN, blueIntensity);
analogWrite(GREEN_PIN, greenIntensity);
}