So I'm making a single multicolored led light display so I can learn the language a bit better/and to test out my new equalization equation I made.
Anyway, I'm having a bit of a problem. Actually 2 problems.
First, I'm experiencing a bit of flickering, not really sure why.
And it seems to me that one of the colors randomly goes out.(and then starts getting brighter again per the loop).
Here's my code:
int Led1 = 3;
int Dirs[] = {0, 0, 0};
float Values[] = {0, 0, 0};
int Led2 = 5;
int Led3 = 6;
int Random;
float ratio;
void setup(){
Serial.begin(9600); //debugging
pinMode(Led1,OUTPUT);
pinMode(Led2,OUTPUT);
pinMode(Led3,OUTPUT);
randomSeed(analogRead(0));
Values[0] = random(0,1025);
Values[1] = random(0,1025);
Values[2] = random(0,1025);
}
void loop(){
for(int i = 0;i < 3; i++){ //this for statement is meant to cause all values to loop back and forth between 0-255.
Random = random(1,5);
if(Dirs[i] < 1){
Values[i] = Values[i] + Random;
}
else{
Values[i] = Values[i] - Random;
}
if(Values[i] >= 1024 and Dirs[i] < 1){
Dirs[i] = 1;
Values[i] = 1024; //Fail-safe I want to avoid values outside of the range 0-1024
}
else if(Values[i] <= 0 and Dirs[i] >= 1){
Dirs[i] = 0;
Values[i] = 0; //Fail-safe I want to avoid values outside of the range 0-1024
}
}
delay(200); //I have no sense of time, this might be to fast. That would explain the flickerying anyway.
ratio = 31*3/(Values[0]+Values[1]+Values[2]); //this equation is meant to equalize the numbers, so that the light always outputs the same ammount of power. Equalizes around 31/32 (don't really know which since pwm starts with 0)
analogWrite(Led1,Values[0]*ratio);
analogWrite(Led2,Values[1]*ratio);
analogWrite(Led3,Values[2]*ratio);
}
ratio = 31*3/(Values[0]+Values[1]+Values[2]);
analogWrite(Led1,Values[0]*ratio);
analogWrite(Led2,Values[1]*ratio);
analogWrite(Led3,Values[2]*ratio);
You need to rethink this. AnalogWrite takes values between 0-255. Values range between 0-1024 and ratio ranges between 0.03-93.
You also have a potential divide by zero problem.
Well the equation actually works out. It makes it so that the total power outputed is ~93 (because 3*31 is 93). Basically if all the values are 1024 then:
ratio = 31*3/1024+1024+1024 //ratio equals ~0.03
then analogWrite(led, Values[any_value]*ratio) //note the *ratio. So Values[any_value]*ratio = 1024*0.03 = 30.72
Now if we had extreme values above 1024 we might have a problem. But that won't happen outside of a programming bug.
I'm actually really proud of coming up with the formula for this. :3
Anyway, I totally didn't think about the possibility of a 0 denominator. I'll make sure to fix it.
And a little addition info. I accidentally set the randomizer to only out put 0. And it still flickers(though it doesn't change color.) I'll plug something into the serial monitor to check values sometime tomorrow.
Well i've been trying to fix my flashing problem, with no luck. I disabled the randomizer, and outputed the values that is analogWriten to the serial monitor. The values never changed. I even went so far as to add an additional int array just to see if giving analogwrite a value that wasn't a float would fix it. Still nothing.
Any ideas?
Edit: I just tried giving analogwrite a specific value. still nothing.
I just tried your code with debug print statements to show the values you are passing to the analogWrite() calls. I have posted a few seconds of the data at the bottom of this post. They look to be doing what you expect, slowly ramping up or down in random steps. I don't have LEDs or test equipment handy to check the outputs on the pins you have defined.
Can you clarify a bit more what problems you are getting?
Have you tried only connecting one LED colour at a time to see how each colour is behaving?
Thanks!
Apologies, I changed my code a bit to debug. This is what I'm using now.
int Led1 = 6;
int Dirs[] = {0, 0, 0};
float Values[] = {1, 1, 1};
int iValues[] = {0, 0, 0};
int Led2 = 10;
int Led3 = 11;
float Random;
float ratio;
void setup(){
Serial.begin(9600); //debugging
pinMode(Led1,OUTPUT);
pinMode(Led2,OUTPUT);
pinMode(Led3,OUTPUT);
randomSeed(analogRead(0));
Values[0] = random(1,1025);
Values[1] = random(1,1025);
Values[2] = random(1,1025);
}
void loop(){
for(int i = 0;i < 3; i++){ //this for statement is meant to cause all values to loop back and forth between 0-255.
Random = random(0.0,1.0);
if(Dirs[i] < 1){
Values[i] = Values[i] + Random;
}
else{
Values[i] = Values[i] - Random;
}
if(Values[i] >= 1025 and Dirs[i] < 1){
Dirs[i] = 1;
Values[i] = 1025; //Fail-safe I want to avoid values outside of the range 0-1024
}
else if(Values[i] <= 1 and Dirs[i] >= 1){
Dirs[i] = 0;
Values[i] = 1; //Fail-safe I want to avoid values outside of the range 0-1024
}
}
delay(200); //I have no sense of time, this might be to fast. That would explain the flickerying anyway.
ratio = 31*3/(Values[0]+Values[1]+Values[2]); //this equation is meant to equalize the numbers, so that the light always outputs the same ammount of power. Equalizes around 31/32 (don't really know which since pwm starts with 0)
iValues[0] = Values[0]*ratio;
iValues[1] = Values[1]*ratio;
iValues[2] = Values[2]*ratio;
analogWrite(Led1,iValues[0]);
analogWrite(Led2,iValues[0]);
analogWrite(Led3,iValues[0]);
Serial.print(iValues[0]);
Serial.print(" ");
Serial.print(iValues[1]);
Serial.print(" ");
Serial.print(iValues[2]);
Serial.print("\n");
}
The above doesn't climb or fall, it just stays the same.
And I did, when I unplug led's it usually stopped. I have had occasions when it still flashes with 2 leds, but once I only have 1 plugged in it stops.