Simple Arduino -> OF won't work (Rangesensor)

Hi,

I’m reading out a Sharp Rangesensor with an Arduino.
This works nicely, and in the Arduino Serial Monitor I get fine numbers.

OF gets weird numbers that are not even in the scale of my sensor (above 900 / sensor is ~550max)

What am I doing wrong? I tried to solve it for hours now, and cannot find the solution.
Is it possible that I am reading the data in oF not as an int somehow?
What does it mean, that I read “3 bytes” in oF? The data is max 550, so it’s maximal 3 digits. Shouldn’t it work now?

So, now I tested it that Arduino just sends statically “567” ( Serial.println(567); / Serial.println(“567”); )
oF reads “7” or sometimes also an 567 flickers on the screen. WTF???

Here’s the Arduino Code I use:

  
/*  
  AnalogReadSerial  
 Reads an analog input on pin 0, prints the result to the serial monitor   
   
 This example code is in the public domain.  
 */  
   
 int sensorCal = 0;  
 int sensorOld = 0;  
 int sensorCount = 0;  
  
void setup() {  
  Serial.begin(9600);  
}  
  
void loop() {  
  int sensorValue = analogRead(A0);  
  sensorCal = abs(sensorValue - sensorOld);  
//        Serial.println(sensorValue, DEC);  
    
  if (sensorCal <= 5 | sensorCount >= 50) {  
      Serial.println(sensorValue, DEC);  
      sensorCount = 0;  
      sensorOld = sensorValue;  
  } else {  
      // Serial.println("ZENSOR");  
      sensorCount++;  
  }  
}  

And here the testApp.cpp

  
#include "testApp.h"  
  
//--------------------------------------------------------------  
void testApp::setup(){	   
  
	ofSetVerticalSync(true);  
  
	bSendSerialMessage = true;  
	ofBackground(255,255,255);	  
	ofSetLogLevel(OF_LOG_NOTICE);  
  
	//-----------------------------------   
	font.loadFont("DIN.otf", 64);  
			  
	serial.listDevices();  
	vector <ofSerialDeviceInfo> deviceList = serial.getDeviceList();  
	  
	//----------------------------------- note:  
	// < this should be set  
	// to whatever com port  
	// your serial device is  
	// connected to.  
	// (ie, COM4 on a pc, /dev/tty.... on linux, /dev/tty... on a mac)  
	// arduino users check in arduino app....  
		  
	//serial.setup(0, 9600); //open the first device  
  
	//serial.setup("COM4");  						  // windows example  
	serial.setup("/dev/tty.usbmodem1d11",9600); // mac osx example  
	//serial.setup("/dev/ttyUSB0", 9600);			  //linux example  
  
	nTimesRead = 0;  
	nBytesRead = 0;  
	readTime = 0;  
	memset(bytesReadString, 0, 4);  
}  
  
//--------------------------------------------------------------  
void testApp::update(){  
	  
	if (bSendSerialMessage){  
		  
		// (1) write the letter "a" to serial:  
		serial.writeByte('!');  
		  
		// (2) read  
		// now we try to read 3 bytes  
		// since we might not get them all the time 3 - but sometimes 0, 6, or something else,  
		// we will try to read three bytes, as much as we can  
		// otherwise, we may have a "lag" if we don't read fast enough  
		// or just read three every time. now, we will be sure to   
		// read as much as we can in groups of three...  
		  
		nTimesRead = 0;  
		nBytesRead = 0;  
		int nRead  = 0;  // a temp variable to keep count per read  
		  
		unsigned char bytesReturned[3];  
		  
		memset(bytesReadString, 0, 4);  
		memset(bytesReturned, 0, 3);  
		  
		while( (nRead = serial.readBytes( bytesReturned, 3)) > 0){  
			nTimesRead++;	  
			nBytesRead = nRead;  
		};  
		  
		memcpy(bytesReadString, bytesReturned, 3);  
		  
		bSendSerialMessage = true;  
		readTime = ofGetElapsedTimef();	}  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
  
	char tempStr[1024];  
	sprintf(tempStr, "click to test serial:\nnBytes read %i\nnTimes read %i\nread: %i\n(at time %0.3f)", nBytesRead, nTimesRead, ofToInt(bytesReadString), readTime);  
	  
	if (nBytesRead > 0 && ((ofGetElapsedTimef() - readTime) < 0.5f)){  
		ofSetHexColor(0x000000);  
	} else {  
		ofSetHexColor(0xdddddd);  
	}  
	font.drawString(tempStr, 50,100);  
  
}  
  
//--------------------------------------------------------------  
void testApp::keyPressed  (int key){   
	  
}  
  
//--------------------------------------------------------------  
void testApp::keyReleased(int key){   
	  
}  
  
//--------------------------------------------------------------  
void testApp::mouseMoved(int x, int y ){  
	  
}  
  
//--------------------------------------------------------------  
void testApp::mouseDragged(int x, int y, int button){  
	  
}  
  
//--------------------------------------------------------------  
void testApp::mousePressed(int x, int y, int button){  
//	bSendSerialMessage = true;  
}  
  
//--------------------------------------------------------------  
void testApp::mouseReleased(int x, int y, int button){  
  
}  
  
//--------------------------------------------------------------  
void testApp::windowResized(int w, int h){  
  
}  
  
//--------------------------------------------------------------  
void testApp::gotMessage(ofMessage msg){  
  
}  
  
//--------------------------------------------------------------  
void testApp::dragEvent(ofDragInfo dragInfo){   
  
}  
  

Thanks!!

Okay, I found out that oF was reading

  
57  
57  
57  

as

  
575  
757  

what explains the weird numbers

accordingly I send

  
057  
057  
057  

now.

Weird is, that oF Reads now this:

  
05  
0  
  
57  
0  
  
057  
  
05  
5  

I explain this to myself, that oF is NOT ALWAYS reading 3 digits. but only sometimes, what completely destroys the value.

I just want to read a range sensor!!! This shouldn’t be that hard, right? Frustration!!!

Allright, I did it now like this

  
	if (ofToString(bytesReadString[0]).find("\n") != string::npos){  
	} else if (ofToString(bytesReadString[1]).find("\n") != string::npos){  
	} else if (ofToString(bytesReadString[2]).find("\n") != string::npos){  
	} else {  
		do it  
	}  

Now, by sending the signal with an \n at the end, I can make sure that the value is not being shifted around.
But it’s not very nice because this way I randomly throw away nice data.

Someone told me to look at firmata. I will definitely do this and hope this solves my problem as well.
But still, I don’t get it.

The terminating character for the println() statement is actually a \r or ASCII 13 AND a \n (ASCII 10), so when you send println(“575”) you’re actually sending “575\r\n”. print() doesn’t do this, nor does write(). Another thing I do a lot is to append a control character that wouldn’t normally be in my data like a @ or $ to demarcate the end of a message. That way you can just use ofSerial::readByte() until you get to that character or a -1 (which means there’s nothing left to read).