Go Down

Topic: Bounce2 with more than one button (Read 240 times) previous topic - next topic

Symphonie

Hallo, I want to use more than one button, but I am not shure, how I must write the code! Can you please help me!?

Many thanks.

KenF

Code: [Select]

#define button1 10
#define button2 11

void setup()
{
pinMode(button1,INPUT_PULLUP);
pinMode(button2,INPUT_PULLUP);
}

void loop()
{
if (digitalRead(button1)==LOW) 
  {
   Serial.println("Blobby Blobby");   
  }

if (digitalRead(button2)==LOW) 
  {
   Serial.println("Bloop Bloop");   
  }
}

JimboZA

That doesn't show how to use the Bounce2 library for two buttons. This does:

Code: [Select]
#include <Bounce2.h>

/*
DESCRIPTION
====================
Simple example of the bounce library that switches the debug LED when a button is pressed.
Adapted for 2 switches by JimboZA July 2014

CIRCUIT
====================
https://raw.github.com/thomasfredericks/Bounce-Arduino-Wiring/master/Bounce/examples/circuit-bounce-change-duration-retrigger.png
*/


#define BUTTON_PINA 2
#define LED_PINA 12
#define BUTTON_PINB 3
#define LED_PINB 11



// Instantiate 2 Bounce object
Bounce debouncerA = Bounce();
Bounce debouncerB = Bounce();

void setup() {
  // Setup the buttons
  pinMode(BUTTON_PINA,INPUT_PULLUP);
  pinMode(BUTTON_PINB,INPUT_PULLUP);

 
  // After setting up the button, setup debouncer
  debouncerA.attach(BUTTON_PINA);
  debouncerA.interval(5);
  debouncerB.attach(BUTTON_PINB);
  debouncerB.interval(5);
 
  //Setup the LED
  pinMode(LED_PINA,OUTPUT);
    pinMode(LED_PINB,OUTPUT);
 
}

void loop() {
 // Update the debouncer
  debouncerA.update();
    debouncerB.update();
 
 // Get the update value
 int valueA = debouncerA.read();
  int valueB = debouncerB.read();
 
 // Turn on or off the LED
 if ( valueA == HIGH) {
   digitalWrite(LED_PINA, HIGH );
 } else {
    digitalWrite(LED_PINA, LOW );
 }
 
  if ( valueB == HIGH) {
   digitalWrite(LED_PINB, HIGH );
 } else {
    digitalWrite(LED_PINB, LOW );
 }
 
}
meArm build blog:     http://jimbozamearm.blogspot.com/

Please don't PM for technical advice. Firstly my advice is sometimes wrong, and second, in the forum you get a broader, more timely, range of responses.

KenF

That doesn't show how to use the Bounce2 library for two buttons. This does:
Aha! Bounce2 is a library.  I thought it was some kind of game :)

Symphonie

Many thanks for the example.

One question:

which pin uses "#define BUTTON_PINA 2"? Is that Pin 2? And the alphabetic character for the bounce is "A"? Is that correct?

Many thanks

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy