Adafruit Feather M0 Adalogger button press state change

Breakout boards, sensors, other Adafruit kits, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
pripri804
 
Posts: 5
Joined: Tue Jul 26, 2016 10:37 am

Adafruit Feather M0 Adalogger button press state change

Post by pripri804 »

Hi,

I'm trying to get the led to change it's state when the button is pressed on my feather M0, but somehow it is not working.

This is the circuit
Image

This is the code:

Code: Select all

const int  buttonPin = 2;    // the pin that the pushbutton is attached to
 const int ledPin = 13;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
   // initialize the button pin as a input:
   pinMode(buttonPin, INPUT);
   // initialize the LED as an output:
   pinMode(ledPin, OUTPUT);
   // initialize serial communication:
   Serial.begin(9600);
}


void loop() {
   // read the pushbutton input pin:
   buttonState = digitalRead(buttonPin);

   // compare the buttonState to its previous state
   if (buttonState != lastButtonState) {
     // if the state has changed, increment the counter
     if (buttonState == HIGH) {
       // if the current state is HIGH then the button
       // wend from off to on:
       buttonPushCounter++;
       Serial.println("on");
       Serial.print("number of button pushes:  ");
       Serial.println(buttonPushCounter);
     } else {
       // if the current state is LOW then the button
       // wend from on to off:
       Serial.println("off");
     }
     // Delay a little bit to avoid bouncing
     delay(50);
   }
   // save the current state as the last state,
   //for next time through the loop
   lastButtonState = buttonState;


   // turns on the LED every four button pushes by
   // checking the modulo of the button push counter.
   // the modulo function gives you the remainder of
   // the division of two numbers:
   if (buttonPushCounter % 4 == 0) {
     digitalWrite(ledPin, HIGH);
   } else {
     digitalWrite(ledPin, LOW);
   }

}
What could be the issue ?

User avatar
adafruit_support_bill
 
Posts: 88141
Joined: Sat Feb 07, 2009 10:11 am

Re: Adafruit Feather M0 Adalogger button press state change

Post by adafruit_support_bill »

What exactly does it do when you press the button? What is the output on the serial monitor?

User avatar
pripri804
 
Posts: 5
Joined: Tue Jul 26, 2016 10:37 am

Re: Adafruit Feather M0 Adalogger button press state change

Post by pripri804 »

Nothing - nothing happens.

However, I tried outputting the value of digitalRead(buttonPin) onto the serial monitor and I've noticed that it returns 1 if the button is connected and 0 if not. Pushing the button doesn't affect this at all, it still returns 1 regardless of the button's state.

User avatar
adafruit_support_bill
 
Posts: 88141
Joined: Sat Feb 07, 2009 10:11 am

Re: Adafruit Feather M0 Adalogger button press state change

Post by adafruit_support_bill »

It sounds like either a bad button, or you have it wired wrong. Test the button out of the circuit with a multimeter. Typically with a 4-pin button, the pins are connected internally in 2 sets of 2.

User avatar
pripri804
 
Posts: 5
Joined: Tue Jul 26, 2016 10:37 am

Re: Adafruit Feather M0 Adalogger button press state change

Post by pripri804 »

I have tested the button and I've made sure that the board is wired correctly. Now I've also found out that whether or not the button is plugged in, the println still keeps on outputting the same value. (1)

User avatar
adafruit_support_bill
 
Posts: 88141
Joined: Sat Feb 07, 2009 10:11 am

Re: Adafruit Feather M0 Adalogger button press state change

Post by adafruit_support_bill »

Please post an actual photo of your circuit.

User avatar
pripri804
 
Posts: 5
Joined: Tue Jul 26, 2016 10:37 am

Re: Adafruit Feather M0 Adalogger button press state change

Post by pripri804 »

Here they are:
IMG_03.jpg
IMG_03.jpg (440.56 KiB) Viewed 732 times
IMG_02.jpg
IMG_02.jpg (629 KiB) Viewed 732 times
IMG_01.jpg
IMG_01.jpg (390.37 KiB) Viewed 732 times

User avatar
adafruit_support_bill
 
Posts: 88141
Joined: Sat Feb 07, 2009 10:11 am

Re: Adafruit Feather M0 Adalogger button press state change

Post by adafruit_support_bill »

It looks like you have the switch connected to digital pin 16 or 17, but your code is reading from pin 2.

Image

User avatar
pripri804
 
Posts: 5
Joined: Tue Jul 26, 2016 10:37 am

Re: Adafruit Feather M0 Adalogger button press state change

Post by pripri804 »

Hi, yes the problem was actually a wrong pin number in my code. Thank you, the program is now functioning as intended.

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Products from Adafruit”