Skip to content

Instantly share code, notes, and snippets.

@apetrone
Last active January 1, 2016 21:19
Show Gist options
  • Save apetrone/8202393 to your computer and use it in GitHub Desktop.
Save apetrone/8202393 to your computer and use it in GitHub Desktop.
2014 New Years Hat
/*
Date: December 2013
Author: Adam Petrone
New Year's Hat
*/
#include <Adafruit_NeoPixel.h>
const uint8_t NUM_PIXELS = 16;
const uint8_t PIXEL_DATA_PIN = 3;
const uint8_t NUM_COLORS = 6;
const uint8_t LOW_THRESHOLD = 600;
const uint8_t HIGH_THRESHOLD = 830;
const uint8_t FSR_PIN = 4;
const long COLOR_CHANGE_DELAY = 1000;
uint8_t max_led = 0;
const uint8_t NUM_BRIGHTNESS_LEVELS = 6;
uint8_t brightness[] = {
0, 16, 32, 64, 127, 255
};
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUM_PIXELS, PIXEL_DATA_PIN);
long next_tick = 0;
long next_color = millis() + COLOR_CHANGE_DELAY;
uint8_t value_below_threshold = 1;
long brightness_countdown = 300;
long current_brightness_level = 0;
uint8_t current_color = 0;
void setup()
{
pixels.begin();
pixels.setBrightness( brightness[ current_brightness_level ] );
pinMode(FSR_PIN, INPUT);
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
// (This code is lifted from one of Adafruit's examples)
uint32_t Wheel(Adafruit_NeoPixel & pixels, byte WheelPos) {
if(WheelPos < 85) {
return pixels.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if(WheelPos < 170) {
WheelPos -= 85;
return pixels.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return pixels.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}
void wheel()
{
long current_tick = millis();
if (current_brightness_level > 0)
{
if (current_tick >= next_color)
{
next_color = current_tick+COLOR_CHANGE_DELAY;
max_led++;
if (max_led >= pixels.numPixels())
{
max_led = 0;
current_color += 32;
if (current_color > 255)
{
current_color = 0;
}
}
}
}
uint8_t last_color = current_color-32;
if (current_color == 0)
{
last_color = 223;
}
for(int8_t p = 0; p < pixels.numPixels(); ++p)
{
if (p > max_led)
{
pixels.setPixelColor(pixels.numPixels()-p-1, Wheel(pixels, last_color));
}
else if (p == max_led)
{
pixels.setPixelColor(pixels.numPixels()-p-1, pixels.Color(255, 255, 255));
}
else
{
pixels.setPixelColor(pixels.numPixels()-p-1, Wheel(pixels, current_color));
}
}
pixels.show();
}
void loop()
{
wheel();
long current_tick = millis();
int value = analogRead(FSR_PIN);
if (value >= HIGH_THRESHOLD)
{
if (value_below_threshold)
{
if (current_tick >= next_tick)
{
// step down the brightness
next_tick = current_tick + brightness_countdown;
value_below_threshold = 0;
current_brightness_level++;
// wrap
if (current_brightness_level > (NUM_BRIGHTNESS_LEVELS-1))
{
current_brightness_level = 0;
}
if (current_brightness_level == 0)
{
current_color = 0;
max_led = 0;
next_color = current_tick+COLOR_CHANGE_DELAY;
}
pixels.setBrightness(brightness[current_brightness_level]);
pixels.show();
}
}
}
else if (value <= LOW_THRESHOLD)
{
value_below_threshold = 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment