Skip to content

Instantly share code, notes, and snippets.

@WilliamHester
Created October 5, 2014 19:37
Show Gist options
  • Save WilliamHester/7847a52688ee4a58719c to your computer and use it in GitHub Desktop.
Save WilliamHester/7847a52688ee4a58719c to your computer and use it in GitHub Desktop.
Arduino
#include <Adafruit_NeoPixel.h>
#define PIN 1
const int buttonPin = 2;
const int pot1Pin = 3;
const int pot2Pin = 4;
int buttonState = 0;
int pot1Val = 0; // color select
int pot2Val = 0; // pulse speed select
int r = 0;
int g = 0;
int b = 0;
bool wasOn = false;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(32, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
strip.begin();
strip.show();
pinMode(buttonPin, INPUT);
pinMode(pot1Pin, INPUT);
pinMode(pot2Pin, INPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH && !wasOn) { // Turn it on
colorWipeOn(strip.Color(r, g, b), 10);
wasOn = true; // set the internal state to be on
}
else if (buttonState == LOW){ // Turn it off
wasOn = false; // set the internal state to off
colorWipeOff(strip.Color(0, 0, 0), 10);
}
else { // it's on now, so do its thing
pot1Val = analogRead(pot1Pin);
pot2Val = analogRead(pot2Pin);
pot2Val = map(pot1Val, 0, 1023, 0, 200); //scale pot value down to 1-100
if (pot1Val < 341) {
pot1Val = (pot1Val * 3) / 4; // scale back to 0-255
r = 256 - pot1Val; // Red from full to off
g = pot1Val; // Green from off to full
b = 1; // Blue off
}
else if (pot1Val < 682) // Middle third of potentiometer's range (341-681)
{
pot1Val = ( (pot1Val-341) * 3) / 4; // scale back to 0-255
r = 1; // Red off
g = 256 - pot1Val; // Green from full to off
b = pot1Val; // Blue from off to full
}
else // Upper third of potentiometer"s range (682-1023)
{
pot1Val = ( (pot1Val-683) * 3) / 4; // scale back to 0-255
r = pot1Val; // Red from off to full
g = 1; // Green off
b = 256 - pot1Val; // Blue from full to off
}
colorAll(strip.Color(r, g, b), 10);
delay (pot2Val);
colorAll(strip.Color(50, 50, 50), 10);
delay (pot2Val);
}
}
void colorAll(uint32_t c, uint8_t wait) {
uint16_t i;
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
}
strip.show();
delay(wait);
}
void colorWipeOn(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(10);
}
}
void colorWipeOff(uint32_t c, uint8_t wait) {
for(uint16_t i=strip.numPixels(); i>0; i--) {
strip.setPixelColor(i, c);
strip.setPixelColor(0, 0, 0, 0);
strip.show();
delay(10);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment