Go Down

Topic: troubleshooting code for RGB light sequence arrays (Read 960 times) previous topic - next topic

Drc3p0

Hey there, i've been working on this code for toggling between preprogrammed light sequences with a pushbutton.  I will use it with some LDR's for an audio piece when its finished.  I've been staring at this code for awhile now and could use a fresh perspective for it.  
It kind of works, but the light sequences are not exactly what is written in the code.  I've removed the fade in/fade out features since it is hard to get it to recognize the button being pressed while the fade is happening.  Can you help me figure out the imperfections in this code?  

thank you in advance for your input.  

Code: [Select]
long lastTime = 0;
const int RPIN = 6;
const int GPIN =5;
const int BPIN =3;
const int SWITCHPIN = A4;

boolean rArray0[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
boolean gArray0[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
boolean bArray0[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int hArray0[] = {0,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50};



//boolean rArray0[] = {0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
//boolean gArray0[] = {0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,0,1,0,1,0,1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,0,1,0,1,0,1,1};
//boolean bArray0[] = {0,1,1,1,1,1,1,1,1,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,1,1,1,1,1,1};
//int hArray0[] = {0,10,20,30,40,50,60,70,80,90,100,120,140,160,180,200,220,240,260,280,300,330,360,390,420,480,550,600,700,800,900,900,800,700,600,550,480,420,390,360,330,300,280,260,240,220,200,180,160,140,120,100,90,80,70,60,50,40,30,20,10};


boolean rArray1[] = {1,1,1,1,1,0,0,0,0,1,1,0,0,1,0,0,1,0,0,1,1,0,1};
boolean gArray1[] = {1,1,1,1,0,0,0,1,0,1,1,0,0,0,1,1,1,0,0,1,1,1,1};
boolean bArray1[] = {1,1,1,1,1,0,0,0,1,1,1,0,0,1,0,1,0,0,0,1,1,0,1};
int hArray1[] = {0,50,50,50,500,500,500,500,700,700,320,320,320,320,700,700,320,320,320,320,700,700,320};

boolean rArray2[] = {1,1,1,1,1,0,0,0,0,0};
boolean gArray2[] = {0,0,0,0,0,1,1,1,1,1};
boolean bArray2[] = {0,0,0,1,1,1,1,0,0,0};
int hArray2[] = {0,500,500,500,500,500,500,500,500,500};

boolean rArray3[] = {0,0,0};
boolean gArray3[] = {1,1,1};
boolean bArray3[] = {0,0,0};
int hArray3[] = {0,50,50,50};

//boolean rArray3[] = {1,0,1,0,1,0};
//boolean gArray3[] = {1,0,1,0,1,0};
//boolean bArray3[] = {1,0,1,0,1,0};
//int hArray3[] = {0,23,10,23,34,34};  

boolean rArray4[] = {1,0,1,0,1,0};
boolean gArray4[] = {1,0,1,0,1,0};
boolean bArray4[] = {1,0,1,0,1,0};
int hArray4[] = {0,100,100,100,100,100};  

boolean rArray5[] = {1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0};
boolean gArray5[] = {0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1};
boolean bArray5[] = {0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0};
int hArray5[] = {0,250,250,250,250,250,250,250,250,250,250,250,250,250,250,250,250,250};  

boolean rArray6[] = {1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0};
boolean gArray6[] = {0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1};
boolean bArray6[] = {0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0};
int hArray6[] = {0,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10};

boolean rArray7[] = {1,0,1,0,1,1,1,1,0,1,0,1,1,1};
boolean gArray7[] = {1,1,1,0,1,0,1,1,1,1,0,1,0,1};
boolean bArray7[] = {1,0,1,1,1,0,1,1,0,1,1,1,0,1};
int hArray7[] = {0,34,34,34,34,34,34,34,34,34,34,34,34,34};  


//boolean mRArray[] = {rArray1[],rArray2[]};    1+10+10+11=32 array0      
//boolean mGArray[] = {gArray1[],gArray2[]};
//boolean mBArray[] = {bArray1[],bArray2[]};
int countArray[] = {8,7,6,5,4,3,2,1};
int maxSwitch = 8;
int currentPlace = 0;
int count = 0;
boolean pressed = false;


void setup(){
 Serial.begin(9600);
 pinMode(RPIN,OUTPUT);
 pinMode(GPIN,OUTPUT);
 pinMode(BPIN,OUTPUT);
 pinMode(SWITCHPIN,INPUT);
}

void checkBtn(){
boolean switcher = digitalRead(SWITCHPIN);
if(!pressed && switcher){
 pressed = true;
 currentPlace++;
 count = 0;
 lastTime = millis();
}
if(pressed && !switcher){
  pressed = false;
}
}

void loop(){
checkBtn();
 
 if(lastTime == 0){
 lastTime = millis();
}
/////////////////////////////////

if(lastTime + hArray0[count] < millis() && currentPlace == 0){
  digitalWrite(RPIN,rArray0[count]);
  digitalWrite(GPIN,gArray0[count]);
  digitalWrite(BPIN,bArray0[count]);
  lastTime = millis();
  count = count + 1;
  if(count >= countArray[currentPlace]){
   count = 0;
  }
}
//////////////////////////
 if(lastTime + hArray1[count] < millis() && currentPlace == 1){  
  digitalWrite(RPIN,rArray1[count]);
  digitalWrite(GPIN,gArray1[count]);
  digitalWrite(BPIN,bArray1[count]);
  lastTime = millis();
  count = count + 1;
  if(count >= countArray[currentPlace]){
   count = 0;
  }
}
/////////////////////////////////
 if(lastTime + hArray2[count] < millis() && currentPlace == 2){  
  digitalWrite(RPIN,rArray2[count]);
  digitalWrite(GPIN,gArray2[count]);
  digitalWrite(BPIN,bArray2[count]);
  lastTime = millis();
  count = count + 1;
  if(count >= countArray[currentPlace]){
   count = 0;
  }
}
///////////////////////////



 if(lastTime + hArray3[count] < millis() && currentPlace == 3){  
  digitalWrite(RPIN,rArray3[count]);
  digitalWrite(GPIN,gArray3[count]);
  digitalWrite(BPIN,bArray3[count]);
  lastTime = millis();
  count = count + 1;
  if(count >= countArray[currentPlace]){
   count = 0;
  }
}
///////////////////////////

 if(lastTime + hArray4[count] < millis() && currentPlace == 4){  
  digitalWrite(RPIN,rArray4[count]);
  digitalWrite(GPIN,gArray4[count]);
  digitalWrite(BPIN,bArray4[count]);
  lastTime = millis();
  count = count + 1;
  if(count >= countArray[currentPlace]){
   count = 0;
  }
}
///////////////////////////
  if(lastTime + hArray5[count] < millis() && currentPlace == 5){  
  digitalWrite(RPIN,rArray5[count]);
  digitalWrite(GPIN,gArray5[count]);
  digitalWrite(BPIN,bArray5[count]);
  lastTime = millis();
  count = count + 1;
  if(count >= countArray[currentPlace]){
   count = 0;
  }
}
///////////////////////////
  if(lastTime + hArray6[count] < millis() && currentPlace == 6){  
  digitalWrite(RPIN,rArray6[count]);
  digitalWrite(GPIN,gArray6[count]);
  digitalWrite(BPIN,bArray6[count]);
  lastTime = millis();
  count = count + 1;
  if(count >= countArray[currentPlace]){
   count = 0;
  }
}
///////////////////////////
  if(lastTime + hArray7[count] < millis() && currentPlace == 7){  
  digitalWrite(RPIN,rArray7[count]);
  digitalWrite(GPIN,gArray7[count]);
  digitalWrite(BPIN,bArray7[count]);
  lastTime = millis();
  count = count + 1;
  if(count >= countArray[currentPlace]){
   count = 0;
  }
}
///////////////////////////


Grumpy_Mike

Quote
but the light sequences are not exactly what is written in the code

No the sequence is exactly what is written in the code.

I suspect that it is you that has different expectations than the code you have written.
So:-
1) Say what it actually does.
2) Say what you want it to do.

Drc3p0

Quote
1) Say what it actually does.
2) Say what you want it to do.

