hey guys...so i just about got a RGB led to fade magenta like...now...but for some reason if i make the delay (dly) less than 2 it goes all trippy...any idea why? and also the way i got this to work seems more than slightly crude...is there no better way to do this with a loop that doesnt need a delay between each on/off toggle?...thanks in advance:)
int R = 3;
int G = 5;
int B = 6;
int dly = 2;
void setup()
{
pinMode(R, OUTPUT);
pinMode(G, OUTPUT);
pinMode(B, OUTPUT);
}
void loop()
{
for(int i=0; i<=255; i++)
{
analogWrite(R, i);
delay(dly);
analogWrite(R, 0);
delay(dly);
analogWrite(B, i);
delay(dly);
analogWrite(B, 0);
delay(dly);
}
for(int i=255; i>=0; i--)
{
analogWrite(R, i);
delay(dly);
analogWrite(R, 0);
delay(dly);
analogWrite(B, i);
delay(dly);
analogWrite(B, 0);
delay(dly);
}
}
uhm the leds dont realy work well with both of them on at the same time...the red diode will just overpower the blue for example...so if i pulse them their on at different times but the eye cant actually see it and it becomes magenta...unless im just doing something wrong haha
Yes, do stop turning off the lights. And it's not needed to delay between each pin setting. Something like this should generate the right pattern:
int count;
void loop() {
// increment the counter and grab 8 bits of brightness
byte brightness = ++count;
// use the next higher bit as an up or down indicator;
// invert the brightness on the down count
if (count & 0x100) brightness = ~brightness;
// update the pwm outputs
analogWrite(R, brightness);
analogWrite(B, brightness);
delay(del);
}
i had a resistor on the cathode? shouldnt that work? and
No it will not work, you will see what you are seeing that only one LED will be on at any one time.
So please wire it up correctly and then program it correctly.
it freaking works!! thank you so much...last thing...i know my noobnes must be close to intolerable butthe part of billroys code that says
(count & 0x100)......what does it mean? and count in that part does reference the variable that was declared in his code right?
0x100 is a number in the hexadecimal system, it is the same as the decimal number 256. It is often easier to use hex notation especially when you are concerned with a bit pattern rather than a number as such. Although in this case I would have used the decimal number.
The trickiness here is using a 16 bit counter (count) to control an eight-bit quantity. The lowest 8 bits of count are used as the brightness, possibly inverted as I'll get to in a minute.
The ninth bit of count is used as an up-down indicator, because it rolls over just as you'd like an up-down indicator to: it's off for the first 256 counts, then on for the next 256, and so on. It's a little state machine, for free.
The expression that is puzzling you (count & 0x100) tests the ninth bit of count to see if it is one or zero (using a bitwise AND between count and a constant denoting the ninth bit). If the ninth bit is one, we are in the counting down phase, and in that case the code inverts the brighness so instead of going from 0..255 it goes from 255..0, ramping down instead of up.
Read up on bitwise boolean operators for more on & (and) and ~ (bitwise complement) and their friends | (or) and ! (logical complement).
Hope that helps and happy to take followup questions.