Hi. I'm a newbie to the Arduino, and electronics in general. I started with the blinking, moved to fading, and now I'm trying to roll my own fading program. The simple point was for the program to dim one diode while the other was getting brighter. However, the lights will take turns cycling through the fades, not as programmed. Here is a video of what happens:
And here is the code:
int ledPin1 = 9; // LED connected to digital pin 9
int ledPin2 = 10;
void setup() {
// nothing happens in setup
}
void loop() {
// fade in from min to max in increments of 5 points:
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin1, fadeValue);
analogWrite(ledPin2, 255 - fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
// fade out from max to min in increments of 5 points:
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin1, fadeValue);
analogWrite(ledPin2, 255 - fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}
Am I doing something wrong? Advice would be appreciated.
Well each LED should have an opposite brightness value at any given point.
Example:
time
led1
led2
0
0
255
1
75
180
2
150
105
3
225
30
4
255
0
5
225
30
6
150
105
7
75
180
8
0
255
HOWEVER, if you watch the video, one LED will go through the 0-255 phase and BACK 255-0 while the other light sits on constantly. When that first light finishes the cycle, it happens with the second light. In the code, it's written to have them at opposite brightness values at any point. While one is getting brighter, the other dims. And vice versa.
Nope, that looks about right--what you're missing is that the brightness of an LED isn't linear with regards to the duty cycle (PWM/255), so you aren't able to see the changes in brightness in the other LED.
Try playing around with the code to figure out the point at which adding 10 to the analogWrite parameter no longer makes a visible difference--I bet you'll find that it's well under 255.
Hey I think im trying to do the same thing as you, did you have any luck getting them to alternate. So far I have managed to get mine to have one led start of bright then dim down and the other start dim and brighten up but once they reach the end of that they just go stait back to their original (one that started bright turns bright and one that started dim turns dim) state imediatly instead of fading in the oposite "brightness" Thx if you can help
So far I have managed to get mine to have one led start of bright then dim down and the other start dim and brighten up but once they reach the end of that they just go stait back to their original (one that started bright turns bright and one that started dim turns dim) state imediatly instead of fading in the oposite "brightness"
Maybe you'd get better help if you posted your code.
int ledPin1 = 9; // LED connected to digital pin 9
int ledPin2 = 10;
int n = 0;
int n2 = 255;
boolean led1 = true;
boolean led2 = false;
void setup() {
// nothing happens in setup
}
void loop()
{
if (n>151)
led1=false;
if (n<=1)
led1=true;
if (n2>151)
led2=false;
if (n2<=1)
led2=true;
delay(30);
if (led1==true)
n++;
if (led1==false)
n--;
if (led2==true)
n2++;
if (led2==false)
n2--;
analogWrite(ledPin1, n);
analogWrite(ledPin2, n2);
}