Skip to content

Instantly share code, notes, and snippets.

@mplacona
Created July 11, 2020 17:21
Show Gist options
  • Save mplacona/0f90fbbd72bdf00658dae65a35e987f3 to your computer and use it in GitHub Desktop.
Save mplacona/0f90fbbd72bdf00658dae65a35e987f3 to your computer and use it in GitHub Desktop.
#include <Bounce2.h>
#define BUTTON_PIN PB4
#define led1Pin PB0
#define led2Pin PB1
#define led3Pin PB2
#define led4Pin PB3
#define switchPin PB4
#define delayInterval 100
Bounce debouncer = Bounce(); // Instantiate a Bounce object
uint8_t val; // variable for reading the pin status
uint8_t val2; // variable for reading the delayed status
uint8_t buttonState; // variable to hold the mode switch state
int lightMode = 2;
void setup() {
PORTB |= (1 << PB4); // set PB4 as input & activate internal pull-up resistor for PB4
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(led3Pin, OUTPUT);
pinMode(led4Pin, OUTPUT);
}
void loop() {
val = digitalRead(switchPin); // read input value and store it in val
delay(10);
if (val != buttonState) {
if (val == HIGH) { // check if the mode button is pressed
if (lightMode == 0) {
lightMode = 1; // turn LEDs on
}
else {
if (lightMode == 1) { // if LEDs on
lightMode = 2; // make it flash
}
else {
if (lightMode == 2) { // if LEDs flashing
lightMode = 3; // make it wave
}
else {
if (lightMode == 3) { // if LEDs waving,
lightMode = 0; // turn LEDs off
}
}
}
}
}
buttonState = val; // save the new state in our variable
}
// Now do whatever the lightMode indicates
if (lightMode == 0) { // LEDs-off
PORTB |= (1 << PB1); // HIGH
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, LOW);
digitalWrite(led4Pin, LOW);
}
if (lightMode == 1) { // LEDs-on
digitalWrite(led1Pin, HIGH);
digitalWrite(led2Pin, HIGH);
digitalWrite(led3Pin, HIGH);
digitalWrite(led4Pin, HIGH);
}
if (lightMode == 2) { // LEDS-flashing
digitalWrite(led1Pin, HIGH);
digitalWrite(led2Pin, HIGH);
digitalWrite(led3Pin, HIGH);
digitalWrite(led4Pin, HIGH);
delay(100);
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, LOW);
digitalWrite(led4Pin, LOW);
delay(100);
}
if (lightMode == 3) { // LEDs-waving
digitalWrite(led4Pin, LOW);
digitalWrite(led1Pin, HIGH);
delay(100);
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, HIGH);
delay(100);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, HIGH);
delay(100);
digitalWrite(led3Pin, LOW);
digitalWrite(led4Pin, HIGH);
delay(100);
digitalWrite(led4Pin, LOW);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment