I wanted to simultaneously set intensities on two LEDs, so that one was pulsing from off to 100% on while the other was pulsing from 100% on to off. I tried 2 code solutions, both listed below. Neither worked as I expected - each LED completes a pulse cycle before the other one starts its pulse cycle. Debug statements to Serial are indicating that the value of i is getting set correctly. I don't understand what's wrong. Can someone help, please?
Project 1 (uses loop plus a global variable to pulse):
// Blinking LED
const int LED1 = 11; // LED is connected to pin 13
const int LED2 = 10; // LED is connected to pin 13
int i = 0;
int step = 5;
void setup()
{
pinMode(LED1, OUTPUT); // Set LED to be an output
pinMode(LED2, OUTPUT); // Set LED to be an output
// Serial.begin(9600); // Initialise serial connection with computer for debugging
// Serial.println("Hello, world!");
}
void loop()
{
analogWrite(LED1, i); // Pulse LED
// Serial.print("LED1: ");
// Serial.println(i);
analogWrite(LED2, 255-i); // Pulse LED
// Serial.print("LED2: ");
// Serial.println(255-i);
delay(50);
i += step;
if ((i > 255) || (i < 0))
{
step = -step;
i+= step;
}
}
Project 2 (loops over i in a for loop):
// Blinking LED
const int LED1 = 11; // Red LED is connected to pin 11
const int LED2 = 10; // Green LED is connected to pin 10
int step = 5; // Pulse speed - higher value pulses quicker
void setup()
{
pinMode(LED1, OUTPUT); // Set red LED to be an output
pinMode(LED2, OUTPUT); // Set green LED to be an output
// Serial.begin(9600); // Initialise serial connection with computer for debugging
// Serial.println("Hello, world!");
}
void loop()
{
int i;
for (i=0; i<256; i+=step)
{
analogWrite(LED1, i);
// Serial.print("LED1: ");
// Serial.println(i);
analogWrite(LED2, 255-i);
// Serial.print("LED2: ");
// Serial.println(255-i);
delay(50);
}
for (i=255; i>=0; i-=step)
{
analogWrite(LED1, i);
// Serial.print("LED1: ");
// Serial.println(i);
analogWrite(LED2, 255-i);
// Serial.print("LED2: ");
// Serial.println(255-i);
delay(50);
}
}
If I understand correctly, you're doing manual PCM on the LEDs and relying on the slowness of the loop to give you a smooth movement for Knight Rider? Isn't there a way to use analogWrite to set the brightness of the two LEDs independently?
const int LED1 = 11; // LED is connected to pin 13
const int LED2 = 10; // LED is connected to pin 13
Comments and code do not match.
My apologies! Could this be what is causing the problem?
Dave.
PS. I have a working version now, and a theory as to why I thought this wasn't working correctly. Knight Rider was a big help. The key point is to quantise the brightness - the change between 255 and 128 was basically imperceptible. By choosing just 9 values in the brightness range, I get a nice fade-in/fade-out behaviour.
Thanks!
// Blinking LED
const int LED1 = 11; // LED is connected to pin 11
const int LED2 = 10; // LED is connected to pin 10
void setup()
{
pinMode(LED1, OUTPUT); // Set LED to be an output
pinMode(LED2, OUTPUT); // Set LED to be an output
}
int intensities[] = {255,127,63,31,15,7,3,1,0};
int num_steps = 9;
int d = 40;
void loop()
{
int i;
for (i = 0; i < num_steps; i++) {
analogWrite(LED1, intensities[i]); // Pulse LED
analogWrite(LED2, intensities[num_steps-i-1]); // Pulse LED
delay(d);
}
for (i = num_steps-1; i >= 0; i--) {
analogWrite(LED1, intensities[i]); // Pulse LED
analogWrite(LED2, intensities[num_steps - i - 1]); // Pulse LED
delay(d);
}
}
I wanted to simultaneously set intensities on two LEDs, so that one was pulsing from off to 100% on while the other was pulsing from 100% on to off. I tried 2 code solutions, both listed below. Neither worked as I expected - each LED completes a pulse cycle before the other one starts its pulse cycle. Debug statements to Serial are indicating that the value of i is getting set correctly. I don't understand what's wrong. Can someone help, please?
I believe I understand what the problem was (sharing to confirm and help anyone with similar issues).
The LED basically looks the same from all brightnesses from 128 up to 255.
That means that as I was cycling from 0 to 255, when the LED values were (128,127) and when they were (140,115), LED1 was basically the same brightness. The end result is it looks like LED2 is fading out, then fading in, while LED1 is just on. If I replace the top brightness in the first example with 128, the code works more or less as expected (although the selected values works best with the powers of 2).
Depends on what you want the code to do, it is never a good idea to have a conflict, the code always wins.
It also indicates sloppiness that spreads to your coding.
the change between 255 and 128 was basically imperceptible
So you have to ask yourself why. I suspect that your series resistor was too low. What value was it?
Depends on what you want the code to do, it is never a good idea to have a conflict, the code always wins.
It also indicates sloppiness that spreads to your coding.
Wow! Is this special treatment for me, or do all newcomers to the forum get this red carpet rolled out for them?
the change between 255 and 128 was basically imperceptible
So you have to ask yourself why. I suspect that your series resistor was too low. What value was it?
I was not using a resistor. I added a 220 Ohm resistor and things are better.
Is this special treatment for me, or do all newcomers to the forum get this red carpet rolled out for them?
Yes it is special to you. Only arrogant, sarcastic ones get back what they give.
Thanks for the confidence boost,
You have to learn that you have to learn. You only can be confident when you actually know what you are doing. That will come naturally. I know there is a current trend of praising every little move even when it is wrong, or should I say differently right. But I am old enough to believe that the best kindness to someone is to tell them when they are wrong.
I added a 220 Ohm resistor and things are better.
Glad it works.
You see I didn't give up on you when you thought you had a solution. Now you know so much more.
I know I shouldn't rise to the bait, and you have been helpful (thank you for that), but...
Grumpy_Mike:
Is this special treatment for me, or do all newcomers to the forum get this red carpet rolled out for them?
Yes it is special to you. Only arrogant, sarcastic ones get back what they give.
In your initial comment, the only thing you said about the code was that the comments didn't match the code - no suggestion that this was only a style issue, no indication that this wouldn't change the behaviour of the code. Imagine for a second if I had been a 15 year old bashing away copying & pasting code samples. That's not the case, but if it were, your comment would have been unhelpful.
Then in your follow-up, you say I'm sloppy. Now, I see you have 5 stars and 20K forum posts - I'm a newcomer who created his account yesterday and now I'm up to 7 posts. Perhaps you could have gone a bit easier on me?
Thanks for the confidence boost,
You have to learn that you have to learn. You only can be confident when you actually know what you are doing. That will come naturally. I know there is a current trend of praising every little move even when it is wrong, or should I say differently right. But I am old enough to believe that the best kindness to someone is to tell them when they are wrong.
I added a 220 Ohm resistor and things are better.
Glad it works.
You see I didn't give up on you when you thought you had a solution. Now you know so much more.
Thanks for the link. While I've been around the software world for a while, hardware hacking is new to me. And in spite of what you believe, I'm painfully aware of what I have to learn. The "fix your comments" jibe didn't exactly teach me anything. What you read as confidence was honest surprise on my part that my first ever message to the forum might have gotten an unhelpful comment focussing on style rather than substance.
Anyway - thank you again for your later helpful comments, and for the link - perhaps we can start over?
If I understand correctly, you're doing manual PCM on the LEDs and relying on the slowness of the loop to give you a smooth movement for Knight Rider? Isn't there a way to use analogWrite to set the brightness of the two LEDs independently?
No and yes. Yes there is PWM (used by analogWrite) but it is only available for some pins. No, I do not rely on the "slowness for smooth movement". Depending on the example you looked at I compensated for the loop speed. The more advanced (flickerfree) examples do not require a slow loop. In fact they require as fast as possible execution.