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]