Go Down

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

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 );
 }
 
}
"Could you do the egg bacon spam and sausage without the spam then? "

No PMs for help please.
DO NOT power servos from Arduino 5V: give them their own power and connect the grounds.

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 :)

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