Help with button project!?

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
1EAS1
 
Posts: 18
Joined: Mon Feb 04, 2013 10:06 pm

Help with button project!?

Post by 1EAS1 »

I want to use 13 buttons and each button displaying a different text/word. Theres already a example on the arduino website but for only 1 button, heres the code from the website (1 button)

Code: Select all

/* 
 Keyboard Button test
 
 For the Arduino Leonardo and Micro.
 
 Sends a text string when a button is pressed.
 
 The circuit:
 * pushbutton attached from pin 2 to +5V
 * 10-kilohm resistor attached from pin 4 to ground
 
 created 24 Oct 2011
 modified 27 Mar 2012
 by Tom Igoe
 
 This example code is in the public domain.
 
 http://www.arduino.cc/en/Tutorial/KeyboardButton
 */

const int buttonPin = 2;          // input pin for pushbutton
int previousButtonState = HIGH;   // for checking the state of a pushButton
int counter = 0;                  // button push counter

void setup() {
  // make the pushButton pin an input:
  pinMode(buttonPin, INPUT);
  // initialize control over the keyboard:
  Keyboard.begin();
}

void loop() {
  // read the pushbutton:
  int buttonState = digitalRead(buttonPin);
  // if the button state has changed, 
  if ((buttonState != previousButtonState) 
    // and it's currently pressed:
  && (buttonState == HIGH)) {
    // increment the button counter
    counter++;
    // type out a message
    Keyboard.print("You pressed the button ");
    Keyboard.print(counter); 
    Keyboard.println(" times.");
  }
  // save the current button state for comparison next time:
  previousButtonState = buttonState; 
}
and heres an image of the setup I had in mind, https://docs.google.com/file/d/0ByzaYWp ... sp=sharing
Does anyone know if this is possible? If so, how would you mode the code for it to work?
Thanks!

User avatar
attermann
 
Posts: 6
Joined: Sun Feb 24, 2013 3:41 pm

Re: Help with button project!?

Post by attermann »

I would suggest using the Bounce library (http://playground.arduino.cc/code/bounce). Not only does it provide a nice container for each button in your code, but it also performs "de-bouncing" so that you get reliable button-push indication.

The example code for this library, modified for your application, might look like the following.

Code: Select all

#include <Bounce.h>

// Instantiate Bounce objects with a 5 millisecond debounce time
Bounce bouncer1 = Bounce( 1, 5 );
Bounce bouncer2 = Bounce( 2, 5 );
...
Bounce bouncer12 = Bounce( 12, 5 );
Bounce bouncer13 = Bounce( 13, 5 );

void setup() {

  Serial.begin(9600);

  pinMode( 1, INPUT );
  pinMode( 2, INPUT );
  ...
  pinMode( 12, INPUT );
  pinMode( 13, INPUT );

}

void loop() {

 // Update the debouncer
  bouncer1.update( );
  bouncer2.update( );
  ...
  bouncer12.update( );
  bouncer13.update( );

  // Get the update value
  if ( bouncer1.read() ) {
    Serial.println( "You pressed button #1" );
  }
  if ( bouncer2.read() ) {
    Serial.println( "You pressed button #2" );
  }
  ...
  if ( bouncer12.read() ) {
    Serial.println( "You pressed button #12" );
  }
  if ( bouncer13.read() ) {
    Serial.println( "You pressed button #13" );
  }

}
It's also worth mentioning that you can typically simplify your circuit by using internal pull-up resistors and only wiring the switch between input and ground. There's a good how-to that describes this at http://www.instructables.com/id/Arduino ... -Tutorial/.

Good luck!

1EAS1
 
Posts: 18
Joined: Mon Feb 04, 2013 10:06 pm

Re: Help with button project!?

Post by 1EAS1 »

Thanks so much! I will post the results after the parts come in;)

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

Return to “Arduino”