Skip to content

Instantly share code, notes, and snippets.

@natendaben
Created September 19, 2018 14:45
Show Gist options
  • Save natendaben/75e7f274f610a0a25b99a38e2dc5a30e to your computer and use it in GitHub Desktop.
Save natendaben/75e7f274f610a0a25b99a38e2dc5a30e to your computer and use it in GitHub Desktop.
A brief Neopixel strip demonstration using two simple buttons
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
//pin that the strip is connected to
#define PIN 4
//set some variables
const int firstbutton = 2;
const int secondbutton = 3;
const int lightstrip = 4;
//first argument is # pixels - change to how many you have
Adafruit_NeoPixel strip = Adafruit_NeoPixel(5, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
// put your setup code here, to run once:
//initialize strip
strip.begin();
strip.show(); //sets all pixels to "off"
//define which pins are inputs and which are outputs
pinMode(firstbutton, INPUT);
pinMode(secondbutton, INPUT);
pinMode(lightstrip, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
//first set pixel color, then show
//showPixelColor(n, red, green, blue); n=pixel number,
//0 is closest to the arduino
while(digitalRead(firstbutton)==HIGH && digitalRead(secondbutton)==LOW)
//while button one is pushed
{
for(int i=0; i<5; i++)
{
strip.setPixelColor(i, 255, 0, 0);
strip.show();
delay(100);
strip.setPixelColor(i, 0, 0, 0);
strip.show();
}
for(int i=4; i>-1; i--)
{
strip.setPixelColor(i, 255, 0, 0);
strip.show();
delay(100);
strip.setPixelColor(i, 0, 0, 0);
strip.show();
}
}
while(digitalRead(secondbutton)==HIGH && digitalRead(firstbutton)==LOW)
//while button two is pushed
{
strip.setPixelColor(0, 157, 0, 255);
strip.setPixelColor(1, 21, 0, 255);
strip.setPixelColor(2, 0, 131, 255);
strip.setPixelColor(3, 0, 255, 221);
strip.setPixelColor(4, 33, 255, 0);
strip.show();
}
while(digitalRead(firstbutton)==HIGH && digitalRead(secondbutton)==HIGH)
//while both buttons are pushed
{
strip.setPixelColor(0, 182, 0, 255);
strip.show();
delay(125);
strip.setPixelColor(1, 255, 0, 246);
strip.show();
delay(125);
strip.setPixelColor(2, 255, 0, 102);
strip.show();
delay(125);
strip.setPixelColor(3, 255, 38, 0);
strip.show();
delay(125);
strip.setPixelColor(4, 255, 161, 0);
strip.show();
}
//reset colors at the end of each button press
for(int i=0; i<5; i++)
{
strip.setPixelColor(i, 0, 0, 0);
}
strip.show();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment