Serial input; Graphing and saving
in
Integration and Hardware
•
2 years ago
So here is the deal i have been wrestling around with this for quite some time and I have finally decided to ask for help.
What I am trying to do is create a program that graphs the serial data coming in (one axis for starters, three if i or someone feels compelled) and also can save all the information being gathered from the Arduino. I haven't been able to figure out how to write all the data coming in to a text file right now. The path I am trying is to save it all to an array and then magically save that array.
Thanks
Please note I am VERY new to this
For reference the Arduino code:
#include <Ports.h>
#include <RF12.h>
typedef struct { // 14 bytes
uint16_t ms;
//int16_t accel;
int16_t accelx;
int16_t accely;
int16_t accelz;
int16_t pres;
} Payload;
Payload values;
boolean ACK;
void setup () {
Serial.begin(57600);
// Serial.println("\n[rocket_REC.pde]");
rf12_initialize(1, RF12_915MHZ, 212); // 915 Mhz, net group 212, node 1
}
void loop () {
if (rf12_recvDone() && rf12_crc == 0 && rf12_len == sizeof (Payload)) {
Payload* values = (Payload*) rf12_data;
// Serial.print(values->ms);
// Serial.print(',');
// Serial.print(values->accel);
// Serial.print('\n');
Serial.print(values->accelx);
Serial.print(',');
Serial.print(values->accely);
Serial.print(',');
Serial.print(values->accelz);
Serial.print(',');
Serial.print(values->pres);
Serial.println(',');
}
}
Processing Code (that needs work)
import controlP5.*;
ControlP5 controlP5;
DropdownList p1;
int comnum = 0;
import processing.serial.*;
Serial myPort; // The serial port
int xPos = 1; // horizontal position of the graph
int index = 0;
int[] xaxis;
int[] yaxis;
int[] zaxis;
int[] press;
public void setup () {
// set the window size:
size(400, 300);
controlP5 = new ControlP5(this);
controlP5.addButton("Start",100,25,100,80,19);
controlP5.addButton("Stop",255,25,120,80,19);
controlP5.addButton("Save",128,25,140,80,19);
controlP5.addButton("Clear",255,25,160,80,19);
controlP5 = new ControlP5(this);
//
p1 = controlP5.addDropdownList("myList-p1",25,50,100,120);
customize(p1);
//initialize a serial port with first available port which will be selectable later
myPort = new Serial(this, Serial.list()[0], 57600);
myPort.bufferUntil('\n');
background(0);
}
void draw () {
//background(0);
}
void redraw() {
background(0);
xPos = 0;
}
//
void customize(DropdownList ddl) {
ddl.setBackgroundColor(color(190));
ddl.setItemHeight(20);
ddl.setBarHeight(15);
ddl.captionLabel().set("Change port");
ddl.captionLabel().style().marginTop = 3;
ddl.captionLabel().style().marginLeft = 3;
ddl.valueLabel().style().marginTop = 3;
println(Serial.list());
for(int i=0;i<Serial.list().length;i++) {
ddl.addItem(Serial.list()[i],i);
}
ddl.setColorBackground(color(60));
ddl.setColorActive(color(255,128));
}
void controlEvent(ControlEvent theEvent) {
// PulldownMenu is if type ControlGroup.
// A controlEvent will be triggered from within the ControlGroup.
// therefore you need to check the originator of the Event with
// if (theEvent.isGroup())
// to avoid an error message from controlP5.
if (theEvent.isGroup()) {
println(int(theEvent.group().value()));
myPort.stop();
myPort = new Serial(this, Serial.list()[int(theEvent.group().value())], 57600);
myPort.bufferUntil('\n');
redraw();
}
}
public void Stop() {
println("Stop");
}
public void Start() {
println("Start");
xPos = 0;
background(0);
}
void Save()
{
println("Save");
}
public void Clear() {
println("Clear");
xPos = 0;
background(0);
}
void serialEvent(Serial myPort) {
// get the ASCII string:
//Get x,y,z Axes
String inStringx = myPort.readStringUntil(',');
String inStringy = myPort.readStringUntil(',');
String inStringz = myPort.readStringUntil(',');
String inStringpress = myPort.readStringUntil(',');
xaxis[index] = Integer.parseInt(inStringx);
yaxis[index] = Integer.parseInt(inStringy);
zaxis[index] = Integer.parseInt(inStringz);
press[index] = Integer.parseInt(inStringpress);
println(xaxis[index]);
index++;
inStringy = trim(inStringy);
// convert to an int and map to the screen height:
float inByte = float(inStringy);
inByte = map(inByte, 0, 1023, 0, height);
//
// // draw the line:
stroke(127,34,255);
line(xPos, height, xPos, height - inByte);
//
// // at the edge of the screen, go back to the beginning:
if (xPos >= width) {
xPos = 0;
background(0);
}
else {
// // increment the horizontal position:
xPos++;
}
}
What I am trying to do is create a program that graphs the serial data coming in (one axis for starters, three if i or someone feels compelled) and also can save all the information being gathered from the Arduino. I haven't been able to figure out how to write all the data coming in to a text file right now. The path I am trying is to save it all to an array and then magically save that array.
Thanks
Please note I am VERY new to this
For reference the Arduino code:
#include <Ports.h>
#include <RF12.h>
typedef struct { // 14 bytes
uint16_t ms;
//int16_t accel;
int16_t accelx;
int16_t accely;
int16_t accelz;
int16_t pres;
} Payload;
Payload values;
boolean ACK;
void setup () {
Serial.begin(57600);
// Serial.println("\n[rocket_REC.pde]");
rf12_initialize(1, RF12_915MHZ, 212); // 915 Mhz, net group 212, node 1
}
void loop () {
if (rf12_recvDone() && rf12_crc == 0 && rf12_len == sizeof (Payload)) {
Payload* values = (Payload*) rf12_data;
// Serial.print(values->ms);
// Serial.print(',');
// Serial.print(values->accel);
// Serial.print('\n');
Serial.print(values->accelx);
Serial.print(',');
Serial.print(values->accely);
Serial.print(',');
Serial.print(values->accelz);
Serial.print(',');
Serial.print(values->pres);
Serial.println(',');
}
}
Processing Code (that needs work)
import controlP5.*;
ControlP5 controlP5;
DropdownList p1;
int comnum = 0;
import processing.serial.*;
Serial myPort; // The serial port
int xPos = 1; // horizontal position of the graph
int index = 0;
int[] xaxis;
int[] yaxis;
int[] zaxis;
int[] press;
public void setup () {
// set the window size:
size(400, 300);
controlP5 = new ControlP5(this);
controlP5.addButton("Start",100,25,100,80,19);
controlP5.addButton("Stop",255,25,120,80,19);
controlP5.addButton("Save",128,25,140,80,19);
controlP5.addButton("Clear",255,25,160,80,19);
controlP5 = new ControlP5(this);
//
p1 = controlP5.addDropdownList("myList-p1",25,50,100,120);
customize(p1);
//initialize a serial port with first available port which will be selectable later
myPort = new Serial(this, Serial.list()[0], 57600);
myPort.bufferUntil('\n');
background(0);
}
void draw () {
//background(0);
}
void redraw() {
background(0);
xPos = 0;
}
//
void customize(DropdownList ddl) {
ddl.setBackgroundColor(color(190));
ddl.setItemHeight(20);
ddl.setBarHeight(15);
ddl.captionLabel().set("Change port");
ddl.captionLabel().style().marginTop = 3;
ddl.captionLabel().style().marginLeft = 3;
ddl.valueLabel().style().marginTop = 3;
println(Serial.list());
for(int i=0;i<Serial.list().length;i++) {
ddl.addItem(Serial.list()[i],i);
}
ddl.setColorBackground(color(60));
ddl.setColorActive(color(255,128));
}
void controlEvent(ControlEvent theEvent) {
// PulldownMenu is if type ControlGroup.
// A controlEvent will be triggered from within the ControlGroup.
// therefore you need to check the originator of the Event with
// if (theEvent.isGroup())
// to avoid an error message from controlP5.
if (theEvent.isGroup()) {
println(int(theEvent.group().value()));
myPort.stop();
myPort = new Serial(this, Serial.list()[int(theEvent.group().value())], 57600);
myPort.bufferUntil('\n');
redraw();
}
}
public void Stop() {
println("Stop");
}
public void Start() {
println("Start");
xPos = 0;
background(0);
}
void Save()
{
println("Save");
}
public void Clear() {
println("Clear");
xPos = 0;
background(0);
}
void serialEvent(Serial myPort) {
// get the ASCII string:
//Get x,y,z Axes
String inStringx = myPort.readStringUntil(',');
String inStringy = myPort.readStringUntil(',');
String inStringz = myPort.readStringUntil(',');
String inStringpress = myPort.readStringUntil(',');
xaxis[index] = Integer.parseInt(inStringx);
yaxis[index] = Integer.parseInt(inStringy);
zaxis[index] = Integer.parseInt(inStringz);
press[index] = Integer.parseInt(inStringpress);
println(xaxis[index]);
index++;
inStringy = trim(inStringy);
// convert to an int and map to the screen height:
float inByte = float(inStringy);
inByte = map(inByte, 0, 1023, 0, height);
//
// // draw the line:
stroke(127,34,255);
line(xPos, height, xPos, height - inByte);
//
// // at the edge of the screen, go back to the beginning:
if (xPos >= width) {
xPos = 0;
background(0);
}
else {
// // increment the horizontal position:
xPos++;
}
}
1