SparkFun Forums 

Where electronics enthusiasts find answers.

General project discussion / help
Did you make a robotic coffee pot which implements HTCPCP and decafs unauthorized users? Show it off here!
By JamesBryan
#198411
im building a tron legacy disc upgrade and i want to be able to change colours so that i can go from a blue "regular" program to a green "virus" to a red "military" disc and preferably while the blade spin animation is going. how do i code it so that the animations look for a selected colour {thearter chase (colour)} instead of {theater chase 0,0,255} (forgive the improper syntax here, my actual code is writen correctly as seen in my post momentarily) i am also using a second board to control the c ring but that only uses a colour wipe and only needs to change colour so i know all i have to do is modify it along the lines of how i have to modify my disc code. i know how to add the second button, having seen a tutorial on that, what i havent figured out yet is how to make the first button pick the colour and the animations look to THAT for what colour to be
well here is my code for the disc in case anyone wants to build a disc anyway



#include <Adafruit_DotStar.h> //Comment out if using NeoPixels



// This is a demonstration on how to use an input device to trigger changes on your neo pixels.
// You should wire a momentary push button to connect from ground to a digital IO pin. When you
// press the button it will change to a new pixel animation. Note that you need to press the
// button once to start the first animation!

//#include <Adafruit_NeoPixel.h> //commnt out if using DotStar

#ifdef __AVR__
#include <avr/power.h>
#endif


#define BUTTON_PIN 5 // Digital IO pin connected to the button. This will be
// driven with a pull-up resistor so the switch should
// pull the pin to ground momentarily. On a high -> low
// transition the button press logic will execute.

#define PIXEL_PIN 0 // Digital IO pin connected to the NeoPixels.

#define PIXEL_COUNT 45
#define NUMPIXELS 45
// set both of the above to the number of pixels in your strip
// Parameter 1 = number of pixels in strip, neopixel stick has 8
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_RGB Pixels are wired for RGB bitstream
// NEO_GRB Pixels are wired for GRB bitstream, correct for neopixel stick
// NEO_KHZ400 400 KHz bitstream (e.g. FLORA pixels)
// NEO_KHZ800 800 KHz bitstream (e.g. High Density LED strip), correct for neopixel stick
//Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
// Here's how to control the LEDs from any two pins:
#define DATAPIN 4
#define CLOCKPIN 3
Adafruit_DotStar strip = Adafruit_DotStar(
NUMPIXELS, DATAPIN, CLOCKPIN, DOTSTAR_BRG);

int head = 0, tail = -10; // Index of first 'on' and 'off' pixels
uint32_t color = 0xFF0000; // 'On' color (starts red)

void setup() {
#if defined (__AVR_ATtiny85__)
if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
pinMode(BUTTON_PIN, INPUT_PULLUP);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
startAnimation(); //we use () because we are not passing any parameters
}
int mode = 0; // Start in the "Off" mode
void loop() {

// check for a button press
if (digitalRead(5) == LOW) // button pressed
{
mode++; // advance to the next mode
if (mode == 5)
{
mode = 0; // go back to Off
}
}

switch (mode) // execute code for one of the 3 modes:
{
case 0:
colorWipe(strip.Color(0, 0, 0), 10);
break; // mode == OFF
// add code for the "Off" mode here
break;

case 1:
colorWipe(strip.Color(255, 0, 0), 1); // color order is GRB // mode == Ignition
// add code for the "Ignition" mode here
break;

case 2:
colorWipe(strip.Color(0, 255, 0), 15); // color order is GRB
// add code for the "Spinning" mode here
break;

case 3:
colorWipe(strip.Color(0, 0, 255), 1); // color order is GRB // mode == Ignition
// add code for the "Ignition" mode here
break;

case 4:
colorWipe(strip.Color(255, 255, 255), 1); // color order is GRB // mode == Ignition
// add code for the "Ignition" mode here
break;
}
}

// 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);
}
}

//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 (uint16_t i = 0; i < strip.numPixels(); i = i + 3) {
strip.setPixelColor(i + q, c); //turn every third pixel on
}
strip.show();

delay(wait);

for (uint16_t i = 0; i < strip.numPixels(); i = i + 3) {
strip.setPixelColor(i + q, 0); //turn every third pixel off
}
}
}
}

void startAnimation() {
for (int x = 0; x < NUMPIXELS * 3; x++) {
strip.setPixelColor(head, color); // 'On' pixel at head
strip.setPixelColor(tail, 0); // 'Off' pixel at tail
strip.show(); // Refresh strip
delay(20); // Pause 20 milliseconds (~50 FPS)

if (++head >= NUMPIXELS) { // Increment head index. Off end of strip?
head = 0; // Yes, reset head index to start
if ((color >>= 8) == 0) // Next color (R->G->B) ... past blue now?
color = 0xFF0000; // Yes, reset to red
}
if (++tail >= NUMPIXELS) tail = 0; // Increment, reset tail index
}
}