Array, Arraylist and Objects adding, removing, and processing
in
Programming Questions
•
2 years ago
Once again i find my self hitting a brick wall....the problem I am having is that i have serial data coming in and i would like to be able add the data to an array (or arraylist) of variable length and then do stuff with that array. I am not entirely sure what the "proper" way of doing this is. I have seen some people use append and extend but for some reason they didnt quite work out for me and used up way to much memory too quickly. I have also played around with using an object and doing a .add for the serial data to come in but im not quite talented enough yet to figure it out and i think it might be more trouble than its worth. So at the end of the day how does one write to an array and array list and then expand that same array?
I have posted code below for reference
thanks so much for the help in the past and future
import controlP5.*;
ControlP5 controlP5;
DropdownList p1;
import processing.serial.*;
Serial myPort; // The serial port
PFont font; // Declare PFont variable
PrintWriter output; //printer
int count = 0;
boolean stop = false;
int intPres = 1013; // sea level hPa
int arraysize = 0;
ArrayList rxdata = new ArrayList();
public void setup () {
String out = "data/" + month() + "_" + day() + "_" + hour() + "_" + minute() + "_data.csv";
output = createWriter(out);
font = loadFont( "ArialMT-20.vlw" ); // Must also use [Tools -> Font Selector]
// set the window size:
size(475, 300);
controlP5 = new ControlP5(this);
controlP5.addButton("Start_Stop_Data",128,25,80,80,19);
controlP5.addButton("Save",128,25,100,80,19);
controlP5.addButton("Clear",255,25,120,80,19);
controlP5 = new ControlP5(this);
//dropdown list
p1 = controlP5.addDropdownList("myList-p1",350,25,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);
//background(#FF8C00);
}
void draw () {
textFont(font);
text("Explorer Rocket Telemetry System", 25, 20);
fill(0, 102, 153);
textFont(font);
text("Telemetry Data", 25, 175);
text("X Axis:", 25, 200);
text("Y Axis:", 25, 225);
text("Z Axis:", 25, 250);
text("Pressure:", 25, 275);
fill(0, 102, 153);
while (myPort.available() > 0 && stop == false) { //Start principal loop
String inBuffer = myPort.readStringUntil(',');
//data aquisition
if (inBuffer != null) {
rxdata.add(inBuffer);
arraysize = rxdata.size();
println(rxdata); // a
delay(200);
count = count + 1;
}
break;
} // end principal loop
//background(0);
}
void redraw() {
background(0);
// background(#FF8C00);
}
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();
}
}
void Start_Stop_Data() {
if(stop == false)
{
stop = true;
println("Stop");
}
else
{
stop = false;
println("Start");
}
}
public void Save()
{
println("Save");
if( arraysize <= 0)
{
println("no data to save");
}
else {
for(int counter = 0; counter < arraysize; counter = counter+1)
{
println(rxdata);
}
}
}
public void Clear() {
println("Clear");
background(0);
for(int removecounter = 1; removecounter < arraysize; removecounter = removecounter+1){
rxdata.remove(rxdata.get);
println(rxdata);
}
}
I have posted code below for reference
thanks so much for the help in the past and future
import controlP5.*;
ControlP5 controlP5;
DropdownList p1;
import processing.serial.*;
Serial myPort; // The serial port
PFont font; // Declare PFont variable
PrintWriter output; //printer
int count = 0;
boolean stop = false;
int intPres = 1013; // sea level hPa
int arraysize = 0;
ArrayList rxdata = new ArrayList();
public void setup () {
String out = "data/" + month() + "_" + day() + "_" + hour() + "_" + minute() + "_data.csv";
output = createWriter(out);
font = loadFont( "ArialMT-20.vlw" ); // Must also use [Tools -> Font Selector]
// set the window size:
size(475, 300);
controlP5 = new ControlP5(this);
controlP5.addButton("Start_Stop_Data",128,25,80,80,19);
controlP5.addButton("Save",128,25,100,80,19);
controlP5.addButton("Clear",255,25,120,80,19);
controlP5 = new ControlP5(this);
//dropdown list
p1 = controlP5.addDropdownList("myList-p1",350,25,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);
//background(#FF8C00);
}
void draw () {
textFont(font);
text("Explorer Rocket Telemetry System", 25, 20);
fill(0, 102, 153);
textFont(font);
text("Telemetry Data", 25, 175);
text("X Axis:", 25, 200);
text("Y Axis:", 25, 225);
text("Z Axis:", 25, 250);
text("Pressure:", 25, 275);
fill(0, 102, 153);
while (myPort.available() > 0 && stop == false) { //Start principal loop
String inBuffer = myPort.readStringUntil(',');
//data aquisition
if (inBuffer != null) {
rxdata.add(inBuffer);
arraysize = rxdata.size();
println(rxdata); // a
delay(200);
count = count + 1;
}
break;
} // end principal loop
//background(0);
}
void redraw() {
background(0);
// background(#FF8C00);
}
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();
}
}
void Start_Stop_Data() {
if(stop == false)
{
stop = true;
println("Stop");
}
else
{
stop = false;
println("Start");
}
}
public void Save()
{
println("Save");
if( arraysize <= 0)
{
println("no data to save");
}
else {
for(int counter = 0; counter < arraysize; counter = counter+1)
{
println(rxdata);
}
}
}
public void Clear() {
println("Clear");
background(0);
for(int removecounter = 1; removecounter < arraysize; removecounter = removecounter+1){
rxdata.remove(rxdata.get);
println(rxdata);
}
}
1