4

I'm using Windows 10, Arduino 1.8.12 (Windows Store 1.8.33.0)

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. led on 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() {

}

LED off

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.


#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() {

}

two leds on

Update: Thanks all for your comments. Setting the pinMode to output did not fix the issue. However, changing the function declaration to __attribute__((optimize("-O0"))) void setup() { fixed the issue. -O1 also works, but -O2 fails.

Mark
  • 187
  • 1
  • 8
  • Do both LEDs have suitable current-limiting resistors? – Edgar Bonet May 01 '20 at 16:05
  • Actually there is only one LED attached, and it's connected through a 220 Ohm Resistor. – Mark May 01 '20 at 16:07
  • 1
    Please, show your circuit schematic. – Edgar Bonet May 01 '20 at 16:08
  • 1
    Wait, what? There's only one LED attached, but when you turn on one of your LEDs the other one turns off? That makes no sense. – Duncan C May 01 '20 at 16:11
  • 1
    @DuncanC it's not that I turn on another LED, it's that I analogWrite to an additional pin and it turns off the LED pin. – Mark May 01 '20 at 16:17
  • do both pins exhibit the same behavior? – jsotola May 01 '20 at 16:22
  • @jstola Yes, it seems that doing simultaneous writes will disable both of them. – Mark May 01 '20 at 16:27
  • looks like a bug in the library ... do you have the correct board selected in the arduino IDE? ... does the behavior remain if you insert a command line between the two ... something as simple as `a++;` – jsotola May 01 '20 at 18:03
  • 1
    Try before the write pinMode(GREEN, OUTPUT); pinMode(RED, OUTPUT); and give feedback if it worked – Codebreaker007 May 01 '20 at 19:07
  • @Codebreaker007 I started to post that the OP was missing the calls setting the pins to outputs, but if you look up the docs on `analogWrite()` it says specifically that you don't have to set the pin to output before using it, which surprised me. It's probably still worth trying a try though. – Duncan C May 01 '20 at 19:53
  • You should post an answer to your own question. with the solution you found. I would suggest trying calls to `pinMode(GREEN, OUTPUT); pinMode(RED, OUTPUT)` as Codebreaker suggests, as well as adding a short delay in your setup in place of the "garbage code" you posted. – Duncan C May 01 '20 at 19:56
  • 1
    If you use ArfuinoIDE < 1.6 you have to define pin mode, since then its done in analogWrite.c.If using IDE >=1.8.10 its an optimization problem by the compiler for the Mega try to replace the line void setup() with void setup() __attribute__((optimize("-O0"))); // also -O1 works the default -Os and -O3,-O4 not – Codebreaker007 May 01 '20 at 20:03
  • If you look in the source code for analogWrite, one of the first things it does is set the pinMode. – Delta_G May 01 '20 at 20:24
  • Yes since around 1.6 on as I wrote as we do not know OPs version we have to exclude basic things not everybody is always on the latest version – Codebreaker007 May 01 '20 at 20:40
  • @Codebreaker007 Setting the optimization level to 0 or 1 fixes the issue (see my update in my post). I'm wondering if you can direct me to an explanation of why this occurs. – Mark May 02 '20 at 02:06
  • However, would you mind to answer your own question, and mark it (later, if you're forced to wait), please? This will help others investigating the same problem, because they can upfront see that your problem is solved. – the busybee May 02 '20 at 07:23

1 Answers1

5

As you are using using ArduinoIDE >=1.8.10 its an "optimization" problem by the compiler for the Mega try to replace the line

 void setup() 

with

 void setup() __attribute__((optimize("-O0"))); 

// also -O1 works the default -Os and -O3,-O4 not

Actually the problem is not the IDE version but the included AVR boards package version.
You can install any AVR boards package in any IDE with Boards Manager. Last good AVR boards package is 1.6.21. If your compiler (tool chain) shows 7.03 in the output window you are affected. I have examples where the compiler even optimizies the loop away, which ends in funny results - I dont know wether other boards are also affected but the MEGA is for sure.

Codebreaker007
  • 1,281
  • 1
  • 4
  • 14