3. Arduino sketch
BAUD_RATE : 9600
Device ID
Read analog values
Print to serial
“id,x,y,z” + ‘n’
int id = 0;
void setup()
{
Serial.begin( 9600 );
}
void loop()
{
int x = analogRead( A0 );
int y = analogRead( A1 );
int z = analogRead( A2 );
Serial.print( id );
Serial.print( "," );
Serial.print( x );
Serial.print( "," );
Serial.print( y );
Serial.print( "," );
Serial.print( z );
Serial.println();
delay( 100 );
}
8. Arduino sketch
( ArduinoMicro + XBee )
Serial → Serial1
Serial → Serial1
int id = 0;
void setup()
{
Serial1.begin( 9600 );
}
void loop()
{
int x = analogRead( A0 );
int y = analogRead( A1 );
int z = analogRead( A2 );
Serial1.print( id );
Serial1.print( "," );
Serial1.print( x );
Serial1.print( "," );
Serial1.print( y );
Serial1.print( "," );
Serial1.print( z );
Serial1.println();
delay( 100 );
}
9. Read Serial
( openFrameworks )
ofApp.h ofApp.cpp
class ofApp : public ofBaseApp
{
public:
ofSerial serial;
string str;
int index, x, y, z;
};
void ofApp::setup()
{
index = x = y = z = 0;
str = "";
serial.listDevices();
serial.setup( 0, 9600 );
ofBackground( 255 );
}
void ofApp::update()
{
while( true )
{
int c = serial.readByte();
if( c == OF_SERIAL_NO_DATA || c == OF_SERIAL_ERROR || c == 0 )
{
break;
}
if( c == ‘n’ )
{
vector< string > valStr = ofSplitString( str, "," );
if( valStr.size() == 4 )
{
index = ofToInt( valStr.at( 0 ) );
x = ofToInt( valStr.at( 1 ) );
y = ofToInt( valStr.at( 2 ) );
z = ofToInt( valStr.at( 3 ) );
}
str = "";
}
else
{
str.push_back( c );
}
}
}
10. Flow Chart
false
read 1 character
false
true
true
int c = serial.readByte()
c == OF_SERIAL_NO_DATA
or
c == OF_SERIAL_ERROR
or
c == 0
while( true )
break str.push_back( c )
c == ‘n’
ofSplitString( str, "," )
str = ""
index = ofToInt( valStr.at( 0 ) )
x = ofToInt( valStr.at( 1 ) )
y = ofToInt( valStr.at( 2 ) )
z = ofToInt( valStr.at( 3 ) )
11. Get values from string
“0,524,535,751”
convert to int from string
index = ofToInt( valStr.at( 0 ) )
x = ofToInt( valStr.at( 1 ) )
y = ofToInt( valStr.at( 2 ) )
z = ofToInt( valStr.at( 3 ) )
“0” “524” “535” “751”
id
string
string
x y z
string
int
vector< string > valStr = ofSplitString( str, "," )