Alright everyone, I've got a lot of cool stuff cooking up over here, if you'd just like to turn your heads, uhhhhhhh:
Code: Select all#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIXELDATAPIN 6
#define NUMPIXELS 60
// 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(NUMPIXELS, PIXELDATAPIN, NEO_GRB + NEO_KHZ800);
// 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.
const int buttonPin = 2; //The input pin is where the pushbutton switch is located
int val; //This variable stores the state of the input pin
int buttonState = 0; //Store the state of the switch
int flashMode = 0; //0 means the LEDs are doing red comets...
//...and 1 means the LEDs are flashing hues of blue and white
int delayval; //How quick will blue and white flash?
int r, g, b;
int low = 0; //The bottom of the color range
int range = 255; //The spread of the color range
int delayshort = 20; //Fast flashing, 20 ms delay
int delaylong = 50; //Slower flashing, 40 ms delay
// NOTE
// NUMBER_COMETS x (COMET_LENGTH + COMET_SPACING) must be <= TOTAL_COMET_PIXELS
// The number of pixels in each comet
#define COMET_LENGTH 12
// The number of comets
#define NUMBER_COMETS 3
// The number of pixels between each comet
#define COMET_SPACING 8
// The total number of pixels in the strip dedicated to comets
#define TOTAL_COMET_PIXELS 60
// The direction of the comet 0 or 1
#define COMET_DIRECTION 1
// The max comet brightness
#define MAX_COMET_BRIGHTNESS 255
// The comet speed
#define SPEED 60
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
pinMode(buttonPin, INPUT); //Tell the Arduino the pushbutton is an input
Serial.begin(9600); //open the serial port at 9600 bps:
strip.begin(); //Initialize the NeoPixel library
strip.show(); //Initialize all pixels to 'off'
buttonState = digitalRead(buttonPin); //Read the initial state of the pushbutton
randomSeed(analogRead(0));
InitComet();
}
byte rgbyRedLevels[NUMPIXELS];
void InitComet(){
int iIndex = 0;
byte byMasterPos = 0;
byte byCometIndex = 0;
memset(rgbyRedLevels, 0, NUMPIXELS);
for (byCometIndex = 0; byCometIndex < NUMBER_COMETS; byCometIndex++){
#if COMET_DIRECTION
byte byBrightness = 0;
#else
byte byBrightness = MAX_COMET_BRIGHTNESS;
#endif
// Dim comet by length
byte byDim = MAX_COMET_BRIGHTNESS / COMET_LENGTH;
for (iIndex = 0; iIndex < COMET_LENGTH; iIndex++){
rgbyRedLevels[byMasterPos++] = byBrightness;
#if COMET_DIRECTION
byBrightness += byDim;
#else
byBrightness -= byDim;
#endif
}
byMasterPos += COMET_SPACING;
}
}
#if COMET_DIRECTION
void ProcessCometsDir(){
byte iIndex = 0;
byte byLast = 0;
byte byDestPixel = 0;
byte byOld = 0;
byLast = rgbyRedLevels[TOTAL_COMET_PIXELS - 1]; // Preserve the contents of the last pixel
for (iIndex = TOTAL_COMET_PIXELS - 1; iIndex > 0; iIndex--){
rgbyRedLevels[iIndex] = rgbyRedLevels[iIndex - 1];
}
rgbyRedLevels[0] = byLast; // Put what was the last pixel intio the first
for (iIndex = 0; iIndex <TOTAL_COMET_PIXELS; iIndex++){
strip.setPixelColor(iIndex, rgbyRedLevels[iIndex], 0, 0);
}
strip.show();
delay(SPEED);
}
#else
void ProcessCometsDir()
{
byte iIndex = 0;
byte byLast = 0;
byte byDestPixel = 0;
byte byOld = 0;
byLast = rgbyRedLevels[0]; // Preserve the contents of the last pixel
for (iIndex = 0; iIndex < TOTAL_COMET_PIXELS - 1; iIndex++)
{
rgbyRedLevels[iIndex] = rgbyRedLevels[iIndex + 1];
}
rgbyRedLevels[TOTAL_COMET_PIXELS - 1] = byLast; // Put what was the last pixel intio the first
for (iIndex = 0; iIndex <TOTAL_COMET_PIXELS; iIndex++)
{
strip.setPixelColor(iIndex, rgbyRedLevels[iIndex], 0, 0);
}
strip.show();
delay(SPEED);
}
#endif
void BlueWhiteFlashing() {
//Randomly choose the color the NeoPixels will have while the pushbutton switch is pressed
//Take that color and apply it to all NeoPixels
r = low + random(0, 100); //The red channel. Brightness goes between 0 (off) and its
//contribution to white, 100.
g = r; //The green channel, same as red
b = random(25, 150); //The blue channel. Want to have a lot of blues.
//For a set of NeoPixels the first NeoPixel is 0, the second is 1, all the way up to
//the count of NeoPixels, minus one.
for (int i = 0; i < NUMPIXELS; i++) {
//RGB values from a minimum of (0, 0, 0) up to a maximum of (255, 255, 255)
strip.setPixelColor(i, strip.Color(r, g, b)); //Pulse the NeoPixels, hues of blue
}
strip.show(); //Send the updated pixel color out to the NeoPixel ring
delayval = random(delayshort, delaylong);
delay(delayval); //Delay for a period of time before the next flash, in milliseconds
}
void loop() {
val = digitalRead(buttonPin); //Read the state of the pushbutton and store it in val
if (val == HIGH) { //If the button is not pressed down...
flashMode = 0; //...then we should be seeing red comets
} else if (val == LOW) { //But if the button IS pressed down
flashMode = 1; //...we need to queue up the blue and white flashes.
}
if (flashMode == 0) {
ProcessCometsDir();
}
if (flashMode == 1) { //When the button IS pressed down and flashMode is on...
BlueWhiteFlashing(); //...show blue and white flashes.
}
buttonState = val; //Save the pushbutton's new state as our variable so we can check it on the next loop.
}
I took some of tommysb's code from above and added in a random flashing of the NeoPixels between white and different hues of blue.
***Hook the NeoPixel data line up to pin 6 on your Uno.
***To initiate the flashing, you need to have a momentary pushbutton switch connected to pin 2 on your Uno. Don't forget the appropriate resistors coming off your switch: a 10 kiloohm one between your switch and +5V, and a 100 ohm one between your switch and pin 2, with a lead to GND on the same pin side as the 10 kiloohm resistor, like this
Mind you, I only tested this code with a NeoPixel 60 ring, so I haven't seen its behavior with the Jewel added in. It shouldn't be a problem, just change NUMPIXELS to 67 instead of 60 and hook up the ring and Jewel in series through their "data in" and "data out" soldering pads.
I'd love feedback about this sketch, and many thanks to tommyb for doing the heavy lifting on the red comet part of it.
EDIT: Crud. I forgot about the rainbow startup part. Will add that in later, but I wanted you to have this part of the code now since I know you're under a time crunch.
"Baby, if I knew what it was, I wouldn't say weird sparking thing."