I have three while loops, each brightening and dimming a color, and between each loop, it briefly flashes the color that was just dimmed. RGB is common anode.
This is because the last iteration of the while loop starts with a value of rn being 255. You then increment rn making it 256 which when you apply to PWM is the equivalent of zero. So make all the while loops just less than 255 not less than or equal to 255.
You are going to add an analog reading to this and at the moment it is commented out. But when it is not the your lines like
r = rn * audio/1024;
Are going to return zero because the variable audio is an integer, and any integer divided by a larger integer will return zero. You need to look at floating point variables at this stage.
Grumpy_Mike:
This is because the last iteration of the while loop starts with a value of rn being 255
Agreed, the OP could use for() loops for this instead of while() loops, or assign the loop control variable back to zero before the while() loop.
Grumpy_Mike:
But when it is not the your lines like
r = rn * audio/1024;
Are going to return zero because the variable audio is an integer, and any integer divided by a larger integer will return zero.
Mike, won't the * and / get evaluated in left to right order because they have equal precedence? Hence my suggestion of casting to long so that the intermediate result of the * did not overflow, and the / would then give a useable result. (I though this overflow was causing the effect the OP is describing, I did not spot the more obvious answer as you did!)