I've been learning how to encapsulate my code within functions, and one of the examples I worked on is a simple INPUT_PULLUP button. When I upload the sketch from the arduino website everything works, so I know it's not a hardware issue. However, with the code below the button will just not go LOW.
I just don't see what the issue is, and I'm feeling really hopeless that I can't even get the simple thing done. I would be grateful for any advice.
int button_pin = 2;
int button_state = 0;
void setup() {
Serial.begin(9600);
pinMode(button_pin, INPUT_PULLUP);
}
void loop() {
Button();
}
void Button() {
// Read button(s)
button_state = digitalRead(button_pin);
if (button_state = LOW) {
Serial.println(button_state);
};
};
==
(double equal sign). A single=
is an assignment. So you are actually assigning the value LOW tobutton_state
. And an assignment will return the value, that you assigned, in this case LOW, aka 0, aka false. Thus the if statement will never be entered