[SOLVED] Arduino Due program does not seem to return from class member

Hello All,

For my new Quadcopter I have been working on some code.

What I am trying to do is declare an object and let the constructor of it do some setup. The problem starts when I call the first member function. The program does not seem to return from that function. It does fully execute the function as I do get the debug message “Ini display:OK” in my OLED display.

After that it seems to hang.

The code of my header file is:

#ifndef QUADCOPTER_H_
#define QUADCOPTER_H_

#include "Telemetry.h"
#include "RCCONTROL.h"
#include "Adafruit_GPS.h"
#include "Adafruit_SSD1306.h"

#define newLine 1
#define sameLine 0

class QuadCopter
{
public:
	QuadCopter();
	~QuadCopter();
	
	void secondaryLoop();
	void primaryLoop();

	void initializeTelemetry(USARTClass serialPort);
	void initializeRcInput(HardwareSerial &serialPort);
	void initializeAccelGyro();
	void initializeAltitude();
	void initializeCompass();
	void initializeGps(HardwareSerial &serialPort);
	void initializeDisplay();
	void initializeStorage();
	float rcChannelInput[16];
	void debugMessage(String message, bool nextLine);

private:

	Telemetry *telemetry;
	RCCONTROL *rcControl;
	Adafruit_GPS *GPS;
	Adafruit_SSD1306 *OLEDdisplay;


	uint8_t displayLineCounter;

};

#endif /* QUADCOPTER_H_*/

The code from my cpp file:

#include "QuadCopter.h"
#include <Wire.h>

QuadCopter::QuadCopter()
{
	delay(1000);					//Allow power to stabilize

	Wire.begin();

	initializeDisplay();						////@Arduino FORUM Program does not     seem to return from this function

	debugMessage("Quadcopter_V5", newLine);
	debugMessage("By DVDV 2016", newLine);
	
	initializeRcInput(Serial1);
	initializeTelemetry(Serial2);
	initializeGps(Serial3);

}


QuadCopter::~QuadCopter()
{
}


void QuadCopter::initializeTelemetry(USARTClass serialPort) {

	
	debugMessage("Ini telemetry:", sameLine);

	Telemetry _telemetry(&serialPort);
	telemetry = &_telemetry;

	debugMessage("OK", newLine);
}

void QuadCopter::initializeRcInput(HardwareSerial &serialPort) {

	debugMessage("Ini RC:", sameLine);

	RCCONTROL _rcControl(serialPort);
	rcControl = &_rcControl;

	rcControl->setChannelType(1, ANALOG, -50, 50);
	rcControl->setChannelType(2, ANALOG, -50, 50);
	rcControl->setChannelType(3, ANALOG, 0, 100);
	rcControl->setChannelType(4, ANALOG, -50, 50);
	rcControl->setChannelType(5, ANALOG, 0, 5);
	rcControl->setChannelType(6, ANALOG, 0, 5);
	rcControl->setChannelType(7, ANALOG, 0, 5);
	rcControl->setChannelType(8, ANALOG, 0, 5);
	rcControl->setChannelType(9, DIGITAL_2);
	rcControl->setChannelType(10, DIGITAL_3);
	rcControl->setChannelType(11, DIGITAL_3);
	rcControl->setChannelType(12, DIGITAL_3);
	rcControl->setChannelType(13, DIGITAL_3);
	rcControl->setChannelType(14, DIGITAL_3);
	rcControl->setChannelType(15, DIGITAL_3);
	rcControl->setChannelType(16, DIGITAL_2);

	debugMessage("OK", newLine);

}

void QuadCopter::initializeAccelGyro() {

	debugMessage("Ini AccelGyro:", sameLine);

	debugMessage("OK", newLine);

}

void QuadCopter::initializeAltitude() {

	debugMessage("Ini Altitude:", sameLine);

	debugMessage("OK", newLine);
}

void QuadCopter::initializeCompass() {

	debugMessage("Ini Compass:", sameLine);

	debugMessage("OK", newLine);
}

void QuadCopter::initializeGps(HardwareSerial &serialPort) {

	debugMessage("Ini GPS:", sameLine);

	//Adafruit_GPS _GPS(&serialPort);
	//GPS = &_GPS;

	debugMessage("OK", newLine);

}

void QuadCopter::initializeDisplay() {

	Adafruit_SSD1306 _OLED(4);
	OLEDdisplay = &_OLED;

	OLEDdisplay->begin(SSD1306_SWITCHCAPVCC, 0x3D);  // initialize with the I2C addr 0x3D or 0x3E(for the 128x64)	
	OLEDdisplay->clearDisplay();
	OLEDdisplay->setCursor(0, 0);
	OLEDdisplay->setTextSize(1);
	OLEDdisplay->setTextColor(WHITE);
	OLEDdisplay->display();

	debugMessage("Ini display:", sameLine);
	debugMessage("OK", newLine);				//@Arduino FORUM Program untill here

}

void QuadCopter::initializeStorage() {

	debugMessage("Ini SD mem:", sameLine);

	debugMessage("OK", newLine);

}

void QuadCopter::secondaryLoop() {

	//rcControl->process();	//	@DENNIS -> Moet ongeveer 100Hz draaien? Input van receiver is 66Hz, dus nog testen op 50Hz.
	//telemetry->sendData();

}

void QuadCopter::primaryLoop() {

	

}

void QuadCopter::debugMessage(String message, bool nextLine) {

	if (displayLineCounter > 6) {

		displayLineCounter = 0;
		OLEDdisplay->clearDisplay();
		OLEDdisplay->setCursor(0, 0);
	}

	if (nextLine) {
		OLEDdisplay->println(message);
		OLEDdisplay->display();
		displayLineCounter++;
	}
	if(!nextLine) {
		OLEDdisplay->print(message);
		OLEDdisplay->display();
	}

}

The code from the ino file:

#include <Wire.h>
#include <spi.h>
#include "QuadCopter.h"

void setup()
{
	QuadCopter myQuadcopter;

}

void loop()
{

  /* add main program code here */

}

Can anyone help me in the right direction for this problem? Any other tips & trics for the code I have so far are welcome.

Thanks in advance.

Brand

Found the problem :slight_smile:

It is going wrong with the pointer:

When I change the location of this piece of code:
Adafruit_SSD1306 _OLED(4);
OLEDdisplay = &_OLED;

Anyone who can give me some clarification on this?

It al works fine. However I do not have a clear explaination for it. The updated code can be found here:

#include "QuadCopter.h"
#include <Wire.h>

QuadCopter::QuadCopter()
{
	pinMode(13, OUTPUT);
	digitalWrite(13, LOW);
	
	delay(500);					//Allow power to stabilize

	Wire.begin();

	Adafruit_SSD1306 _OLED(4);        //@Arduino forum: code has moved to here
	OLEDdisplay = &_OLED;

	initializeDisplay();					
	
	debugMessage("Quadcopter_V5", newLine);
	debugMessage("By DVDV 2016", newLine);

	initializeRcInput(Serial1);
	initializeTelemetry(Serial2);
	initializeGps(Serial3);


	digitalWrite(13, HIGH);

	
}


QuadCopter::~QuadCopter()
{
}


void QuadCopter::initializeTelemetry(USARTClass serialPort) {

	
	debugMessage("Ini telemetry:", sameLine);

	Telemetry _telemetry(&serialPort);
	telemetry = &_telemetry;

	debugMessage("OK", newLine);

}

void QuadCopter::initializeRcInput(HardwareSerial &serialPort) {

	debugMessage("Ini RC:", sameLine);

	RCCONTROL _rcControl(serialPort);
	rcControl = &_rcControl;

	rcControl->setChannelType(1, ANALOG, -50, 50);
	rcControl->setChannelType(2, ANALOG, -50, 50);
	rcControl->setChannelType(3, ANALOG, 0, 100);
	rcControl->setChannelType(4, ANALOG, -50, 50);
	rcControl->setChannelType(5, ANALOG, 0, 5);
	rcControl->setChannelType(6, ANALOG, 0, 5);
	rcControl->setChannelType(7, ANALOG, 0, 5);
	rcControl->setChannelType(8, ANALOG, 0, 5);
	rcControl->setChannelType(9, DIGITAL_2);
	rcControl->setChannelType(10, DIGITAL_3);
	rcControl->setChannelType(11, DIGITAL_3);
	rcControl->setChannelType(12, DIGITAL_3);
	rcControl->setChannelType(13, DIGITAL_3);
	rcControl->setChannelType(14, DIGITAL_3);
	rcControl->setChannelType(15, DIGITAL_3);
	rcControl->setChannelType(16, DIGITAL_2);

	debugMessage("OK", newLine);

}

void QuadCopter::initializeAccelGyro() {

	debugMessage("Ini AccelGyro:", sameLine);

	debugMessage("OK", newLine);

}

void QuadCopter::initializeAltitude() {

	debugMessage("Ini Altitude:", sameLine);

	debugMessage("OK", newLine);

}

void QuadCopter::initializeCompass() {

	debugMessage("Ini Compass:", sameLine);

	debugMessage("OK", newLine);

}

void QuadCopter::initializeGps(HardwareSerial &serialPort) {

	debugMessage("Ini GPS:", sameLine);

	//Adafruit_GPS _GPS(&serialPort);
	//GPS = &_GPS;

	debugMessage("OK", newLine);

}

void QuadCopter::initializeDisplay() {

	OLEDdisplay->begin(SSD1306_SWITCHCAPVCC, 0x3D);  // initialize with the I2C addr 0x3D or 0x3E(for the 128x64)	
	OLEDdisplay->clearDisplay();
	OLEDdisplay->setCursor(0, 0);
	OLEDdisplay->setTextSize(1);
	OLEDdisplay->setTextColor(WHITE);
	OLEDdisplay->display();

	debugMessage("Ini display:", sameLine);
	debugMessage("OK", newLine);

}

void QuadCopter::initializeStorage() {

	debugMessage("Ini SD mem:", sameLine);

	debugMessage("OK", newLine);


}

void QuadCopter::secondaryLoop() {

	//rcControl->process();	//	@DENNIS -> Moet ongeveer 100Hz draaien? Input van receiver is 66Hz, dus nog testen op 50Hz.
	//telemetry->sendData();

}

void QuadCopter::primaryLoop() {

	

}

void QuadCopter::debugMessage(String message, bool nextLine) {

	if (displayLineCounter > 6) {

		displayLineCounter = 0;
		OLEDdisplay->clearDisplay();
		OLEDdisplay->setCursor(0, 0);
	}

	if (nextLine) {
		OLEDdisplay->println(message);
		OLEDdisplay->display();
		displayLineCounter++;
	}
	else {
		OLEDdisplay->print(message);
		OLEDdisplay->display();
	}

}

Here:

    Adafruit_SSD1306 _OLED(4);
    OLEDdisplay = &_OLED;

You create an instance of the Adagruit_SSD1306 object on the stack. You then set OLEDdisplay to point to that object on the stack. When the initializeDisplay function returns, _OLED goes out of scope, and the stack RAM that is used, is released. Sometime later (next time you make a function call, create a local variable, etc.) that area of the stack is over-written with something else.

Read up on variable scope.

Regards, Ray L.