Go Down

Topic: buttonState will not change my RGB LED, stuck on 'ON' (Read 106 times) previous topic - next topic

SkyCrafter

My code, as I understand it (I've came back from about a year and a half of not coding) should toggle the RGB LED from red to green, but it stays stuck on green. The code compiles just fine.
My code: (I've modified some of it from the reference tutorials.)
Code: [Select]
int buttonState = HIGH;
int previous = LOW;
int bluePin = 11;
int greenPin = 10;
int redPin = 9;
int buttonPin = 7;
int buttonPressed = digitalRead(buttonPin);
long time = millis();
long debounce = 200;


void setup()
{
  pinMode(buttonPin,INPUT);
  pinMode(bluePin,OUTPUT);
  pinMode(greenPin,OUTPUT);
  pinMode(redPin,OUTPUT);
 
}

void loop()
{
  if (buttonPressed == HIGH && previous == LOW) {
    if (buttonState == HIGH)
      buttonState = LOW;
    else
      buttonState = HIGH;

    time = millis();   
  }
  previous = buttonPressed;
  if (buttonState == HIGH) {
    setColor(0,255,0);
  }
  if (buttonState == LOW) {
    setColor(255,0,0);
  }
  delay(10);
}
void setColor(int red, int green, int blue)
{
  #ifdef COMMON_ANODE
    red = 255 - red;
    green = 255 - green;
    blue = 255 - blue;
  #endif
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue); 
}


Some people are just dinguses. Some people are just geniuses. I'm a dingus. It's fun to push dinguses down the stairs. It's fun to push yourself down the stairs.

AWOL

Have you thought about reading the state of the switch?

SkyCrafter

Have you thought about reading the state of the switch?
Yes, but this is a push button, How exactly would I do that?
Some people are just dinguses. Some people are just geniuses. I'm a dingus. It's fun to push dinguses down the stairs. It's fun to push yourself down the stairs.

PickyBiker

#3
May 15, 2019, 08:46 pm Last Edit: May 15, 2019, 08:49 pm by PickyBiker
I don't know hoe the switch is wired or if it has a pullup resistor. My guess is it does not have a pullup resistor. You could try this:

Code: [Select]
pinMode(buttonPin,INPUT_PULLUP);

You must also read the button state every time through the loop.

Blue Eyes

You declare the variable buttonPressed like this:

Code: [Select]

int buttonPressed = digitalRead(buttonPin);


that will read the button once.  You want to read it continually in loop()

Code: [Select]

buttonPressed = digitalRead(buttonPin);

if (buttonPressed == HIGH && previous == LOW) {

SkyCrafter

You declare the variable buttonPressed like this:

Code: [Select]

int buttonPressed = digitalRead(buttonPin);


that will read the button once.  You want to read it continually in loop()

Code: [Select]

buttonPressed = digitalRead(buttonPin);

if (buttonPressed == HIGH && previous == LOW) {


Ahh, I guess I thought it would read it every time it was called. Thanks!
Some people are just dinguses. Some people are just geniuses. I'm a dingus. It's fun to push dinguses down the stairs. It's fun to push yourself down the stairs.

Go Up