Hello all,
I have a question, I’m trying to make a sort of ‘menu’ switch for my RGB LED’s from Adafruit.
The LED’s have a LPD6803 chip, so i cam control them individually.
My problem is this, I can only switch when the running program is ended. I would like to switch during the program.
Who can help me out?
Here is the code:
#include <TimerOne.h>
#include "LPD6803.h"
// Constants that won't change:
const int dataPin = 2; // 'yellow' wire Data
const int clockPin = 3; // 'green' wire Clock
const int buttonPin = 7; // pushbutton for Mode change
// Constants that will change:
int buttonState = 0;
int lastButtonState = 0;
int stateNum = 0;
int delayValue = 50;
// Set the first variable to the NUMBER of pixels. 20 = 20 pixels in a row
LPD6803 strip = LPD6803(25, dataPin, clockPin);
void setup() {
// The Arduino needs to clock out the data to the pixels
// this happens in interrupt timer 1, we can change how often
// to call the interrupt. setting CPUmax to 100 will take nearly all all the
// time to do the pixel updates and a nicer/faster display,
// especially with strands of over 100 dots.
// (Note that the max is 'pessimistic', its probably 10% or 20% less in reality)
strip.setCPUmax(70); // start with 50% CPU usage. up this if the strand flickers or is slow
// Start up the LED counter
strip.begin();
// Update the strip, to start they are all 'off'
strip.show();
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize serial communication:
Serial.begin(9600);
Serial.println("LPD-6803 sequencing");
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
stateNum++;
// Serial.println("on");
Serial.print("Program: ");
Serial.println(stateNum, DEC);
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
// change the state of the led when someone pressed the button.
if(stateNum>2) stateNum=0; {
}
if (stateNum == 0) {
colorWipe(Color(63, 0, 0), delayValue);
colorWipe(Color(63, 53, 0), delayValue);
colorWipe(Color(63, 63, 0), delayValue);
colorWipe(Color(0, 63, 0), delayValue);
colorWipe(Color(0, 63, 53), delayValue);
colorWipe(Color(0, 63, 63), delayValue);
colorWipe(Color(0, 0, 63), delayValue);
colorWipe(Color(63, 0, 63), delayValue);
}
if (stateNum == 1) {
rainbow(delayValue);
}
if (stateNum == 2) {
rainbowCycle(delayValue);
}
}
void rainbow(uint8_t wait) {
int i, j;
for (j=0; j < 96 * 1; j++) { // 1 cycle of all 96 colors in the wheel
for (i=0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel( (i + j) % 96));
}
strip.show(); // write all the pixels out
delay(wait);
}
}
// Slightly different, this one makes the rainbow wheel equally distributed
// along the chain
void rainbowCycle(uint8_t wait) {
int i, j;
for (j=0; j < 96 * 1; j++) { //1 cycle of all 96 colors in the wheel
for (i=0; i < strip.numPixels(); i++) {
// tricky math! we use each pixel as a fraction of the full 96-color wheel
// (thats the i / strip.numPixels() part)
// Then add in j which makes the colors go around per pixel
// the % 96 is to make the wheel cycle around
strip.setPixelColor(i, Wheel( ((i * 96 / strip.numPixels()) + j) % 96) );
}
strip.show(); // write all the pixels out
delay(wait);
}
}
// fill the dots one after the other with said color
// good for testing purposes
void colorWipe(uint16_t c, uint8_t wait) {
int i;
for (i=0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
/* Helper functions */
// Create a 15 bit color value from R,G,B
unsigned int Color(byte r, byte g, byte b)
{
//Take the lowest 5 bits of each value and append them end to end
return( ((unsigned int)g & 0x1F )<<10 | ((unsigned int)b & 0x1F)<<5 | (unsigned int)r & 0x1F);
}
//Input a value 0 to 127 to get a color value.
//The colours are a transition r - g -b - back to r
unsigned int Wheel(byte WheelPos)
{
byte r,g,b;
switch(WheelPos >> 5)
{
case 0:
r=31- WheelPos % 32; //Red down
g=WheelPos % 32; // Green up
b=0; //blue off
break;
case 1:
g=31- WheelPos % 32; //green down
b=WheelPos % 32; //blue up
r=0; //red off
break;
case 2:
b=31- WheelPos % 32; //blue down
r=WheelPos % 32; //red up
g=0; //green off
break;
}
return(Color(r,g,b));
}
Thanx a lot!