analog write anomily on Mega 2560

Hi guys,

Any ideas as to why un-commenting the last few lines of code where it says 'reverse train' causes the 6 previous lines of code to not work. I even changed throttle pin to 7 from 5 after reading about shared timer of pwm lengths and delay and milli functions.

const int cabBThrottlePin = 7;
const int cabBFwdRev = 17;
const int blk7CabSel= 35;
const int blk7EastWest=36;
void setup() {
   pinMode(blk7CabSel,OUTPUT);
   pinMode(blk7EastWest,OUTPUT);
   pinMode(cabBFwdRev,OUTPUT);
delay(2000);
     digitalWrite(blk7CabSel,HIGH);//cab b has power to block 7
  delay(1000);
  digitalWrite(blk7EastWest,HIGH);//block 7 set for eastbound
  delay(2000);


  //ensure cab b fwd/rev relay set for forward
digitalWrite(cabBFwdRev,LOW);
delay(1000);

  //start train
analogWrite(cabBThrottlePin,165);
delay(3000);
analogWrite(cabBThrottlePin,0);
delay(1000);
analogWrite(cabBThrottlePin,190);

//GO IN REVERSE
//digitalWrite(cabBFwdRev,HIGH);
//delay(1000);
//analogWrite(cabBThrottlePin,128);
//delay (3000);
//analogWrite(cabBThrottlePin,0);
//delay(1000);
//digitalWrite(cabBFwdRev,LOW);


}

void loop() {
  // put your main code here, to run repeatedly:

}

You do realise all your code is in setup and do only runs from top to bottom once , then stops .

Hi Hammy,

Yes, I'm only trying to run it once. That's what is so confusing for such a painfully simle program. The staggered delay times are so I can hear my relays clicking to identify what line of the code I'm at without the serial mon. Get this though, I can enter another relay command at the end of the reverse code and still hear it clicking. So even the delays work but only the analogWrite code isn't working???

Rick

You may be seeing this bug

There is a work around solution proposed at the end of the thread.

Hi Cattledog,

I think you're onto something. Thanks so much. Was about to try D46 where cab 'A' has been working from all this time but something kept telling me it was in the guts somewhere....I'm only talking to relays at low speed! I've got a lot of pins used but loading up the Mega shouldn't matter at all. I've included an almost up-to-date Mega block diagram(D17 isn't labelled yet) but it's all hooked up.

cattledog:
You may be seeing this bug
analogWrite - PWM Problem in IDE-Version 1.8.10 · Issue #339 · arduino/ArduinoCore-avr · GitHub

There is a work around solution proposed at the end of the thread.

Instead of removing all lto related stuff in platform.txt, I would rather downgrade the AVR boards or use Rocketct's solution dated 2020/02/25. For the latter, one has to locate the version of Arduino.h matching the AVR boards used and a boards update might break it again.

problem resolved by 1.8.12