More serial issues

Alright, so I got my code up and “running”. but it’s not really doing what I need it to do. It’s based on the 2 byte serial read system from Theo’s code. I get the following message from Xcode:

ofSerial: Trouble reading from the port

And in the Arduino it spits out nothing but gibberish. The only thing I have changed in the Arduino code is the analog pin from 0 to 2.

Here is my code for OF:

  
#include "testApp.h"  
  
  
//--------------------------------------------------------------  
void testApp::setup(){	   
	ofBackground(255,255,255);	  
	  
	isSetup = false;  
	  
	serial.enumerateDevices();  
	  
  
	//----------------------------------- note:  
	serial.setup("/dev/tty.usbserial-A6004nen", 9600);	// < this should be set  
										// to whatever com port  
										// your serial device is   
										// connected to.    
										// (ie, COM4 on a pc, dev/tty.... on a mac)  
										// arduino users check in arduino app....  
	  
		  
}  
  
//--------------------------------------------------------------  
void testApp::update(){  
  
	// (1) write the letter "!" to serial -   
	//this will tell the arduino we ready to get data:  
	serial.writeByte('!');  
		  
	// (2) read  
	// we try to read 2 bytes  
  
	//clear our variables  
	int nTimesRead  = 0;  
	int nBytesRead  = 0;  
	int nRead		= 0;  // a temp variable to keep count per read  
	  
	//we are going to read a 0 - 1024 number as two bytes  
	//we need a buffer to store the two bytes and a second   
	//buffer with space for the terminating zero byte   
	unsigned char bytesReturned[2];  
	unsigned char bytesReadString[3];  
	  
  
	//clear our buffers  
	memset(bytesReadString, 0, 3);  
	memset(bytesReturned, 0, 2);  
			  
	//we read as much as possible so we make sure we get the newest data  
	while( (nRead = serial.readBytes( bytesReturned, 2)) > 0){  
		nTimesRead++;	  
		nBytesRead = nRead;  
	}  
  
	//if we have got both bytes  
	if( nBytesRead == 2){  
	  
		//lets update our buffer  
		memcpy(bytesReadString, bytesReturned, 2);  
		  
		//we need to put the bytes back into an int  
		int num = ( (unsigned char)bytesReadString[1] << 8 | (unsigned char)bytesReadString[0] );  
		  
		//the number as a string  
		message = "1st byte:"+ ofToString( (unsigned char)bytesReadString[0] ) + "  2nd byte:" + ofToString( (unsigned char)bytesReadString[1] ) +" = "+ofToString(num);  
		  
		valueIn = num;  
		  
		//****************************  
		//adds new value to array and then cycles up one  
		zeroValue[countUp] = valueIn;  
		  
		countUp = countUp + 1;  
		if (countUp > 49) {   
			countUp = 0;  
		}  
		  
		//****************************  
		//averaging for the zero value  
		for(int b = 0; b < 50; b++){   
		totalZero = totalZero + zeroValue[b];  
		}  
		  
		averageZero = totalZero / 50;  
		  
		//****************************  
		//Now we need to know lowest possible value  
		//set inital sample value for lowest value selection  
		int lowZero = zeroValue[0];  
		  
		// start with min = first element   
		//and then compare with all subsequent values always swapping in the new lowest  
		for(int i = 1; i < 50; i++){  
			if (zeroValue[i] > 200){  
				if(zeroValue[i] < lowZero){  
				lowZero = zeroValue[i];  
				}  
			}  
		}  
				  
		//****************************  
		//compare lowest value to averaged zero and average again for usable zero   
		actualZero = (averageZero + lowZero) / 2;  
		  
		//****************************  
		//always remain between the 0 value and 1023... lets say 850 and 1023...   
		//must then calculate how much to multiply to equal range of slider bar  
		//173 difference goes into 800 pixel range for horizontal...   
		//for each sensor value it can move 4.6 to right (theoretical of course)  
  
		//determines the range of values that I'm sensing between  
		foundRange = 1023 - actualZero;  
		  
		//for each valuel, determine how many pixels its worth in terms of movement  
		//800 is current total possible movement range (based on pixels) and can be reconfigured  
		unitMove = 800 / foundRange;  
		  
		//****************************		  
		//calculate this average based on (array position - 5)  
		//constantly averaging the last 5 readings to get a smoother number  
		//can be tweaked  
		  
		//handle 0 array value averaging  
		//0 is special case  
		if (countUp == 0){  
			for(int b = 46; b < 50; b++){   
			smoothValue = smoothValue + zeroValue[b];  
			}  
  
			smoothValue = smoothValue + zeroValue[0];  
			smoothValue = smoothValue / 5;  
		}  
		  
		//calculates for wrap around  
		else if (countUp < 5){  
			smallNumber = 5 - countUp;  
			wrapped = 50 - smallNumber;  
  
			for(int b = wrapped; b < 50; b++){   
				smoothValue = smoothValue + zeroValue[b];  
			}  
			for(int c = 0; c < countUp; c++){   
				smoothValue = smoothValue + zeroValue[c];  
			}  
			smoothValue = smoothValue / 5;  
		}  
		  
		  
		else if (countUp >= 5){  
			for(int b = (countUp - 5); b < countUp; b++){   
			smoothValue = smoothValue + zeroValue[b];  
			}  
			smoothValue = smoothValue / 5;  
		}  
		//amount of units to move to right then multiply this by 4.6 (in example)  
		  
		//only do this if the smoothed value is greater than the zero value  
		//if equal to or smaller, we want our slider to remain at zero  
  
	}  
}  
		  
		  
		  
