PWM output on Mega - very strange code behaviour, only sometimes works!

Hi guys,

I am hoping you can help with this very odd problem. I'm using a Mega as part of an Industrial Shields MDuino 21+ controller (Product Link).

I am trying to control one of the PWM output pins, which works fine until I add a certain line of code, and then previous code stops working! I am at a total loss here.. but also fairly new to Arduino programming.

Here is the code: (please note, for the MDuino you do not need to setup pins with output types etc.)

void setup() {
  // put your setup code here, to run once:

}


void loop() {

  //Motor 1 - Run FWD, full speed
  digitalWrite(Q0_2, LOW);
  digitalWrite(Q0_1, HIGH);
  analogWrite(Q0_7, 255);
  delay(3000);

  //Motor 1 - Run REV, full speed
  digitalWrite(Q0_1, LOW);
  digitalWrite(Q0_2, HIGH);
  analogWrite(Q0_7, 255); //change to 255
  delay(3000);

  //Motor 1 - Run FWD, low speed
  digitalWrite(Q0_2, LOW);
  digitalWrite(Q0_1, HIGH);
  analogWrite(Q0_7, 60);
  delay(3000);

  //Motor 1 - Run REV, low speed
  digitalWrite(Q0_1, LOW);
  digitalWrite(Q0_2, HIGH);
  //  analogWrite(Q0_7, 60); //this line breaks the code...?
  delay(3000);

}

The PWM outputs as it should as long as the commented 'analogWrite(Q0_7, 60);' line stays commented, but as soon as I uncomment it, the previous 'analogWrite(Q0_7, 60);' and the now uncommented line do not work.

How can later code be affecting what is happening earlier, and why on earth would this happen?

many thanks

Could you add a few serial prints to get a closer idea on where the issue may be?

Something like

  serial.println( "Motor 1 - Run FWD, low speed" );
  digitalWrite(Q0_2, LOW);
  digitalWrite(Q0_1, HIGH);
  analogWrite(Q0_7, 60);
  delay(3000);

  Serial.println( "Motor 1 - Run REV, low speed" );
  digitalWrite(Q0_1, LOW);
  digitalWrite(Q0_2, HIGH);
  //  analogWrite(Q0_7, 60); //this line breaks the code...?
  delay(3000);

Then let us know what serial prints you get.

Thanks for your response.

Editing the code like this:

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}


void loop() {

  //Motor 1 - Run FWD, full speed
  Serial.println( "Motor 1 - Run FWD, full speed" );
  digitalWrite(Q0_2, LOW);
  digitalWrite(Q0_1, HIGH);
  analogWrite(Q0_7, 255);
  delay(3000);

  //Motor 1 - Run REV, full speed
  Serial.println( "Motor 1 - Run REV, full speed" );
  digitalWrite(Q0_1, LOW);
  digitalWrite(Q0_2, HIGH);
  analogWrite(Q0_7, 255); //change to 255
  delay(3000);

  //Motor 1 - Run FWD, low speed
  Serial.println( "Motor 1 - Run FWD, low speed" );
  digitalWrite(Q0_2, LOW);
  digitalWrite(Q0_1, HIGH);
  analogWrite(Q0_7, 60);
  delay(3000);

  //Motor 1 - Run REV, low speed
  Serial.println( "Motor 1 - Run REV, low speed" );
  digitalWrite(Q0_1, LOW);
  digitalWrite(Q0_2, HIGH);
  //  analogWrite(Q0_7, 60); //this line breaks the code...?
  delay(3000); 
}

Gives me this output:

 10:24:51.365 -> Motor 1 - Run FWD, full speed
10:24:54.355 -> Motor 1 - Run REV, full speed
10:24:57.382 -> Motor 1 - Run FWD, low speed
10:25:00.384 -> Motor 1 - Run REV, low speed
10:25:03.376 -> Motor 1 - Run FWD, full speed
10:25:06.382 -> Motor 1 - Run REV, full speed
10:25:09.373 -> Motor 1 - Run FWD, low speed
10:25:12.348 -> Motor 1 - Run REV, low speed
10:25:15.366 -> Motor 1 - Run FWD, full speed
10:25:18.352 -> Motor 1 - Run REV, full speed

And with that line uncommented:

 10:27:09.349 -> Motor 1 - Run FWD, low speed
10:27:12.351 -> Motor 1 - Run REV, low speed
10:27:15.344 -> Motor 1 - Run FWD, full speed
10:27:18.338 -> Motor 1 - Run REV, full speed
10:27:21.352 -> Motor 1 - Run FWD, low speed
10:27:24.335 -> Motor 1 - Run REV, low speed
10:27:27.351 -> Motor 1 - Run FWD, full speed
10:27:30.355 -> Motor 1 - Run REV, full speed
10:27:33.353 -> Motor 1 - Run FWD, low speed
10:27:36.334 -> Motor 1 - Run REV, low speed

Looks like all the code executes in both cases?

And the printouts continuously, with the comment removed, print for each iteration of loop(), or does the code just print one time and stop printing?

Some of the things that suggest themselves.

Compiler bug. Unlikely, but possible - what computer OS and Arduino software version?
Bad macro/const definitions for all these Q0_7 etc pin names? Something to trigger an issue when used
multiple times.

Its worth experimenting to see if the issue is dependent on specific pin names, or specific to analogWrite,
or specific to the 2nd use of the pin etc etc.

Double check that you haven't simply run out of RAM or Flash memory first, that will show up in all sorts of strange ways.

Idahowalker:
And the printouts continuously, with the comment removed, print for each iteration of loop(), or does the code just print one time and stop printing?

yes printouts continuous and for each iteration.

However, when I was testing later, the printouts started randomly stopping and having some "?" characters in there, and seemed to be constantly restarting the code. Very strange, I'm sure it wasn't doing this before (you can see the printouts I copied into my earlier post). So will do some more testing and see if this behaviour is continuing. Still unsure why the code was not executing properly before though, when the printouts were behaving.

@MarkT
what computer OS and Arduino software version? - Windows 10, Arduino latest 1.8.12

Running out of memory seems very unlikely as the sketch is short with just 3 named variables?

Will try some experimenting as you suggest, cheers!

There is some sort of bug with the current AVR boards core for the MEGA.

Replace the motor with an LED and resistor. Do you still get the same problem?

If not then the problem is the interference the motor is kicking out and you need a hardware fix.