Having trouble adding 1 to a variable and making lights correspond to those values

I made some code that's supposed to add 1 to a variable, and a have 3 lights that are supposed to light up when the variable reaches a certain value.

(When the variable=0 the red LED lights up, when the variable=1 the yellow LED lights up etc.
when the variable's value goes above 2 its resets to 0 again)

When I upload the code to my arduino board the yellow light lights up and when I press the button nothing happens.

Ive been trying to find a solution for a while and I haven't been able to fix my code at all.

const int pinRed = 12;
const int pinYellow = 11;
const int pinGreen = 10;
const int button = 9;
int lastButtonState = 0;
int buttonState = 0;
int lightState = 0;

void setup() {
pinMode(pinRed, OUTPUT);
pinMode(pinYellow, OUTPUT);
pinMode(pinGreen, OUTPUT);
pinMode(button, INPUT);

}

void loop() {

buttonState = digitalRead(button);

if (buttonState != lastButtonState) {

if (buttonState == HIGH) {

  lightState++;
  Serial.println("on");
  Serial.print("number of button pushes: ");
  Serial.println(lightState);
} else {

  Serial.println("off");
}

delay(50);

}

lastButtonState = buttonState;

if (lightState = 0) {

digitalWrite(pinRed, HIGH);
digitalWrite(pinYellow, LOW);
digitalWrite(pinGreen, LOW);

} else if (lightState = 1) {

digitalWrite(pinRed, LOW);
digitalWrite(pinYellow, HIGH);
digitalWrite(pinGreen, LOW);

} else if (lightState = 2) {

digitalWrite(pinRed, LOW);
digitalWrite(pinYellow, LOW);
digitalWrite(pinGreen, HIGH);

}
}

Here's a way of doing it.

Simulation using LC_baseTools

-jim lee

1 Like

That is assignment, not test for equality. '=' vs. '==' There are other spots in your code as well

1 Like

Hello
Well I guess the sketch is calling for the usage of the switch/case instruction. Take a look for that.

Hi, @michaelowens23
Welcome to the forum.

To add code please click this link;

How have you got your button wired?
If between digital input and 5V, do you have a 10K resisitor between digital input and gnd?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

1 Like

I'm using 220 resistors

Hello
Inside the setup() function

Serial.begin(9600);

is missing.

How much experience do you have programming Arduinos?

For the button I'd change that to e.g. 10k or 4k7 or so. There's no need to run 23mA through a button just to read a logic high or low.

Hello
or you could configure the button pin with INPUT_PULLUP. In this way you will save the external pull up resitor and you have to inverse the digitalRead() of the button additional.

Hi,
Can you upload and run this code, tell me what happens.
All three LEDs should come ON when you press your button.
Do you have a DMM?

const int pinRed = 12;
const int pinYellow = 11;
const int pinGreen = 10;
const int button = 9;
int lastButtonState = 0;
int buttonState = 0;
int lightState = 0;

void setup()
{
  pinMode(pinRed, OUTPUT);
  pinMode(pinYellow, OUTPUT);
  pinMode(pinGreen, OUTPUT);
  pinMode(button, INPUT);

}

void loop()
{

  buttonState = digitalRead(button);

  if (buttonState = HIGH)
  {
    digitalWrite(pinRed, HIGH);
    digitalWrite(pinYellow, HIGH);
    digitalWrite(pinGreen, HIGH);
  }
  else
  {
    digitalWrite(pinRed, LOW);
    digitalWrite(pinYellow, LOW);
    digitalWrite(pinGreen, LOW);
  }

}

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

when I run this code all of the lights turn on without me pressing the button.

next to none, I started about 2 months ago and all of my knowledge comes from googling stuff. I only use arduino about twice a week, for 1 hour sessions each (I am a student)

I've resolved my problems, thank you all for your help.

Here is my finished code.

const int pinRed = 12;
const int pinYellow = 11;
const int pinGreen = 10;
const int button = 9;
int lastButtonState = 0;
int buttonState = 0;
int lightState = 0;

void setup() {
  pinMode(pinRed, OUTPUT);
  pinMode(pinYellow, OUTPUT);
  pinMode(pinGreen, OUTPUT);
  pinMode(button, INPUT);
  Serial.begin(9600);
}

void loop() {

  buttonState = digitalRead(button);

  if (buttonState != lastButtonState) {

    if (buttonState == HIGH) {

      lightState++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(lightState);
    } else {

      Serial.println("off");
    }

    delay(50);
  }

  lastButtonState = buttonState;

  if (lightState == 0) {

    digitalWrite(pinRed, HIGH);
    digitalWrite(pinYellow, LOW);
    digitalWrite(pinGreen, LOW);

  } else if (lightState == 1) {

    digitalWrite(pinRed, LOW);
    digitalWrite(pinYellow, HIGH);
    digitalWrite(pinGreen, LOW);

  } else if (lightState == 2) {

    digitalWrite(pinRed, LOW);
    digitalWrite(pinYellow, LOW);
    digitalWrite(pinGreen, HIGH);

  } else if (lightState > 2) {

    lightState = 0;
  }
}

Hi,
What was the problem?

Tom....

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