I've copied and slightly modified your diagram, as below. Note the added connections of R1, R3 cathodes to that of R2, and similarly for green and blue LEDs.
schematic
simulate this circuit – Schematic created using CircuitLab
I'm supposing that unlike the original diagram, this one represents what you actually wired up.
A basic problem with your program is that it spends most of its time changing PWM settings, without letting PWM cycles actually occur. To see this, do some timing analysis on the code.
The code repeatedly executes sections of code like the following:
led(0,i,0);
__asm__("nop\n\t"); //one clock cycle delay
PORTD = B01000000; //select LED-2
The call to led()
results in three calls to analogWrite()
. As seen by perusing at garretlab the source code for analogWrite()
, digitalWrite()
, and pinMode()
, each of those analogWrite()
calls takes a few handfuls of cycles – let's say 50 at most – so the three lines of code shown above take less than a dozen microseconds to execute. Thus each of the two for
loops takes less than 10 milliseconds to complete, causing the loop()
routine to run more than 50 times per second. Persistence of vision will result in observers just seeing the average level of light, rather than any ramping up or down.
- 8.9k
- 3
- 21
- 33