So, my small group of guys is messing around with the Arduino IDE when this code below manages to make the one of the LEDs fade and the other blink.
int fading= 3;
int blinking= 4;
int brightness = 0;
int fadeAmount = 5;
void setup() {
pinMode(thisPin, OUTPUT);
pinMode(thisotherPin, OUTPUT);
}
void loop() {
analogWrite(fading, brightness);
brightness = brightness + fadeAmount;
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
delay(20);
analogWrite(blinking, brightness);
brightness = brightness + fadeAmount;
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
delay(20);
}
I have altered the code in that the pin that fades says fading, and the pin that blinks says blinking. I understand why the fading pin fades, but why does the blinking pin blinks? This piece of code also does the same thing on other Arduino boards and breadboards. Is this a bug, or is the code doing something that we're just not getting?
Pin 4 is not one PWM pin.
PWM: 3, 5, 6, 9, 10, and 11. Provide 8-bit PWM output with the analogWrite() function.
tis information is copyed from thins page:
http://arduino.cc/en/Main/ArduinoBoardUno
Even with the PWM pin oversight, I'm surprised that you can get the sketch to even compile, much less get it to fade an LED.:
void setup()
{
pinMode(thisPin, OUTPUT);
pinMode(thisotherPin, OUTPUT);
}
since you didn't declare thisPin or thisotherPin.
there are other issues here as well.
@BulldogLowell: I believe that the names of the pins were changed to show what was the pin that fade and what was the pin that blink, and he pins from the setup() function were missing in this process. So, I think that the problem that you point don't make sense.
right,
if the sketch they posted is wrong, their real sketch may be right. XD
Yes, only something that they forget to rename.
It is quite clear that the fault is that Pin 4 is used instead of a PWM pin.
The code will behave like it is observed.
Is this a bug,
No.
or is the code doing something that we're just not getting?
Yes, you are using a pin in a mode that will not work for that pin.
Simple as that.