Created
December 7, 2015 06:34
-
-
Save bholzer/cdbe2383b9a47c8fbe95 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Adafruit_NeoPixel.h> | |
#ifdef __AVR__ | |
#include <avr/power.h> | |
#endif | |
#define PIN 6 | |
// Parameter 1 = number of pixels in strip | |
// Parameter 2 = Arduino pin number (most are valid) | |
// Parameter 3 = pixel type flags, add together as needed: | |
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs) | |
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers) | |
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products) | |
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2) | |
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800); | |
const int numReadings = 60; | |
//400 great | |
int readings[numReadings]; // the readings from the analog input | |
int readIndex = 0; // the index of the current reading | |
int total = 0; // the running total | |
int average = 0; // the average | |
int inputPin = 0; | |
int k = 0; | |
// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across | |
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input | |
// and minimize distance between Arduino and first pixel. Avoid connecting | |
// on a live circuit...if you must, connect GND first. | |
void setup() { | |
// This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket | |
#if defined (__AVR_ATtiny85__) | |
if (F_CPU == 16000000) clock_prescale_set(clock_div_1); | |
#endif | |
// End of trinket special code | |
Serial.begin(9600); | |
while (!Serial) { | |
; // wait for serial port to connect. Needed for Leonardo only | |
} | |
for (int thisReading = 0; thisReading < numReadings; thisReading++) { | |
readings[thisReading] = 0; | |
} | |
//Serial.println("ASCII Table ~ Character Map"); | |
strip.begin(); | |
for (int i = 0; i < 3; i++) { | |
rainbowCycle(0); | |
} | |
strip.show(); // Initialize all pixels to 'off' | |
pinMode(inputPin, INPUT_PULLUP); | |
} | |
int previousVoltage = 0; | |
void loop() { | |
//int voltage = analogRead(inputPin) / 4; | |
total = total - readings[readIndex]; | |
// read from the sensor: | |
readings[readIndex] = analogRead(inputPin); | |
// add the reading to the total: | |
total = total + readings[readIndex]; | |
// advance to the next position in the array: | |
readIndex = readIndex + 1; | |
// if we're at the end of the array... | |
if (readIndex >= numReadings) { | |
// ...wrap around to the beginning: | |
readIndex = 0; | |
} | |
// calculate the average: | |
average = total / numReadings; | |
//int voltage = average / 4; | |
int voltage = average; | |
if (voltage != previousVoltage) { | |
int difference = (voltage - previousVoltage); | |
int interval = (abs(difference)/2)+1; | |
if (difference > 0) { | |
//New voltage is higher, step there | |
for (int i = previousVoltage; i < voltage; i+=interval) { | |
fillStrip(strip.Color(i,i,i)); | |
} | |
} else { | |
for (int i = previousVoltage; i > voltage; i-=interval) { | |
fillStrip(strip.Color(i,i,i)); | |
} | |
} | |
previousVoltage = voltage; | |
int red = Wheel(voltage+30); | |
int green = Wheel(voltage); | |
int blue = Wheel(voltage+10); | |
double brightness = (1.0 - map(voltage, 0, 40, 0, 255)/255.0); | |
fillStrip(strip.Color(red*brightness, green*brightness, blue*brightness)); | |
delay(5); | |
} | |
} | |
void clearStrip() { | |
fillStrip(strip.Color(0,0,0)); | |
strip.show(); | |
} | |
void fillStrip(uint32_t c) { | |
for(uint16_t i=0; i<strip.numPixels(); i++) { | |
strip.setPixelColor(i, c); | |
} | |
strip.show(); | |
} | |
void fillFromEnds(uint32_t c) { | |
} | |
void alternateColors(uint32_t c1, uint32_t c2) { | |
for(int16_t i=0; i<strip.numPixels(); i++) { | |
//Serial.println("Inner Pixel number: "); | |
//strip.setPixelColor(i, c2); | |
for(int j = 0; j<strip.numPixels(); j++) { | |
//Serial.println("Inner Pixel number: " + String(j)); | |
int modOuter = i % 2; | |
int modInner = j % 3; | |
//Serial.println(mod); | |
if ((modOuter == 0 && modInner == 2) || (modOuter == 1 && modInner == 1)) { | |
strip.setPixelColor(j, c1); | |
//Serial.println("Color 1"); | |
//Serial.println("On"); | |
//delay(wait); | |
} else { | |
strip.setPixelColor(j, c2); | |
//Serial.println("Color 2"); | |
//strip.show(); | |
//Serial.println("Off"); | |
//delay(wait); | |
} | |
delay(2); | |
//strip.show(); | |
} | |
strip.show(); | |
} | |
//strip.show(); | |
} | |
// Fill the dots one after the other with a color | |
void colorSnake(uint32_t c, uint32_t leng, uint8_t wait) { | |
int16_t half; | |
half = leng / 2; | |
//Serial.println("half: " + String(half)); | |
for(int16_t i=0; i<strip.numPixels(); i++) { | |
//Serial.println("Pixel number: " + String(i)); | |
int16_t virtualMin = 0; | |
//int16_t virtualNax = 0 - half; | |
//Serial.println(String(virtualMin)); | |
for(virtualMin; virtualMin<strip.numPixels(); virtualMin++) { | |
//Serial.println("Inner Pixel number: " + String(l)); | |
if(virtualMin >= 0) { | |
if (virtualMin > (i - half) && virtualMin< (i + half) && virtualMin >= 0) { | |
strip.setPixelColor(virtualMin, c); | |
//Serial.println("On"); | |
//delay(wait); | |
} else { | |
strip.setPixelColor(virtualMin, strip.Color(0, 0, 0)); | |
//strip.show(); | |
//Serial.println("Off"); | |
//delay(wait); | |
} | |
} | |
//strip.show(); | |
} | |
//strip.setPixelColor(i, c); | |
strip.show(); | |
delay(wait); | |
} | |
} | |
// Fill the dots one after the other with a color | |
void colorWipe(uint32_t c, uint8_t wait) { | |
for(uint16_t i=0; i<strip.numPixels(); i++) { | |
strip.setPixelColor(i, c); | |
strip.show(); | |
delay(wait); | |
} | |
} | |
void rainbow(uint8_t wait) { | |
uint16_t i, j; | |
for(j=0; j<256; j++) { | |
for(i=0; i<strip.numPixels(); i++) { | |
strip.setPixelColor(i, Wheel((i+j) & 255)); | |
} | |
strip.show(); | |
delay(wait); | |
} | |
} | |
// Slightly different, this makes the rainbow equally distributed throughout | |
void rainbowCycle(uint8_t wait) { | |
uint16_t i, j; | |
for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel | |
for(i=0; i< strip.numPixels(); i++) { | |
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255)); | |
} | |
strip.show(); | |
delay(wait); | |
} | |
} | |
//Theatre-style crawling lights. | |
void theaterChase(uint32_t c, uint8_t wait) { | |
for (int j=0; j<10; j++) { //do 10 cycles of chasing | |
for (int q=0; q < 3; q++) { | |
for (int i=0; i < strip.numPixels(); i=i+3) { | |
strip.setPixelColor(i+q, c); //turn every third pixel on | |
} | |
strip.show(); | |
delay(wait); | |
for (int i=0; i < strip.numPixels(); i=i+3) { | |
strip.setPixelColor(i+q, 0); //turn every third pixel off | |
} | |
} | |
} | |
} | |
//Theatre-style crawling lights with rainbow effect | |
void theaterChaseRainbow(uint8_t wait) { | |
for (int j=0; j < 256; j++) { // cycle all 256 colors in the wheel | |
for (int q=0; q < 3; q++) { | |
for (int i=0; i < strip.numPixels(); i=i+3) { | |
strip.setPixelColor(i+q, Wheel( (i+j) % 255)); //turn every third pixel on | |
} | |
strip.show(); | |
delay(wait); | |
for (int i=0; i < strip.numPixels(); i=i+3) { | |
strip.setPixelColor(i+q, 0); //turn every third pixel off | |
} | |
} | |
} | |
} | |
// Input a value 0 to 255 to get a color value. | |
// The colours are a transition r - g - b - back to r. | |
uint32_t Wheel(byte WheelPos) { | |
WheelPos = 255 - WheelPos; | |
if(WheelPos < 85) { | |
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3); | |
} | |
if(WheelPos < 170) { | |
WheelPos -= 85; | |
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3); | |
} | |
WheelPos -= 170; | |
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment