Skip to content

Instantly share code, notes, and snippets.

@eroeber
Created September 20, 2018 00:15
Show Gist options
  • Save eroeber/c4588e22612d0271b74f04ee8b9ada06 to your computer and use it in GitHub Desktop.
Save eroeber/c4588e22612d0271b74f04ee8b9ada06 to your computer and use it in GitHub Desktop.
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
// pint that the strip is connected to
#define PIN 7
// strip is a variable (instance of neopixel library)
// first argument is number of pixels, then pin number
Adafruit_NeoPixel strip = Adafruit_NeoPixel(5, 7, NEO_GRB + NEO_KHZ800);
// define button pins
const int button1 = 2;
const int button2 = 4;
int button1State = 0; // changing from 0 and 1 (digital)
int button2State = 0; // changing from 0 and 1 (digital)
void setup() {
// initialize the strip
strip.begin();
strip.show(); // sets all pixels to 'off'
// initialize buttons as input
pinMode(button1, INPUT);
pinMode(button2, INPUT);
// establish serial comm for debugging
Serial.begin(9600);
}
void loop() {
// first set the pixel color, then you show them
// setPixelColor(n, red, green, blue); n is the pixel #
// 0 is closest to the arduino
// r g b are values 0 - 255
// strip.setPixelColor(0, 0, 150, 150);
// strip.setPixelColor(2, 150, 0, 150);
// strip.show();
button1State = digitalRead(button1);
button2State = digitalRead(button2);
Serial.println(button2State);
// if button 1 is pressed, turn LEDs red/pink from bottom to top
if((button1State == HIGH) && (button2State == LOW)){
for(uint16_t i = 0; i < strip.numPixels(); i++){
strip.setPixelColor(i, 191, 10, 48);
strip.show();
delay(500);
}
}
// if button 2 is pressed, turn LEDs blue/teal from bottom to top
else if((button2State == HIGH) && (button1State == LOW)){
for(uint16_t i = 0; i < strip.numPixels(); i++){
strip.setPixelColor(i, 0, 128, 129);
strip.show();
delay(500);
}
}
// if both are pressed, turn LEDs purple/lavender from bottom to top
else if((button1State == HIGH) && (button2State == HIGH)){
for(uint16_t i = 0; i < strip.numPixels(); i++){
strip.setPixelColor(i, 107, 63, 160);
strip.show();
delay(500);
}
}
// else, all lights are off
else if((button1State == LOW) && (button2State == LOW)){
for(uint16_t i = 0; i < strip.numPixels(); i++){
strip.setPixelColor(i, 0, 0, 0);
strip.show();
delay(500);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment