Just messing around with some code as I just got myself an arduino. When running these LEDs on almost full power (220) the all funtion properly, and everything goes as it should. When I adjust the brightness and lower it (120) only three of the LED's will actually light up. I've moved the LEDs around and all of them work, only the LEDs on pins 2 and 4 don't seem to work when the brightness is less than 128. What's wrong?
Here's my code:
int delayTime=100;
// Time difference between random LED
int brightness=128;
// The brightnes of the LED
// LEDs on pins 2 & 4 won't light unless brightness >= 128
void setup ()
{
pinMode(6,OUTPUT);
pinMode(5,OUTPUT);
pinMode(4,OUTPUT);
pinMode(3,OUTPUT);
pinMode(2,OUTPUT);
}
void loop()
{
int led=random(2,7);
// LEDs are setup on pins
// 2 through 6 (digital)
analogWrite(led,brightness);
delay(delayTime);
analogWrite(led,0);
}
To vary the brightness, you need to apply a Pulse Width Modulation (PWM) signal to your LED.
On the Arduino, only pins 3, 5, 6, 9, 10 and 11 support PWM.
On the Duemilanove these pins are marked with the letters "PWM".
On the Uno these pins are marked with the Tilde "~" (The Tilde looks kinda like a minature sine wave, so is sometimes used to indicate an AC or varying voltage.)
The other pins only recognize 1 for fully on and 0 for fully off, so you'll not see anything in between.
See www.richardvannoy.info/lab-05.php for my lab on having an LED flicker like a candle.
The other pins only recognize 1 for fully on and 0 for fully off, so you'll not see anything in between.
You can use analogWrite to non-PWM pins, but it acts as a simple digitalWrite, writing "LOW" if the PWM value is less than 128, and HIGH if greater than or equal to 128.