Putting Arduino data into tables using Processing

edited November 2014 in Arduino

I'm trying to teach myself how to save data acquired from Arduino using Processing. So far I've been able to save single column data in processing but I also want to add a column that prints the time alongside voltage.

This is my Arduino code:

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  unsigned long time;
  time = millis();
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1023.0);
  // print out the value you read:
  Serial.println(voltage);
  Serial.println(time/1000);
  delay(1000);

}

This is my Processing code:

import processing.serial.*;


PrintWriter output;
Serial myPort;        // The serial port
int xPos = 1;         // horizontal position of the graph 

//Variables to draw a continuous line.
int lastxPos=1;
int lastheight=0;

void setup () {
  // set the window size:
  size(1000, 500);        

  // List all the available serial ports
  println(Serial.list());
  // Check the listed serial ports in your machine
  // and use the correct index number in Serial.list()[].

  myPort = new Serial(this, Serial.list()[2], 9600);  //
  output=createWriter("Voltage.txt");
  // A serialEvent() is generated when a newline character is received :
  myPort.bufferUntil('\n');
  background(0);      // set inital background:
}
void draw () {
  // everything happens in the serialEvent()
}

void serialEvent (Serial myPort) {
  // get the ASCII string:
  String inString = myPort.readStringUntil('\n');

  if (inString != null) {
    inString = trim(inString);                // trim off whitespaces.
    float inByte = float(inString);    // convert to a number.
    println(inByte);
    output.println(inByte);
    inByte = map(inByte, 0, 5, 0, height); //map to the screen height.

    //Drawing a line from Last inByte to the new one.
    stroke(127,34,255);     //stroke color
    strokeWeight(4);        //stroke wider
    line(lastxPos, lastheight, xPos, height - inByte); 
    lastxPos= xPos;
    lastheight= int(height-inByte);
    // at the edge of the window, go back to the beginning:
    if (xPos >= width) {
      xPos = 0;
      lastxPos= 0;
      background(0);  //Clear the screen.
    } 
    else {
      // increment the horizontal position:
      xPos++;
    }
  }
}

void keyPressed() {
    output.flush();
    output.close();
    exit();
  }

So the code right now only saves the voltage data but I'm not sure how to add a column in the saved text file. I've seen both these URL's but I still can't make much sense from it. Please help!!!

https://processing.org/reference/Table.html

https://processing.org/tutorials/data/

Answers

  • You save read voltage into a PrintWriter via println().
    Why don't you add more things to it like: output.println(inByte + ", " + time);?

  • I don't know how you'd go about doing that. Are you talking about the Arduino side of things or the Processing? In the future I'm going to be adding more variables that I'd like to save in a file but I need to know how to create a readable table for something like Matlab. I've only been at it for a week but I need help

  • Either make a Table: https://processing.org/reference/Table.html
    or make your own custom class!

    Remember that a Table stores in memory. While a PrintWriter is for output operations!

  • Okay, I think I asked the wrong question but I (for the most part) figured out what I needed to do. I needed to save different variables into different columns that could be read as a text file so matlab could plot it. This Arduino code put the two measured variables into columns.

    int sensorPin = A0;
    int sensorValue = 0;
    
    void setup() {
      // initialize serial communication at 9600 bits per second:
      Serial.begin(9600);
    }
    
    // the loop routine runs over and over again forever:
    void loop() {
      unsigned long time;
      time = millis();
      // read the input on analog pin 0:
      int sensorValue = analogRead(A0);
      // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
      float voltage = sensorValue * (5.0 / 1023.0);
      // print out the value you read:
    
      Serial.print(time/1000);
      Serial.print(',');
      Serial.println(voltage);
    
      delay(1000);
    
    }
    

    With this all I had to do was take line 39 of the processing code and make it read output.println(inString);

    I still need to be able to isolate the "inString" data so that I can plot Voltage correctly, but I may have to move to another discussion. Thanks for the help GoToLoop

  • Answer ✓

    I still need to be able to isolate the "inString" data

    https://processing.org/reference/split_.html

Sign In or Register to comment.