[Solved] If statement always true when it clearly should not be

// moderator corrected code tags 
 const int led_glass = 2;
const int button_glass_waiting = 6;
const int button_glass_office = 8;
int val_glass_w_now = 0;
int val_glass_o_now = 0;
int val_glass_w_then = 0;
int val_glass_o_then = 0;
int led_glass_status = 0;

void setup ()
{
  pinMode (led_glass, OUTPUT);
  pinMode (button_glass_waiting, INPUT);
  pinMode (button_glass_office, INPUT);
}
void loop ()
  {
  val_glass_w_now = digitalRead(button_glass_waiting);
  if(val_glass_w_now != val_glass_w_then)
  {
    if(val_glass_w_now == HIGH)
    {
      led_glass_status = HIGH;
      digitalWrite(led_glass, led_glass_status);
    }
  }
  val_glass_w_then = val_glass_w_now;
}

I have been trying to debug this code for a while. In my circuit I have a button 6 for testing. When I upload it the led attached to pin 2 stays on no matter what. I have even removed the button and touched and untouched the wires together and still the led remains lit. The led will only go out if I specify that var_glass_w_then = var_glass_w_now + 1.

It sounds like you might be fighting a 'floating input pin condition'. Wiring push buttons to digital input pins requires either pull-up or pull-down resistor for proper operation. The easy way is to enable the internal pull-up resistor for the pins to be wired to switches and then wire the switches between ground and the input pin. Reading a low will mean the switch is pressed and a high means it's not pressed.

Lefty

It's [code] BTW, not .

I don't see anywhere that you turn the led off.

michinyon:
I don't see anywhere that you turn the led off.

The led should start off off until button is pressed, if I were to change where led_glass_status to LOW it would never be on

retrolefty:
It sounds like you might be fighting a 'floating input pin condition'. Wiring push buttons to digital input pins requires either pull-up or pull-down resistor for proper operation. The easy way is to enable the internal pull-up resistor for the pins to be wired to switches and then wire the switches between ground and the input pin. Reading a low will mean the switch is pressed and a high means it's not pressed.

Lefty

Okay, how do I enable the internal pull-up resistor?

maglax:
Okay, how do I enable the internal pull-up resistor?

pinMode (button_glass_waiting, INPUT_PULLUP);
pinMode (button_glass_waiting, INPUT_PULLUP);

This should be in place of pinmode(button_glass_waiting, INPUT); right?

const int led_glass = 2;
const int button_glass_waiting = 7;
const int button_glass_office = 8;
int val_glass_w_now = 0;
int val_glass_o_now = 0;
int val_glass_w_then = 0;
int val_glass_o_then = 0;
int led_glass_status = 0;

void setup ()
{
  pinMode(led_glass, OUTPUT);
  pinMode(button_glass_waiting, INPUT_PULLUP);
  pinMode(button_glass_office, INPUT_PULLUP);

}
void loop ()
{
  val_glass_w_now = digitalRead(button_glass_waiting);
  if(val_glass_w_now != val_glass_w_then)
  {
    if(val_glass_w_now == LOW)
    {
      led_glass_status = HIGH;
      digitalWrite(led_glass, led_glass_status);
  }
  }
  val_glass_w_then = val_glass_w_now;
    val_glass_o_now = digitalRead(button_glass_office);
  if(val_glass_o_now != val_glass_o_then)
  {
    if(val_glass_o_now == LOW)
    {
      led_glass_status = LOW;
      digitalWrite(led_glass, led_glass_status);
  }
  }
  val_glass_o_then = val_glass_o_now;
  
}

So I have re-added the off button but now with the inputs being INPUT_PULLUP the led would stay off, I tried having setting it to INPUT and INPUT_PULL separately but still to no avail.

I also tried removing _PULLUP and added a delay of 110 after each buttons code and the led blinked as if the if statements were being ignored except for the contents.

Delta_G:
How is your button wired?

Wire coming from 3.3volts to positive breadboard.
Wire from positive line to one side on breadboard(at J).
Button attached connecting two sides of the breadboard(connecting both sides of row 30 at E and F).
Wire coming off of opposite side of breadboard (at A) to pin 7 on arduino.

Not a button malfunction because when wires are touched before button same result is shown. Also not the arduino's fault I tried it on My uno and also on my Mega 2560. So it does appear to be code.

So, pin 7 to button, other side of button to +3.3v? Well, that's backwards if you want to use the internal pull-up resistor. You've got either pull-up high, or +3.3v high as your two possible states.

What you need to do is go from 7 to switch, other side of switch to ground, and turn on INPUT_PULLUP.

Like SirNikity says, button from pin 7 to GND, set INPUT_PULLUP, but don't forget that this make the pin LOW when the button is pressed.

lar3ry:
Like SirNikity says, button from pin 7 to GND, set INPUT_PULLUP, but don't forget that this make the pin LOW when the button is pressed.

Okay thank you this fixed it I did not have it set to ground I assumed it should still be on the 3.3V for pullup so thank you!

Will mark as solved if I can (:

maglax:

michinyon:
I don't see anywhere that you turn the led off.

The led should start off off until button is pressed, if I were to change where led_glass_status to LOW it would never be on

It stays off until you press the button, and then it never gets turned off again.