I am going through a series of tutorials to get familiar with the Arduino, and I am having trouble understanding why something isn't working the way that I expected it to. The following two blocks of code do the same thing, but I tried it both ways to see if there was any change, and the results were essentially identical (which the exception of the delay(800)). The problem that I am experiencing is that the LED dims far slower than it brightens. In fact the 0-255 loop seems to be happening in about a second, whereas the 255-0 loop is taking closer to five seconds. There does not seem to be a reason for this in the code, so I am at a loss. Any help or an explanation would be greatly appreciated!
#define ledPin 9 void setup() { // put your setup code here, to run once: pinMode(ledPin, OUTPUT); } void loop() { // put your main code here, to run repeatedly: for(int a = 0; a <= 255; a++) { analogWrite(ledPin, a); delay(8); } for (int a = 255; a >= 0; a--) { analogWrite(ledPin, a); delay(8); } }
And
//Controlling LED By PWM //The LED lights up gradually,and then goes out gradually,repeatedly //Email:support@sunfounder.com //Website:www.sunfounder.com //2015.5.7 /**************************************************************/ const int ledPin = 9; // the pin that the LED is attached to pin 9 void setup () { pinMode(ledPin, OUTPUT); // declare pin 9 to be an output } void loop() { for (int a=0; a<=255;a++) //loop from 0 to 255 { analogWrite(ledPin, a); // set the brightness of pin 9: delay(8); //wait for 8 ms } for (int a=255; a>=0;a--) //loop from 255 down to 0 { analogWrite(ledPin, a); // set the brightness of pin 9: delay(8); //wait for 8 ms } delay(800); //wait for 800 ms }