I have an Arduino MEGA 2560 and a red LED connected to pin 4.
#define RED 4
#define GREEN 2
void setup() {
analogWrite(GREEN, 0);
analogWrite(RED, 100);
}
void loop() {
}
This code lights up the red LED.
However, when I write 1 to the GREEN pin "analogWrite(GREEN, 1)", the red LED goes off. I'm expecting the red LED to stay on.
#define RED 4
#define GREEN 2
void setup() {
analogWrite(GREEN, 1);
analogWrite(RED, 100);
}
void loop() {
}
The red LED stays on when both written values are the same, e.g.
analogWrite(GREEN, 25);
analogWrite(RED, 25);
I also found that adding some "garbage" code at the beginning fixes the issue. I wasn't able to get the garbage into a simpler form without both pins shutting off. (see third attached picture)
#define RED 4
#define GREEN 2
void setup() {
// GARBAGE BEGIN ===
unsigned long t = millis();
const unsigned long period = 10;
float progress = float(t % period)/period;
float r = progress * 0;
// GARBAGE END ===
analogWrite(GREEN, 100 + r);
analogWrite(RED, 50 + r);
}
void loop() {
}
I also posted about the issue here: arduino ide - MEGA 2560: Simple program - One analogWrite turns off the other - Arduino Stack Exchange for
When I run this on my Mega the red LED is ON
#define RED 4
#define GREEN 2
void setup() {
// GARBAGE BEGIN ===
//~ unsigned long t = millis();
//~ const unsigned long period = 10;
//~ float progress = float(t % period)/period;
//~ float r = progress * 0;
// GARBAGE END ===
analogWrite(GREEN, 1);
analogWrite(RED, 100);
}
void loop() {
}
How have you got the LEDs connected to the Mega - make a simple pencil drawing and post a photo of it. See this Simple Image Posting Guide
...R
Thanks Robin2. This has been diagnosed as a compiler bug on Windows. Let me update my post here to insert the pictures correctly.
More details here:
opened 07:08PM - 24 Jan 20 UTC
closed 06:54PM - 22 May 20 UTC
Type: Bug
Compiling under Linux results in a fine working program.
When I compile the s… ame source under W10 the program has a lot of faults like:
- resetting automatically
- changing the value on the pins without the program says to
- exiting the program when it has not yet finished
- measuring a LOW pin when it is not LOW
To investigate this, I used two different Windows 10 laptops, also I did a clean install again on one of them, IDE version 1.8.10.
For Linux I use an older laptop with Ubuntu 18.04.3 LTS and IDE 2:1.05+dfsg2-4.1 is used.
Source:
[AllIn_2020_Base_Keep.TXT](https://github.com/arduino/Arduino/files/4110193/AllIn_2020_Base_Keep.TXT)
Is this the same bug? It's core related and not Windows.
opened 08:17AM - 22 Jan 20 UTC
closed 09:11AM - 22 Jun 20 UTC
There is a Bug in Version 1.8.10 of the Arduino IDE using analogWrite() for PWM … on Arduino Mega 2560. If i use analogWrite() with any value for the pwm-Pins once in the loop()-Method everything works fine. If I put a second call of analogWrite() with the same pin and any value that is not 0 or 255, nothing happens. The loop-Method keeps on working, but the PWM-Pin used in analogWrite() shows an continous output of 0V. In Version 1.8.9 the same programm works fine.
Yeah it looks like the behavior I'm seeing.