Skip to content

Instantly share code, notes, and snippets.

@peow2373
Created February 10, 2020 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peow2373/0e5247d921fb7598be09ac03d8a74053 to your computer and use it in GitHub Desktop.
Save peow2373/0e5247d921fb7598be09ac03d8a74053 to your computer and use it in GitHub Desktop.
Recognizes inputs from two different switch mechanisms and alters the color of a strip of Neopixels depending on which input is activated
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
#define BUTTON_PIN 6 // Digital Input from Breadboard
#define BOARD_PIN 5 // Digital Input from Breakout Board
#define PIXEL_PIN 2 // Digital Output connected to the NeoPixels.
#define PIXEL_COUNT 4 // Number of NeoPixels
// Declare our NeoPixel pixel object:
Adafruit_NeoPixel pixels(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pixels.begin(); // Initialize NeoPixel object
pixels.show(); // Initialize all pixels to 'off'
}
void loop() {
// Get current button state.
int newState1 = digitalRead(BUTTON_PIN);
int newState2 = digitalRead(BOARD_PIN);
// If the Breadboard button is pressed
if (newState1 == HIGH) {
// If the Breakout Board button is pressed too
if (newState2 == HIGH) {
for (int i = 0; i < PIXEL_COUNT; i++) {
pixels.setPixelColor(i, pixels.Color(255, 0, 0)); // Red
pixels.show();
}
}
// If only the Breadboard button is pressed
else {
for (int i = 0; i < PIXEL_COUNT; i++) {
pixels.setPixelColor(i, pixels.Color(0, 0, 255)); // Blue
pixels.show();
}
}
}
// If only the Breakout Board button is pressed
else if (newState2 == HIGH && newState1 == LOW) {
for (int i = 0; i < PIXEL_COUNT; i++) {
pixels.setPixelColor(i, pixels.Color(0, 255, 0)); // Green
pixels.show();
}
}
// If no buttons are pressed
else {
for (int i = 0; i < PIXEL_COUNT; i++) {
pixels.setPixelColor(i, pixels.Color(0, 0, 0)); // Black/off
pixels.show();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment