Connection OpenFrameworks to Arduino

Hii
Im working on the connection between oF and Arduino, but for some reason I can’t get it to work. I get these errors:
C3646: ‘serial’: unknown override specifier missing type specifier - int assumed
C4430: missing type specifier - int assumed. Note: C++ does not support default-int.

I connected my Adruino to my laptop and that is working fine.

This is my code:

Arduino.h:

#pragma once
#pragma warning
#ifndef Arduino_h
#define Arduino_h

#define _OF_SEND_SERIAL


class Arduino {
public:
	ofSerial serial;

	void setup();
	void draw();
	void update();

	bool serialMessage;			// a flag for sending serial			
	char receivedData[10];		// for storing incoming data
	char sendData = 1;	// for sending data

};

#endif

Arduino.cpp:

#include "Arduino.h"

#include <cstdio>

#include "ofMain.h"

#include "constants.h"


void Arduino::setup() {
	ofSetVerticalSync(true);
	serialMessage = false;
	serial.listDevices();
	vector <ofSerialDeviceInfo> deviceList = serial.getDeviceList();

	int baud = 9600;
	serial.setup(0, baud);
	
}

void Arduino::update() {
	
	if (serialMessage) {
		serial.writeByte(sendData);
		serialMessage = false;
		printf("sending");
	}

	while (serial.available() > 0) {
		
		int myByte = serial.readByte();
		cout << "test:" << myByte << endl;
		cout << "ik werk" << endl;

	}
	
}

void Arduino::draw() {
	string msg;
	//msg += "Click to turn LED \n";
	//msg += receivedData;
	ofDrawBitmapString(msg, 200, 200); // Write the data on the output window

	};

Thanks!!

0 is not a port. You need to specify a port (COM3 for exemple in windows)

Here is some code I use in the case there is only one arduino connected.

serial.listDevices();

vector <ofSerialDeviceInfo> deviceList = serial.getDeviceList();

string port = "";

for (auto device : deviceList) {

port = device.getDevicePath();

}

int baud = 115200;

serial.setup(port, baud); //open the first device

Thanks for your help taprik!! unfortunatelly it doesn’t work…

I changed my setup in my Arduino.cpp to this:

void Arduino::setup() {
	ofSetVerticalSync(true);
	serialMessage = false;
	serial.listDevices();

	vector <ofSerialDeviceInfo> deviceList = serial.getDeviceList();

	string port = "COM3";

	for (auto device : deviceList) {
		port = device.getDevicePath();
	}

	int baud = 115200;

	serial.setup(port, baud); //open the first device
	
}

It gives 43 errors now with my directions all over my project. Still have the three errors I had at first as well

I am not sure how far through your project you are but if you are in the early stages take a look at this:

and the packetSerial example.

You will need to use PacketSerial in your arduino code as well

The examples are well laid out and work out of the box and I find these make communication between arduino and OF streamlined and easy.

1 Like

What you write here does something different than just opening COM3:

vector deviceList = serial.getDeviceList();

Here you are getting the list of devices, check which one you see in the console.

string port = “COM3”;

Here you are assigning the name of the port arbitrarily to COM3, instead check in your console or in the arduino IDE what is the actual name of the port on your computer. On Mac it looks like:
/dev/cu.Bluetooth-Incoming-Port
/dev/cu.usbmodem1421

for (auto device : deviceList) {
port = device.getDevicePath();
}

Here you are reassigning the port to the last device into your port.

int baud = 115200;

serial.setup(port, baud); //open the first device

I will use the index, as you did at the beginning but I will choose the index that correspond to your arduino. In my case, I will use index 1, based on the device.list().

Regarding libraries: to build graphMe (https://itunes.apple.com/us/app/graphme/id1186285408) I used ofxSerial and PacketSerial.
ofxSerial in particular offers much better support than the embedded serial library.

Hi Frederique_Voskeuil,

what do you want to send from your Arduino? If it is just a few sensor values, there is a simple way without any addons or arduino library.

Best,
David

Yes it works!!!
This is my code now:

My adruino.hpp

#pragma once
#ifndef Arduino_hpp
#define Arduino_hpp

#include "Direction.hpp"
#include "GridElement.hpp"
//#include "Grid.hpp"
#include <cstdio>

#include "ofMain.h"

#include "constants.h"

#define _OF_SEND_SERIAL

class Arduino {

public:
	ofSerial serial;
	int myByte;

	void readSerial();
	void setup();

};

#endif /Arduino/

Arduino.cpp

#include "Arduino.hpp"

void Arduino::setup() {
	serial.listDevices();
	vector <ofSerialDeviceInfo> deviceList = serial.getDeviceList();
	myByte = 0;
	serial.setup(0, 9600);

}

void Arduino::readSerial() {
	
	while (serial.available()) {
		myByte = serial.readByte();
	}
};