yes, sorry about the lack of proper description.


In the process of writing this response, I just figured out what what was causing the led sequences to repeat improperly.  Its amazing what a good nights rest can do for you!  The int countArray[] = { }; had the wrong values.  I forgot that it related directly to the number of repetitions before the sequence started over.

sorry about that!
this code is alot more satisfying to work with when it does exactly what its supposed to do ;)

I guess while i'm on this topic, i'd like to address some things that I wanted to do to further modify it, which I have trouble with so far:

I want to incorporate fades into the program. 
this code has been written without delays so that it will not interfere with detecting a pressed button.  I looked into using buttonInterrupts but I was not able to get it to work as smoothly as this format.  The arduino tutorial was a little too vague for me, so I have opted for no delays for the moment.
I couldn't find any information about doing PWM without delays.  I had this fade incorporated into the following code, but it was unreliable for detecting when the button was pressed. 

Any insight on how to either perform PWM without delays, or how to incorporate buttonInterrupt properly so that delays may be used?


Code: [Select]
if(lastTime + hArray8[count] < millis() && currentPlace == 8){   
   
 
  // fade in from min to max in increments of 5 points:
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=1) {
    // sets the value (range from 0 to 255):
    analogWrite(RPIN, fadeValue);   
    // wait for 30 milliseconds to see the dimming effect   
    delay(5);                           
  }
