Hello everyone.
I am new to the community of arduino, but I am not new to Arduino world.
I have got an issue which seems to be really simple but I cannot solve.
I have a board called ArduPLC Micro, which is compatible (when coding) with board Arduino Micro; I wired a push button embedding a 5V LED and a built-in resistor (Rugged Metal Pushbutton with White LED Ring [16mm White Momentary] : ID 558 : $4.95 : Adafruit Industries, Unique & fun DIY electronics and kits) in the following way:
the contact C of the push button is connected to the ground and the contact NO is in the digital I/O IO1 [PIN0].
The embedded LED has the - to GND and the + wired in IO2 [PIN9] set as output.
the board embeds 4K7 ohm pull-up resistors for the digital I/O set as inputs, so PIN0 has the pull-up resistor enabled via a jumper.
when I push the momentary button, I notice that it takes the "command" only if I press and hold the push button for 1s or so. If I click the button without holding, the program doesn't start, and the LED doesn't turn on (or turn off, depending on previous state).
apart from this the rest of the program works well..so the only problem I can't solve is this.
Here you can find the code:
(if this is not enough please let me know)
int buttonState;
int lastButtonState = LOW;
int counter = 0; /* counts if the push button has been clicked and even or odd number of times*/
/* momentary push button NO contact wired in IO1 [PIN0] with internal pull-up resistor in the micro ATmega enabled, and external 4K7 ohm pull-up resistor enabled. Contact "C" of the push button is wired in GND */
#define button 0
#define led 9
void setup() {
pinMode(button, INPUT_PULLUP);
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
}
void loop(){
buttonState = digitalRead(button);
if(buttonState != lastButtonState){
if(buttonState == LOW){
counter = counter + 1;
}
}
lastButtonState = buttonState;
// if the counter is odd, then the button went from OFF to ON
if(counter%2 == 1){
digitalWrite(led, HIGH);
/* DO STUFF */
}
if(counter%2 == 0){
digitalWrite(led, LOW);
/* DO OTHER STUFF */
}
}
If I did anything wrong, or if you have any advice I am glad to learn better and more.
thank you