Introduction & a few general Arduino/ofSerial questions

Hi all,

Since this is my first post on the oF forums, I should probably say ‘hi’ & ‘thank you’ ahead of time for the already great amount of information I’ve found here (via search and perusing). I’ve been migrating from primarily using Processing & AS3 to C++/oF over the last few weeks and while it’s been a tough road, these forums have been a life saver.

Anyway - I’m at the point where I’ve gotten a handle (or so I think) on the language and have started to experiment with some hardware & actuators.

I’m working with the Arduino Diecimila. I have successfully communicated to the Arduino via ofSerial and am now attempting to receive data from the Arduino to act upon within oF.

After encountering strange inconsistancies with the serial communication, I came across a great thread (">A bug in ofSerial::readByte()] where user damian had fixed what I believe was a bug in a Snow Leopard.

I am now receiving bytes via serial from the Arduino… /Begin my noob question:

Using the following segment, I understand that we are waiting for and reporting on some amount of data:

int bytes_required = 8;  
	unsigned char bytes[bytes_required];  
	int bytes_remaining = bytes_required;  
	while ( bytes_remaining > 0 )  
		// check for data  
		if ( serial.available() > 0 )  
			// try to read - note offset into the bytes[] array, this is so that we   
			// don't overwrite the bytes we already have  
			int result = serial.readBytes( &bytes[bytes_required-bytes_remaining], bytes_remaining );  
			// check for error code  
			if ( result == OF_SERIAL_ERROR )  
				ofLog( OF_LOG_ERROR, "unrecoverable error reading serial" );  
			else if ( result == OF_SERIAL_NO_DATA )  
				// nothing read  
				// we read some data!  
				// maybe not all..  
				bytes_remaining -= result;  

The ‘result’ I obtain is what I assume to be the size of the packet received via serial, in this case the numerical value of 1. How can I ascertain the actual value of what the arduino is sending? Say for example, I want to distinguish between two analog actions and I assign the arduino to report “x” for one and “y” for the other.

I’m running a dead simple loop on the Arduino while I’m testing:

int buttonPin = 13;  
void setup(){  
void loop(){  

Finally, I’ve been tooling around with Firmata and have found it a bit more elegant for handling general communication between oF & the Arduino. I assume that Firmata would only be used in an instance where oF was available and in constant communication with the Arduino (i.e., not packaging the Arduino with the external power source and making an computer-less installation). Could anyone help explain to me why I might use one versus the other?

Sorry about being so verbose & a huge thanks for anyone who can help.



the data that was read is in the bytes[] array.

how many bytes is your arduino code sending? if it’s only one byte then you don’t need all that loop, you can simply wait for serial.available() to return >= 1 and then call serial.readByte(). if instead you’re waiting for more than 1 byte, but not 8 bytes, then you should change the bytes_required value to however many you need.

Thanks damian, that did it!