lastTime = millis();
   count = count + 1;
   if(count >= countArray[currentPlace]){
    count = 0;
   }
   if(currentPlace >= maxSwitch){
   currentPlace = 0;
}
  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=1) {
    // sets the value (range from 0 to 255):
    analogWrite(RPIN, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect   
    delay(5);                           
  }


  lastTime = millis();
   count = count + 1;
   if(count >= countArray[currentPlace]){
    count = 0;
   }
   if(currentPlace >= maxSwitch){
   currentPlace = 0;

}
  }

   // fade in from min to max in increments of 5 points:
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=1) {
    // sets the value (range from 0 to 255):
   analogWrite(GPIN, fadeValue);   
    // wait for 30 milliseconds to see the dimming effect   
    delay(5);                           
  }
lastTime = millis();
  count = count + 1;
  if(count >= countArray[currentPlace]){
    count = 0;
   }
   if(currentPlace >= maxSwitch){
   currentPlace = 0;
}
  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=1) {
    // sets the value (range from 0 to 255):
    analogWrite(GPIN, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect   
    delay(5);                           
  }
  lastTime = millis();
   count = count + 1;
   if(count >= countArray[currentPlace]){
    count = 0;
   }
   if(currentPlace >= maxSwitch){
   currentPlace = 0;
}
   // fade in from min to max in increments of 5 points:
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=1) {
    // sets the value (range from 0 to 255):
    analogWrite(BPIN, fadeValue);   
    // wait for 30 milliseconds to see the dimming effect   
    delay(5);                           
  }
lastTime = millis();
   count = count + 1;
   if(count >= countArray[currentPlace]){
    count = 0;
   }
   if(currentPlace >= maxSwitch){
   currentPlace = 0;
}
  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=1) {
    // sets the value (range from 0 to 255):
    analogWrite(BPIN, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect   
    delay(5);                           
  }

   lastTime = millis();
   count = count + 1;
   if(count >= countArray[currentPlace]){
    count = 0;
   }
}
if(currentPlace >= maxSwitch){
   currentPlace = 0;
}
}

Grumpy_Mike

Quote
so I have opted for no delays for the moment.


Consider this bit of code:-
Code: [Select]
// fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=1) {
    // sets the value (range from 0 to 255):
    analogWrite(BPIN, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect   
    delay(5);                           
  }

Ignoring the fact that the comment does not match the code ( you wait for 5 milliseconds to see the dimming effect )
You have 255 * 5 = 1275mS or 1.275 seconds of delay. That is not opting for no delays!

You are starting to get long code so split it up into functions so you can see what is going on.
Quote
I couldn't find any information about doing PWM without delays.

It is just the same as doing anything else without delays. Don't use a for loop, just use an if and an increment. Only change the PWM value when it is time to do so. Just like doing anything else without delays.

Go Up