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

ksj

Blooded
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

Blooded
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.
 
Top