Debouncer for arduino using 2 buttons for 2 leds (each button triggers a different led)

ksj

Well-Known Member
C-like:
// Include the Bounce2 library found here :
// https://github.com/thomasfredericks/Bounce2
#include <Bounce2.h>
#define BUTTON_PIN_1 2
#define BUTTON_PIN_2 7
#define LED_PIN 3
#define LED_PIN1 4
int ledState = LOW; Bounce debouncer1 = Bounce(); Bounce debouncer2 = Bounce();

void setup() {
  pinMode(BUTTON_PIN_1,INPUT_PULLUP); debouncer1.attach(BUTTON_PIN_1); debouncer1.interval(5);   
  pinMode(BUTTON_PIN_2,INPUT_PULLUP); debouncer2.attach(BUTTON_PIN_2); debouncer2.interval(25);
  pinMode(LED_PIN,OUTPUT); pinMode(LED_PIN1,OUTPUT); digitalWrite(LED_PIN1,ledState);
}
void loop() {
  debouncer1.update(); debouncer2.update(); int value1 = debouncer1.read(); int value2 = debouncer2.read();
  if ( value1 == LOW ) { for (int i = 255; i >= 0; i--) { analogWrite(LED_PIN, i);  } } else { digitalWrite(LED_PIN, LOW ); }
  if ( debouncer2.fell() ) { ledState = !ledState; digitalWrite(LED_PIN1,ledState); }
}
 

ksj

Well-Known Member
C-like:
// Include the Bounce2 library found here :
// https://github.com/thomasfredericks/Bounce2
#include <Bounce2.h>
#define BUTTON_PIN_1 2
#define BUTTON_PIN_2 7
#define LED_PIN 3
#define LED_PIN1 4
int ledState = LOW; Bounce debouncer1 = Bounce(); Bounce debouncer2 = Bounce();

void setup() {
  pinMode(BUTTON_PIN_1,INPUT_PULLUP); debouncer1.attach(BUTTON_PIN_1); debouncer1.interval(5);  
  pinMode(BUTTON_PIN_2,INPUT_PULLUP); debouncer2.attach(BUTTON_PIN_2); debouncer2.interval(25);
  pinMode(LED_PIN,OUTPUT); pinMode(LED_PIN1,OUTPUT); digitalWrite(LED_PIN1,ledState);
}
void loop() {
  debouncer1.update(); debouncer2.update(); int value1 = debouncer1.read(); int value2 = debouncer2.read();
  if ( value1 == LOW ) { for (int i = 255; i >= 0; i--) { analogWrite(LED_PIN, i);  } } else { digitalWrite(LED_PIN, LOW ); }
  if ( debouncer2.fell() ) { ledState = !ledState; digitalWrite(LED_PIN1,ledState); }
}

This uses a Bounce2 library available off of the arduino libraries. I will include it once I get a chance as libraries have a way of magically disappearing over time.

Pin 7 is a on/off trigger so you press it once it stays on, press it again and it switches off. Pin 2 is trigger for a simple on off say a laser fire.

This is a simple light effects setup.
 

Your message may be considered spam for the following reasons:

  1. Your new thread title is very short, and likely is unhelpful.
  2. Your reply is very short and likely does not add anything to the thread.
  3. Your reply is very long and likely does not add anything to the thread.
  4. It is very likely that it does not need any further discussion and thus bumping it serves no purpose.
  5. Your message is mostly quotes or spoilers.
  6. Your reply has occurred very quickly after a previous reply and likely does not add anything to the thread.
  7. This thread is locked.
Top