Created
July 15, 2018 08:13
-
-
Save Ellrion/d64cbb79177bdf8c4b0d8bfe4c2af3d7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Bounce2.h> | |
#define RED_PIN 9 | |
#define GREEN_PIN 10 | |
#define BLUE_PIN 11 | |
#define RED_BUTTON_PIN 5 | |
#define GREEN_BUTTON_PIN 3 | |
#define BLUE_BUTTON_PIN 4 | |
#define REMOVE_BUTTON_PIN 8 | |
Bounce rButton = Bounce(RED_BUTTON_PIN, 5); | |
Bounce gButton = Bounce(GREEN_BUTTON_PIN, 5); | |
Bounce bButton = Bounce(BLUE_BUTTON_PIN, 5); | |
int r = 0, g = 0, b = 0; | |
void setup() { | |
pinMode(RED_PIN, OUTPUT); | |
pinMode(GREEN_PIN, OUTPUT); | |
pinMode(BLUE_PIN, OUTPUT); | |
pinMode(RED_BUTTON_PIN, INPUT_PULLUP); | |
pinMode(GREEN_BUTTON_PIN, INPUT_PULLUP); | |
pinMode(BLUE_BUTTON_PIN, INPUT_PULLUP); | |
pinMode(REMOVE_BUTTON_PIN, INPUT_PULLUP); | |
Serial.begin(9600); | |
} | |
void loop() { | |
r = handleColorButton(RED_PIN, &rButton, r); | |
g = handleColorButton(GREEN_PIN, &gButton, g); | |
b = handleColorButton(BLUE_PIN, &bButton, b); | |
handleRemoveButton(); | |
} | |
int handleColorButton(int colorPin, Bounce *button, int color) { | |
if ((*button).update() && (*button).rose()) { | |
Serial.println("+"); | |
color = constrain(color + 10, 0, 255); | |
analogWrite(colorPin, color); | |
} | |
return color; | |
} | |
void handleRemoveButton() { | |
if (!digitalRead(REMOVE_BUTTON_PIN)) { | |
r = 0; | |
g = 0; | |
b = 0; | |
analogWrite(RED_PIN, r); | |
analogWrite(GREEN_PIN, g); | |
analogWrite(BLUE_PIN, b); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment