// 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.
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?
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.
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.