How to make my potentiometer work only when push button is held down?

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)

Can you spot the function in your code that causes the RGB to “show” it’s color? Can you see in loop where it is called? Can you put that in an if statement that checks the button? Do you want it to be completely off when you let go of the button? Can you write an else clause for that if statement that turns the LED off?

What’s hard about this? Can you tell me which one of those things stumps you? If I know that maybe I can help you.

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 ()”

What do you mean nothing happened? What did you want to happen? What actually did happen? Did the LED never turn on? OR did it not turn off. Did you need an else with that if?

Do you have a pull-up resistor on that button? How is it wired? Why aren't you using the internal pullup?

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.

Read this:
http://www.gammon.com.au/forum/?id=11955

.

Well you haven’t written any code that says to turn it off. So you want: If the buttonState is LOW turn it on (you have that) else turn it off? That literally translates directly from english to code.

What do you mean you don’t know what the input is? You wrote it in the code? An input is a pin where you are reading a voltage? Like pin 13. See the pinMode call? You wrote it. How can you miss it?

What is a pull-up? It’s something you really need to know about. Here, check this link. Gammon Forum : Electronics : Microprocessors : Switches tutorial

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

That link I gave you discusses the built in pull-up resistors on the Arduino.