How can separate values from Arduino?

In Arduino, i get 3 values like this

Serial.print("pitch : "); Serial.print(pitch* 57.2957795131);    Serial.print(" ");
Serial.print("roll : "); Serial.print(roll* 57.2957795131);  Serial.print(" ");
Serial.print("yaw : "); Serial.println(yaw* 57.2957795131); 

and i got these values in OF

// OF CODE

#include “ofApp.h”

//--------------------------------------------------------------
string ofApp::ofxGetSerialString(ofSerial &serial, char until) {
static string str;
stringstream ss;
char ch;
int ttl = 1000;
while ((ch = serial.readByte())>0 && ttl–>0 && ch != until) {
ss << ch;
}
str += ss.str();
if (ch == until) {
string tmp = str;
str = “”;
return ofxTrimString(tmp);
}
else {
return “”;
}
}

// trim right trailing spaces
string ofApp::ofxTrimStringRight(string str) {
size_t endpos = str.find_last_not_of(" \t\r\n");
return (string::npos != endpos) ? str.substr(0, endpos + 1) : str;
}

// trim left trailing spaces
string ofApp::ofxTrimStringLeft(string str) {
size_t startpos = str.find_first_not_of(" \t\r\n");
return (string::npos != startpos) ? str.substr(startpos) : str;
}

// trim trailing spaces
string ofApp::ofxTrimString(string str) {
return ofxTrimStringLeft(ofxTrimStringRight(str));;
}

void ofApp::setup() {

ofSetVerticalSync(true);

ofBackground(255);
ofSetLogLevel(OF_LOG_VERBOSE);

ofSetColor(0);
font.loadFont("DIN.otf", 32);

serial.listDevices();
/*
[notice ] ofSerial: [0] = tty.usbmodem1441
[notice ] ofSerial: [1] = cu.usbmodem1441
[notice ] ofSerial: [2] = tty.Bluetooth-Incoming-Port
[notice ] ofSerial: [3] = cu.Bluetooth-Incoming-Port
*/

int baud = 9600;
// Tells the computer which port to be listening to
serial.setup("COM3", baud); //open the first device

readTime = 0;
serialString = "";

}

//--------------------------------------------------------------
void ofApp::update() {

serialString = "";
serialString = ofxGetSerialString(serial, '\n'); //read until end of line
if (serialString.length() > 0) {
	readTime = ofGetElapsedTimef();
	//ofLogVerbose() << "serialString = " << serialString << "\n";
	cout << "serialString = " << serialString << "\n";
}

}

I want to use each values.
For example i draw a square and rotate it with each values.
The square rotate according to sensor which i move in real world.

How can i use pitch, roll, yaw values in OF??

hello,

I don’t know the project or how many information you are transmitting, but i personally wouldn’t tag every value with keys.
I would rather send every information coma separated or one after the other like

roll1
pitch1
yaw1
roll2
pitch2
yaw2

then separate them in a for loop

update OF variables to use them later.
You will need to convert strings to int or float with

int i = ofToInt(roll1);

or

float f = ofToFloat(roll1);

Thank you to reply

but i don’t know how can i use int i = ofToInt(roll1);

i do it like this…

void ofApp::update() {

serialString = "";
serialString = ofxGetSerialString(serial, '\n'); //read until end of line
if (serialString.length() > 0) {
	readTime = ofGetElapsedTimef();
	//ofLogVerbose() << "serialString = " << serialString << "\n";
	//cout << "serialString = " << serialString << "\n";

	string yaw = serialString;
	float a = ofToInt(yaw);
	

          int b = ofToInt(serialString);


	cout << a << endl;
	cout << b << endl;

}

}

but the result is 0

how can i do it??

this is my arduino code:

void setup()
{
// Init serial output
Serial.begin(OUTPUT_BAUD_RATE);

// Init sensors
delay(50); // Give sensors enough time to start
I2C_Init();
Accel_Init();
Magn_Init();
Gyro_Init();
Pressure_Init();

// Read sensors, init DCM algorithm
delay(20); // Give sensors enough time to collect data
reset_sensor_fusion();
}

