Arduino Mega 2560 clone not always executing code

Hi All,

I'm facing a difficult bug which has developed from appearing sometime to almost always.

I've build an automation system for a camper which controls almost everything like light, charging, watermanagement, etc. The problem that i'm facing is that the Mega 2560 mostly doesn't execute code after getting power.

In the beginning this happens sometimes. Now this happens always. Sometimes it was fixable by pressing the resetbutton or just wait. Sometimes it was starting at couple of minutes, sometimes it take several hours. I've changed the board with a second one and this didn't fix the issue.

The question is where to look...
I've started with debugging the code by sending data to Serial and monitor that with Putty. It looks like the code isn't entering the setup() function or hangs after calling Serial.begin(). When it does enter, the rest is also working. I Also tryed to load another test project with just a serial message and this works perfectly..

Could it be that the controller is waiting on someting? Or could it be that there goes someting wrong in the object, constant and variable declarations which causes a memory issue?

void setup()
{
	//debugger
	if (SHOW_DEBUG)
	{
		Serial.begin(115200);
        Serial.setTimeout(100);
		Serial.availableForWrite();
	}

	debug("Start initialising...");

    debug("Init Serial 1 - Display");
	//Serial data LCD
	Serial1.begin(115200);
    Serial1.setTimeout(200);
    Serial1.availableForWrite();
	
    debug("Init Serial 2 - Lightcontroller");
	// Arduino Nano lightcontroller
	Serial2.begin(115200);
	Serial2.setTimeout(200);
    Serial2.availableForWrite();

    debug("Init temperature sensor");
	temperatureSensorInside.begin();
	temperatureSensorOutside.begin();

	//Set sensor resolution to 0.125
	temperatureSensorInside.setResolution(sensorTemperatureInside, 11);
	temperatureSensorOutside.setResolution(sensorTemperatureOutside, 11);

	debug("Init SD card");
	//Read config data from SD
	config.readSdData("config.txt");	

    debug("Set HMI brightnesslevels to default");
	//Fill HMI Brightness to 100 to start at max brightness
	for (size_t i = 0; i < SAMPLES_BRIGHTNESS; i++)
	{
		HMIBrightnessLevel[i] = 100;
	}

	debug("Init pins");

	pinMode(BTN_BED_Pin, INPUT_PULLUP);
	pinMode(PIN_WALSTROOM, INPUT_PULLUP);
	pinMode(BTN_BED_Feedback, OUTPUT);

    debug("Done");
	
}

void debug(String text)
{
	if (SHOW_DEBUG && Serial.availableForWrite())
		Serial.println(text);	
}

Full code would be helpful. A full wiring diagram with everything connected is more than likely needed.

Can you still upload? Does uploading of blink still result in blink working as expected.

2 Likes

Yes, is can still upload.

I think I also found the problem by incident. There was a double call to serial.begin in different classes :man_facepalming:t2:. By removing that and use a reference to serial it looks like I managet to solve the issue…

We will start testing on the device this afternoon and I will share the result. For now it looks like it started every time on a dev board. :crossed_fingers:t2:

So the problem was in the part of the code you didn't post. That's the case about 50% of the time when incomplete code is posted.

5 Likes

Is this for personal use in an old non-networked camper, or are you trying to make a commercial unit?
Will you be interfacing with the several existing RV data standards and networks? How many millions do you have planned to lobby against the established players?
Good luck.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.