I've posted a few things related to this project but I have one big issue. I have 16 rgb led lights constantly changing color. Each light has a button. When you press a button it turns that strip a solid color. When you press it again I want it to return to the changing of colors. I'm having a hard time figuring out how to set this up so that it can all run without delay. ie. I want to press two buttons at once if possible or at least press a button and not let it affect the cycle of all the other led's? I've gotten it to change to a solid color before, but getting it to change back has been in an issue.
Any ideas would be very helpful. And thanks to everyone who continues to help me!
Here is my code: its clearly still getting put together and I feel like it might be a little excessive so if you have any pointers on how to shorten it up please let me know. Thanks
#define DEBOUNCE 10
// output pins
int inMin = 2;
int inMax = 49;
//input pins
byte button[16]= {A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15};
#define NUMBUTTONS sizeof(button)
byte buttonpressed[NUMBUTTONS], pressed[NUMBUTTONS];
boolean state[16];
boolean lastState[16];
long previousMillis[48];
long interval[48] = {500, 600, 550, 500, 600, 550, 500};
long time[48];
void setup() {
byte i;
Serial.begin(9600);
for(int i=inMin; i<=inMax; i++)
{
pinMode(i, OUTPUT);
}
for(i = 0; i< 16; i++)
{
pinMode(button[i], INPUT);
}
for(int i=0; i<16; i++)
{
buttonpressed[i] = 0;
}
for(int i=0; i<49; i++)
{
previousMillis[i] = 0;
}
for(int i=0; i<16; i++)
{
lastState[i] = LOW;
}
for(int i=0; i<2; i++)
{
time[i] = millis();
}
}
void check_switches() {
static byte previousstate[NUMBUTTONS];
static byte currentstate[NUMBUTTONS];
static long lasttime;
byte index;
if (millis() < lasttime){
lasttime = millis();
}
if ((lasttime + DEBOUNCE) > millis()) {
return;
}
lasttime = millis();
for (index = 0; index < NUMBUTTONS; index++) {
buttonpressed[index] = 0;
currentstate[index] = digitalRead(button[index]);
if(currentstate[index] == previousstate[index]) {
if ((pressed[index] == HIGH) && (currentstate[index] == HIGH)) {
buttonpressed[index] = 1;
}
pressed[index] = !currentstate[index];
}
previousstate[index] = currentstate[index];
}
}
void loop()
{
check_switches();
if(buttonpressed[0] == 1 && state[0] == 1){
square1solid();}
else if (buttonpressed[0] == 0 && state[1] == 0){
square1();
if(buttonpressed[1] == 1 && state[1] == 1){
square2solid();
}
else if (buttonpressed[1] == 0 && state[1] == 0){
square2();}
}
/////////////////////////////////Square 1///////////////////////////////////
}
void square1solid() {
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);}
void square1(){
byte i;
unsigned long currentMillis1 = millis();
if (currentMillis1 - time[0] > interval[0]) {
time[0] = currentMillis1;
if(lastState[0] == LOW)
lastState[0] = HIGH;
else
lastState[0] = LOW;
digitalWrite(2, lastState[0]);
}
unsigned long currentMillis2 = millis();
if (currentMillis2 - time[1] > interval[1]) {
time[1] = currentMillis2;
if(lastState[1] == LOW)
lastState[1] = HIGH;
else
lastState[1] = LOW;
digitalWrite(3, lastState[1]);
}
unsigned long currentMillis3 = millis();
if (currentMillis3 - time[2] > interval[2]) {
time[2] = currentMillis3;
if(lastState[2] == LOW)
lastState[2] = HIGH;
else
lastState[2] = LOW;
digitalWrite(4, lastState[2]);
}
}
/////////////////////////Square 2////////////////////////////////
void square2solid() {
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
digitalWrite(7, LOW);}
void square2(){
byte i;
unsigned long currentMillis4 = millis();
if (currentMillis4 - time[3] > interval[3]) {
time[3] = currentMillis4;
if(lastState[3] == LOW)
lastState[3] = HIGH;
else
lastState[3] = LOW;
digitalWrite(5, lastState[3]);
}
unsigned long currentMillis5 = millis();
if (currentMillis5 - time[4] > interval[4]) {
time[4] = currentMillis5;
if(lastState[4] == LOW)
lastState[4] = HIGH;
else
lastState[4] = LOW;
digitalWrite(6, lastState[4]);
}
unsigned long currentMillis6 = millis();
if (currentMillis6 - time[5] > interval[5]) {
time[5] = currentMillis6;
if(lastState[5] == LOW)
lastState[5] = HIGH;
else
lastState[5] = LOW;
digitalWrite(7, lastState[5]);
}
}