// Main loop
void loop()
{
// Time to read the sensors again?
if ((millis() - timestamp) >= OUTPUT_DATA_INTERVAL) {
timestamp_old = timestamp;
timestamp = millis();
if (timestamp > timestamp_old)
G_Dt = (float) (timestamp - timestamp_old) / 1000.0f; // Real time of loop run. We use this on the DCM algorithm (gyro integration time)
else
G_Dt = 0;

ReadSensors();

// Run DCM algorithm
Compass_Heading(); // Calculate magnetic heading

Matrix_update();
Normalize();
Drift_correction();
Euler_angles();


Serial.print("pitch : "); Serial.print(pitch* 57.2957795131);    Serial.print(" ");
Serial.print("roll : "); Serial.print(roll* 57.2957795131);  Serial.print(" ");
Serial.print("yaw : "); Serial.println(yaw* 57.2957795131); 

}
}

try to send only one value like one single float value and get it in OF using ofToFloat()
For a simple application, you can use it as a circle radius

float radius = ofToFloat(receivedValue);

then display it in console using :

cout << "received : " << radius << endl;

and draw a circle using this variable radius and see if you can make the circle size varying :

ofDrawCircle(ofGetWidth() / 2, ofGetHeight() / 2, radius);

once you get it right, you can build more complex communications.

Your reply is very helpful!

I got int value like this

Arduino CODE :

GY80 sensor = GY80();

String xAxis = "pitch: ";
String yAxis = "roll: ";
String zAxis = "yaw: ";

float pitch;
float roll;
float yaw;

void setup(){
Serial.begin(9600);
sensor.begin();

}

void loop() {

GY80_scaled val = sensor.read_scaled();

pitch = atan2(val.a_y, val.a_z)*180/PI;

roll = -atan2(val.a_x, val.a_z)*180/PI;

yaw = atan2(val.m_y, val.m_x)*180/PI+180;

/*
Serial.print(xAxis); Serial.print(pitch); Serial.print(" “);
Serial.print(yAxis); Serial.print(roll); Serial.print(” ");
Serial.print(zAxis); Serial.println(yaw, 0);
*/
Serial.println(yaw, 0);

delay(50);
}

OF CODE :

void ofApp::update() {

serialString = "";
serialString = ofxGetSerialString(serial, '\n'); //read until end of line
if (serialString.length() > 0) {
	readTime = ofGetElapsedTimef();
	//ofLogVerbose() << "serialString = " << serialString << "\n";
	//cout << "serialString = " << serialString << "\n";
			
	int a = ofToInt(serialString);

	cout << a << endl;

}

So i got a value(yaw).
But i want to use several values at the same time, not to sent only one value.
How do i send several values and use it??

now try to send 3 coma separated values like

yaw,pitch,roll

then in OF separate those values

vector<string> values;
values = ofSplitString(serialString, ',');
int yaw = ofToInt(values.at(0));
int pitch = ofToInt(values.at(1));
int roll = ofToInt(values.at(2));

this is a quick example but there is probably a more accurate way to grab the values from serial like waiting for bytes received but i have never used serial communication in OF so far.

1 Like

wow!! excellent!!!

Thank you very much because of you i solve this problem

Arduino code :

Serial.print(pitch* 57.2957795131);Serial.print(’,’);Serial.print(roll* 57.2957795131);Serial.print(’,’);Serial.println(yaw* 57.2957795131);

OF code :

void ofApp::update() {

serialString = "";
serialString = ofxGetSerialString(serial, '\n'); //read until end of line
while(serial.available() > 0) {
	readTime = ofGetElapsedTimef();
	
	vector<string> values;
	values = ofSplitString(serialString, ",");
	int pitch = ofToInt(values.at(0));
	int roll = ofToInt(values.at(1));
	int yaw = ofToInt(values.at(2));
	
	cout << "pitch = " << pitch << endl;
	cout << "roll = " << roll << endl;
	cout << "yaw = " << yaw << endl;

}

}

So i can get each values!!!
Thank you and have a nice day!^^

You are welcome. Glad it helped.
happy coding !

Hi, I tried this but got this error:
libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: vector

Do you have any idea why my vector is out of range ?
Thanks !