Hello everyone,
I made an LED costume that I control via xbee on a keyboard press . My problem is I want the light pattern to change to the next pattern as soon as I send a keystroke, but the longer light patterns won’t change until they cycle through to the end of the pattern. How can I break the longer patterns so the pattern will change as soon as state changes?
[code]
#include <BLIP_LEDS_SPI_LPD6803.h>
#define NUM_LEDS 40 // Set the number of LEDs in use here
int state = 0;
int colorDelay = 25;
void setup()
{
BL.setLeds(NUM_LEDS); // Initialisation functions
BL.init(); // SPI interrupt will now send out data to LEDs. This happens in the background, pretty fast.
BL.gridHeight = 4; // Grid dimensions for LINE and BOX effect functions are defined here. Also used for Spectrum Analyzer function.
BL.gridWidth = 11;
// Set all the LEDs to black (off)
BL.setRange(0, NUM_LEDS, BL.color(0, 0, 0));
BL.show();
Serial.begin(9600);
}
void loop()
{
if (Serial.available()) {
int characterRead = Serial.read();
if (characterRead == 'a') {
state = 1;
}
if (characterRead == 's') {
state = 2;
}
if (characterRead == 'd') {
state = 3;
}
if (characterRead == 'f') {
state = 4;
}
if (characterRead == 'g') {
state = 5;
}
if (characterRead == 'h') {
state = 6;
}
if (characterRead == 'p') {
state = 0;
}
}
useSerialData();
}
//***********************************************************************************************************
void useSerialData() {
unsigned int Counter, Counter2, Counter3;
switch (state) {
case 0:
blank();
break;
case 1: //random white blinks
BL.setPixel(random(0, NUM_LEDS), 31, 31, 31);
BL.show();
delay(15);
blank();
break;
case 2:
// Strobe Effect
strobe(45);
break;
case 3: //random red blinks
BL.setPixel(random(0, NUM_LEDS), 0, 31, 0);
BL.show();
delay(15);
blank();
break;
case 4:
// Color Wipe Effect
ColorUp(BL.color(random(0, 32), random(0, 32), random(0, 32)));
delay(1000);
break;
case 5:
for (Counter = 0; Counter < 96 ; Counter++) // Color Wheel Effect
{
for (Counter2 = 0; Counter2 < NUM_LEDS; Counter2++)
{
BL.setPixel(Counter2, BL.Wheel((Counter) % 96));
Counter3 += (96 / NUM_LEDS);
}
BL.show();
delay(colorDelay);
}
break;
case 6:
for (Counter = 0; Counter < 96 ; Counter++) // Scrolling Rainbow Effect
{
for (Counter2 = 0; Counter2 < NUM_LEDS; Counter2++)
{
BL.setPixel(Counter2, BL.Wheel((Counter2 * 2 + Counter) % 96));
Counter3 += (96 / NUM_LEDS);
}
BL.show();
delay(25);
}
break;
default:
blank();
}
}
void blank()
{
BL.setRange(0, NUM_LEDS, BL.color(0, 0, 0));
BL.show();
}
void ColorUp( unsigned int ColorToUse)
{
byte Counter;
for (Counter = 0; Counter < NUM_LEDS; Counter++)
{
BL.setPixel(Counter, ColorToUse);
BL.show();
delay(30);
}
}
void ON_Fade ()
{
byte Counter;
unsigned int a = 0xff, b = 0xff, c = 0xff;
for (Counter = 0; Counter < NUM_LEDS; Counter++)
{
BL.setPixel(Counter, BL.color(31, 31, 31));
}
BL.show();
delay(10);
for (int i = 0; i < 31; i++)
{
for (Counter = 0; Counter < NUM_LEDS; Counter++)
{
BL.setPixel(Counter, BL.color(a, b, c));
}
BL.show();
delay(10);
a--;
b--;
c--;
}
}
void strobe(int flash_rate)
{
for (int i = 0; i < NUM_LEDS; i++)
{
BL.setPixel(i, BL.color(31, 31, 31));
}
BL.show();
delay(flash_rate);
BL.setRange(0, NUM_LEDS - 1, BL.color(0, 0, 0));
BL.show();
delay(flash_rate);
}
[/code]