As a human being, we have a lot of problems. Sometimes it's a little frustrating, and it's actually a great thing being in a situation that you are forced to solve a problem because what's the point if you have no problem at all, and sometimes the problem is literally unsolvable. Here is the time.
It all started out with this simple, nice and fast PWM single led drive that I have wrote long times ago. It worked fine as it.
// This part is a 4 steps PWM
pwmCounter++;
if(pwmCounter>4) pwmCounter=0;
if(pwmCounter<pwmValue) PORTD |= (1<<7);
else PORTD &= (~1<<7);
if(PWM==5) PWM=0;
if(PWM==255) PWM=4;
pwmValue=PWM;
// This part is a 4 steps PWM
The problem suddenly appeared once I tried it using color led. the colorful led lighted up right for the brightness part, but the color is a mess. It seems all random instead of predefined white, blue, red, green and etc.
int ta,tb;
uint8_t ledColor; // 0xE0=white; 0x80=BLUE; 0x40=RED; 0x20=Green; 0xC0=Pink; 0x60=Yellow, 0xA0=teal
uint8_t color[]= {0xE0, 0x80, 0x40, 0x20, 0xC0,0x60, 0xA0};
uint8_t cc=0;
byte pwmCounter, pwmValue=4, PWM=3; // 4=off; 0=on; 1=25%; 2=50%; 3=75%;
void setup() {
DDRD=0b11100000;
DDRB&=0;
}
void loop() {
// This part is a 4 steps PWM state
pwmCounter++;
if(pwmCounter>4) pwmCounter=0;
if(pwmCounter<pwmValue) PORTD = PORTD | (color[cc]);
else PORTD = PORTD & (~color[cc]);
if(PWM==5) PWM=0;
if(PWM==255) PWM=4;
pwmValue=PWM;
// This part is a 4 steps PWM state
ta=millis();
if(ta-tb>1000){
cc++;
if (cc>=7) cc=0;
tb=ta;
}
}
Connections:
D5-6-7 >> Output:3 Channels 30V 5.6A MOSfet (Active LOW)
5-6-7 are connected to the RGB led.