//--------------------------------------------------------------  
void testApp::draw(){  
  
		if (smoothValue >= actualZero){  
		  
			//determines how many units we need to move the bar  
			moveValue = smoothValue - actualZero;  
		  
			//ignores small readings and assumes that they are noise  
			if (moveValue > 4){  
		  
				//move rectangle based on movement values  
				ofSetColor(255,0,0,255);   // red  
				ofRect((moveValue * unitMove),450,10,80);  
			}  
		}  
		  
	  
}	  

I’m not sure if it’s drawing things right to the frame, but I was going to mess around with that once I got it up and running, but for some reason I can’t get the serial to work. [/quote]

just a quick guess,

are you printing out stuff to the arduino console?
OF won’t be able to talk to the arduino if the serial monitor in the arduino software is active.

one other thing to try is ofSetVerticalSync(true); in your setup function - might be trying to read too quickly?

cheers,
theo

I changed things around for a while and the serial still didn’t work and then all of a sudden, for no reason that I can readily determine, it decided to start working. I think the serial is running screwy on my computer. Sometimes the FTDI driver crashes my whole system or doesn’t show up at all. Also, I’ve been doing a little research and apparently the serial bits are actually being shifted by the driver slightly and we’re actually reading them at the edge. I dunno… friggin Arduinos…

Now, I have a simpler, more “beginner” question. What is the best way to debug something and monitor the values passing through my code using Xcode?

The rectangle I’m trying to move is twitching thoughtfully back and forth a few pixels, which means that my code is clearly to some extent functioning, but breaking somewhere along the line. I think possibly different variable types might be throwing something off, but I’m not sure. Anyhow, that’s why I’d like to be able to monitor the variables. Is the best way to print it to the OF window?

[/code]

To print out a value to the console you can use printf:

The %f and %i act as placeholders for float and int variables which pass to the function as bellow. The \n means newline.

  
float floatVal = 20.2;  
int intVal = 100;  
  
printf("my floating point value is %f - my int value is %i \n", floatVal, intVal);  

To print to the screen you could use ofDrawBitmapString and ofToString:
This has to be called in the draw() function of testApp so bare that in mind :slight_smile:

  
ofSetColor(0x000000);  
ofDrawBitmapString("my floating point value is"+ ofToString(floatVal) + "my int value is "+ofToString(intVal), 10, 10 );  

Theo

Thanks! It’s good to see what numbers are actually coming through in the console. I just reverted to commenting everything out and uncommenting everything in one line at time. Actually knowing the numbers is much more useful than making up stories for myself about what I think they are (and my code is working “perfectly” as far as the numbers go! It’s just not doing what I thought it would . :? )