edit
(deleted)
The button is INPUT_PULLUP. That means it is LOW when pressed, and HIGH when unpressed.
IT really depends on the type of switch and how you have it wired. Why do we have to ask for a description of the switch and a schematic of how you have it all wired?
Paul
How is the switch wired? Is it like in the comment in the code (pull down, to ground, by resistor)? If so the pinMode(buttonPin, INPUT_PULLUP) is not appropriate. Should be just INPUT.
Thanks all for your answers.
At first, I have to say that I'm using ONLY the board, by connecting it to my PC and running via ENERGIA.
So I didnt wired up a thing, just connect it by a micro usb cable and run the button example.
Also, I cannot change that INPUT_PULLUP to INPUT_PULLDOWN, accordind to ENERGIA .
Even if it stay on INPUT, the greenLED keeps on, as you can see here
One solution did works, it was spycatcher2k's idea - thanks!
itaybm:
Thanks all for your answers.
At first, I have to say that I'm using ONLY the board, by connecting it to my PC and running via ENERGIA.
So I didnt wired up a thing, just connect it by a micro usb cable and run the button example.
Also, I cannot change that INPUT_PULLUP to INPUT_PULLDOWN, accordind to ENERGIA .
Even if it stay on INPUT, the greenLED keeps on, as you can see hereOne solution did works, it was spycatcher2k's idea - thanks!
An easy way to deal with this is to use defined or named constants to give meaning to the values you use. HIGH and LOW are better than 1 and 0, but are still not descriptive enough for what you want. It doesn't tell you whether HIGH or LOW activates or deactivates the attached circuit, and if you rewire the circuit to invert the logic it can be a pain to change all the instances in your sketch of the appropriate constant.
Add these to the top of your sketch:
#define BUTTON_PRESSED LOW
#define LED_LIT HIGH
Then you can rewrite the code like this:
if (buttonState == BUTTON_PRESSED ) {
// turn LED on:
digitalWrite(ledPin, LED_LIT );
}
else {
// turn LED off:
digitalWrite(ledPin, ! LED_LIT );
}
And now it's perfectly clear what the meaning of the values you are reading and writing from the pin are.
Thanks a lot!