hello all,
i using following code access accelerometer data arduino , need plot real time graph on processing.
arduino code:
#include "naxismotion.h" //contains bridge code between api , arduino environment
#include <wire.h>
naxismotion mysensor; //object sensor
unsigned long laststreamtime = 0; //to store last streamed time stamp
const int streamperiod = 40; //to stream @ 25hz without using additional timers (time period(ms) =1000/frequency(hz))
bool updatesensordata = true; //flag update sensor data. default true perform first read before first stream
void setup() //this code executed once
{
//peripheral initialization
serial.begin(115200); //initialize serial port view information on serial monitor
i2c.begin(); //initialize i2c communication let library communicate sensor.
//sensor initialization
mysensor.initsensor(); //the i2c address can changed here inside function in library
mysensor.setoperationmode(operation_mode_ndof); //can configured other operation modes desired
mysensor.setupdatemode(manual); //the default auto. changing manual requires calling relevant update functions prior calling read functions
//setting manual requires lesser reads sensor
mysensor.updateaccelconfig();
updatesensordata = true;
serial.println();
serial.println("default accelerometer configuration settings...");
serial.print("range: ");
serial.println(mysensor.readaccelrange());
serial.print("bandwidth: ");
serial.println(mysensor.readaccelbandwidth());
serial.print("power mode: ");
serial.println(mysensor.readaccelpowermode());
serial.println("streaming in ..."); //countdown
serial.print("3...");
delay(1000); //wait second
serial.print("2...");
delay(1000); //wait second
serial.println("1...");
delay(1000); //wait second
}
void loop() //this code looped forever
{
if (updatesensordata) //keep updating of data separate task
{
mysensor.updateaccel(); //update accelerometer data
mysensor.updatelinearaccel(); //update linear acceleration data
mysensor.updategravaccel(); //update gravity acceleration data
mysensor.updatecalibstatus(); //update calibration status
updatesensordata = false;
}
if ((millis() - laststreamtime) >= streamperiod)
{
laststreamtime = millis();
//serial.print("time: ");
//serial.print(laststreamtime);
//serial.print("ms ");
//serial.print(" ax: ");
serial.print(mysensor.readaccelx()); //accelerometer x-axis data
//serial.print("m/s2 ");
serial.println();
updatesensordata = true;
}
}
the processing code, try modify following:
import processing.serial.*;
serial myport; // serial port
int xpos = 1; // horizontal position of graph
void setup () {
// set window size:
size(400, 300);
// list available serial ports
println(serial.list());
// know first port in serial list on mac
// arduino, open serial.list()[0].
// open whatever port 1 you're using.
myport = new serial(this, serial.list()[0], 115200);
// don't generate serialevent() unless newline character:
myport.bufferuntil('\n');
// set inital background:
background(0);
}
void draw () {
// happens in serialevent()
}
void serialevent (serial myport) {
// ascii string:
string instring = myport.readstringuntil('\n');
if (instring != null) {
// trim off whitespace:
instring = trim(instring);
// convert int , map screen height:
float inbyte = float(instring);
inbyte = map(inbyte, 0, 1023, 0, height);
println(inbyte);
// draw line:
stroke(inbyte,0,255);
nofill();
line(xpos, height, xpos, height - inbyte);
// @ edge of screen, go beginning:
if (xpos >= width) {
xpos = 0;
background(0);
}
else {
// increment horizontal position:
xpos++;
}
}
}
what want try first step send 1 stream of value instance "ax" , plot respect time in real time. though see values in arduino, stream else in processing, , above graphic window absolutely blank.
can tell me, problem lies?
i using following code access accelerometer data arduino , need plot real time graph on processing.
arduino code:
#include "naxismotion.h" //contains bridge code between api , arduino environment
#include <wire.h>
naxismotion mysensor; //object sensor
unsigned long laststreamtime = 0; //to store last streamed time stamp
const int streamperiod = 40; //to stream @ 25hz without using additional timers (time period(ms) =1000/frequency(hz))
bool updatesensordata = true; //flag update sensor data. default true perform first read before first stream
void setup() //this code executed once
{
//peripheral initialization
serial.begin(115200); //initialize serial port view information on serial monitor
i2c.begin(); //initialize i2c communication let library communicate sensor.
//sensor initialization
mysensor.initsensor(); //the i2c address can changed here inside function in library
mysensor.setoperationmode(operation_mode_ndof); //can configured other operation modes desired
mysensor.setupdatemode(manual); //the default auto. changing manual requires calling relevant update functions prior calling read functions
//setting manual requires lesser reads sensor
mysensor.updateaccelconfig();
updatesensordata = true;
serial.println();
serial.println("default accelerometer configuration settings...");
serial.print("range: ");
serial.println(mysensor.readaccelrange());
serial.print("bandwidth: ");
serial.println(mysensor.readaccelbandwidth());
serial.print("power mode: ");
serial.println(mysensor.readaccelpowermode());
serial.println("streaming in ..."); //countdown
serial.print("3...");
delay(1000); //wait second
serial.print("2...");
delay(1000); //wait second
serial.println("1...");
delay(1000); //wait second
}
void loop() //this code looped forever
{
if (updatesensordata) //keep updating of data separate task
{
mysensor.updateaccel(); //update accelerometer data
mysensor.updatelinearaccel(); //update linear acceleration data
mysensor.updategravaccel(); //update gravity acceleration data
mysensor.updatecalibstatus(); //update calibration status
updatesensordata = false;
}
if ((millis() - laststreamtime) >= streamperiod)
{
laststreamtime = millis();
//serial.print("time: ");
//serial.print(laststreamtime);
//serial.print("ms ");
//serial.print(" ax: ");
serial.print(mysensor.readaccelx()); //accelerometer x-axis data
//serial.print("m/s2 ");
serial.println();
updatesensordata = true;
}
}
the processing code, try modify following:
import processing.serial.*;
serial myport; // serial port
int xpos = 1; // horizontal position of graph
void setup () {
// set window size:
size(400, 300);
// list available serial ports
println(serial.list());
// know first port in serial list on mac
// arduino, open serial.list()[0].
// open whatever port 1 you're using.
myport = new serial(this, serial.list()[0], 115200);
// don't generate serialevent() unless newline character:
myport.bufferuntil('\n');
// set inital background:
background(0);
}
void draw () {
// happens in serialevent()
}
void serialevent (serial myport) {
// ascii string:
string instring = myport.readstringuntil('\n');
if (instring != null) {
// trim off whitespace:
instring = trim(instring);
// convert int , map screen height:
float inbyte = float(instring);
inbyte = map(inbyte, 0, 1023, 0, height);
println(inbyte);
// draw line:
stroke(inbyte,0,255);
nofill();
line(xpos, height, xpos, height - inbyte);
// @ edge of screen, go beginning:
if (xpos >= width) {
xpos = 0;
background(0);
}
else {
// increment horizontal position:
xpos++;
}
}
}
what want try first step send 1 stream of value instance "ax" , plot respect time in real time. though see values in arduino, stream else in processing, , above graphic window absolutely blank.
can tell me, problem lies?
quote
even though see values in arduino, stream else in processinyou can't see values in arduino. might me able see values in serial monitor application connects serial port arduino sending data to.
but, if doing that, processing can't connect same serial port, not surprising can't data.
Arduino Forum > Using Arduino > Interfacing w/ Software on the Computer > Visualizing Accelerometer data in Processing
arduino
Comments
Post a Comment