I’m messing around with my new Arduino UNO R3 and I just had a little personal success. I modified the Hello World of the Arduino, the Blink example, to utilize an analog pin on the board to cause the LED to fade smoothly in and out.
Nothing amazing, and probably not the most efficient way to do this… but here is the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | /* 1/27/2012 Fade in and out - original sketch without looking at example. */ int ledPin = 9; void setup() { pinMode(ledPin, OUTPUT); } void loop() { for(int x = 0; x < 200; x++) { analogWrite(ledPin, x); delay(10); } for(int x = 200; x > 0; x--) { analogWrite(ledPin, x); delay(10); } //while(1){} //end program (loop forever) } |
And just for kick, I’ll include the official Fade example that I just looked at. They did it a bit differently and more efficiently.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | int brightness = 0; // how bright the LED is int fadeAmount = 5; // how many points to fade the LED by void setup() { // declare pin 9 to be an output: pinMode(9, OUTPUT); } void loop() { // set the brightness of pin 9: analogWrite(9, 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 10 milliseconds to see the dimming effect delay(10); } |


Recent Comments