Arduino Nano Flaky Code Execution - AVR Bug?

Wondering if anyone has come across this issue.

Basically adding or removing a line of code that has nothing to do with an output on or off,state disables control of some outputs. In my case I'm noticing one in particular - pin 11.

Background
The below code is snip-it of a function which is part of the a 27,644 bytes of code being uploaded to my genuine Nano. The function is part of a temperature control system. It determines if a set of fans (PWM) are on or off. Fans do not turn off with a PWM value of 0 therefore the DC power must be cut to the fans in order to turn them off.

  • Arduino "FAN_EN" (#defined elsewhere as pin 11) drives an opto-isolator that feeds a relay controlling power to the fans.
  • Another function outside the below code determines the PWM speed of those fans. Prior the this function call, the PWM value is verified correctly.
  • "Closed" refers to a door status being supplied by a second Nano (MC1_STATUS). When open (logic 0), the fan enable pin goes low and cuts power to the fans.
  • FanOverride is used to manually override fan on/off
  • UseTemp and tmFanOnTemp essentially determine if the ambient temp is higher than the fan on set temp.
  • SW2 and SW3 are just dip switches feeding two other inputs.

All that is quite irrelevant but thought it would give you a better picture and would likely get asked.

Here is the rub... The below snip-it of code works perfectly. Yet if I only change the code by commenting out the serial.print.. on lines 3 and 4 (put there for debug purposes only) all the output pins become dysfunctional. I can confirm my code is running because if I can manually override the fans on or off via a serial command I can see the FAN_EN output (pin 11) change between 0V and 1.2V (scoped). It never goes to 5V as it does when the Serial.print lines are un-commented/active. I also have an Serial monitor window opened that replies with data to my several serial commands so the MC must be executing the code in flash memory.

No other changes to any other code. Just commenting out the two serial.print commands kacks the entire output functionality.

Have I discovered an AVR bug?? I read somewhere that using a big chunk of the flash memory causes strange things to happen. But yet, by commenting out the two lines, I'm actually occupying less flash memory? I'm puzzled.

Any info our similar experience would be greatly appreciated.

Here is my example code:

void CoolOn(){
	if ((SW2 || SW3) && !(SW2 && SW3)) Closed = 1;
        Serial.print(Closed); // does not work if I comment out this print command.
	Serial.print(bitRead(MC1_STATUS, bitClosedstatus)); // does not work if I comment out this print command.
	if ((Closed == 1 && UseTemp > tmFanOnTemp[profile]) && FanOnOverRide == 1)
	{
		digitalWrite(HEATER_PIN, LOW);
		digitalWrite(FANEN, HIGH);
	}

	else
	{
		digitalWrite(FANEN, LOW);
	}

}

Bunzie34_569:
Fans do not turn off with a PWM value of 0 therefore the DC power must be cut to the fans in order to turn them off.

I suspect that is provably false. Let's check...

Yup. False. You wasted your time working around a problem that is not a problem.

Bunzie34_569:
Have I discovered an AVR bug??

No.

Thanks for your quick reply but your link....

"ArduinoCore-avr/wiring_analog.c at master · arduino/ArduinoCore-avr · GitHub"

doesn't explain why a Serial.println statement being commented or active, would disrupt output pin functionality. Pin 11 is set to output. I have a PWM out signal on pin 10 (valid by all accounts). Same with either the Serial.print command in or out. Maybe I'm not explaining myself clearly?

Bunzie34_569:
Maybe I'm not explaining myself clearly?

Either that or you failed to read the part of your post that I quoted.

  1. I see a ""FAN_EN output (pin 11)"" in your description and a ""FANEN"" in your code. Are the same?

  2. Try putting a small delay, when "removing" the serial state ments (perhaps your rest code needs a time to react properly)

'kacks the entire output functionality. '
What does this mean?

Just attach all your code, even if it is long. A snippet like that is frequently worthless.

I am not an accomplished programmer by any stretch of the imagination. However if this were my code I was troubleshooting I would:

1st) put braces around "Closed = 1" Some will say it a waste of time but that time is short.

2nd) I would extract this code, set it up in a simple sketch and see if that changes the result.

John