Hi
Just trying my new Arduino Uno, I thought writing a fade program for a led was a good idea to test it... So I wrote this in two minutes:
int pin = 11;
void setup() {
// put your setup code here, to run once:
pinMode(pin, OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
int i = 0;
while (i < 255) {
digitalWrite(pin, i);
i++;
delay(10);
Serial.println(i);
}
while (i > 0) {
digitalWrite(pin, i);
i--;
delay(10);
}
}
I know it's not well optimized but... that's not the point now. And it won't work. The LED stays HIGH everytime, and then shuts down ten milliseconds (I guess) and is HIGH until it's down ten milliseconds, no fade effect, only HIGH a long time and LOW a very very short time. So I tried the example Fade program :
/*
Fade
This example shows how to fade an LED on pin 9
using the analogWrite() function.
This example code is in the public domain.
*/
int led = 11; // the pin that the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 1; // how many points to fade the LED by
// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// set the brightness of pin 9:
analogWrite(led, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
// wait for 30 milliseconds to see the dimming effect
delay(5);
}
And it works... So can anyone say me what is wrong in my program (just to know :p)
Thank you in advance