How to send Arduino data to Processing and save it on PC

edited October 2016 in Arduino

Hello everybody!

I'm currently using Arduino to control an LED-strip (as semicircle) of 180 LEDs with an Arduino Uno board. My Arduino sketch has the following process operation: The LEDs are blinking one after another and start with a frequency of 50Hz and go down to 20 Hz before they switch forward. In addition to that, there is a button which can be pressed for instant switching, whereby the frequency and the respective LED-number are send over a serial port, so you can see them in the serial Monitor (Arduino).

Now I would like to save these data on my PC with help of Processing. I know that there are many tutorials and articles to find but I only found Processing-codes to save sensorsinput. I guess there is a simple the solution, but I'm a total greenhorn in programming (especially Processing) and would like an opinion about my current "work" (I don't even know if my code is even sligthly correct :D)

And of course my crappy (sorry for vile language^^) Processing sketch: Note that the framework is from codes I found in other posts!

import processing.data.Table;
import processing.serial.*;


Serial myPort; //creates a software serial port
Table dataTable; //table to store values.

int numReadings = 500; //keeps track of how many readings one would like to take before writing the file. 
int readingCounter = 0; //counts each reading to compare to numReadings. 
float Winkel;
String Wink;



String fileName;
void setup()
{
  String portName = Serial.list()[0]; 

  myPort = new Serial(this, portName, 9600); //set up your port to listen to the serial port

  dataTable = new Table();
  dataTable.addColumn("id"); //This column stores a unique identifier for each record. We will just count up from 0 - so the first reading will be ID 0, the second will be ID 1, etc. 

  //the following adds columns for time.
  dataTable.addColumn("year");
  dataTable.addColumn("month");
  dataTable.addColumn("day");
  dataTable.addColumn("hour");
  dataTable.addColumn("minute");
  dataTable.addColumn("second");

  //columns for values
  dataTable.addColumn("Winkel");
  dataTable.addColumn("Frequenz");
}

  void draw() { 
    TableRow newRow = dataTable.addRow(); //add a row for this new reading
    newRow.setInt("id", dataTable.lastRowIndex());//record a unique identifier (the row's index)

    Wink = myPort.readString();
    println(Winkel);

    //record time stamp
    newRow.setInt("year", year());
    newRow.setInt("month", month());
    newRow.setInt("day", day());
    newRow.setInt("hour", hour());
    newRow.setInt("minute", minute());
    newRow.setInt("second", second());

    //record information. 
    newRow.setFloat("Winkel", Winkel);


    readingCounter++; //optional, use if you'd like to write your file every numReadings reading cycles

    //saves the table as a csv in the same folder as the sketch every numReadings. 
    if (readingCounter % numReadings ==0)//The % is a modulus, a math operator that signifies remainder after division. The if statement checks if readingCounter is a multiple of numReadings (the remainder of readingCounter/numReadings is 0)
    {
      fileName = str(year()) + str(month()) + str(day()) + str(dataTable.lastRowIndex()); //filename: year+month+day+readingCounter
      saveTable(dataTable, fileName); // save it to computer.
    }
}

The idea here is just to get the frequency (in German: Frequenz) and angle (in German: Winkel) data in a file. It doesn't have to look fancy. I do hope that I provided you with every necessary information! I can also add a screenshot of the serial monitor from Arduino by request.

Many thanks in advance for your help!

Answers

  • Break your problem down into smaller steps.

    Step 1: Can you write a simple Processing sketch that simply outputs a single value to a file? You can hardcode everything, just get it working.

    Step 2: Can you modify that simple sketch so that it outputs a variable? Something simply like mouseX would be fine.

    Step 3: Can you create a simple Arduino program that sends a single value to Processing? Again, the value can be hardcoded. When you receive it in Processing, just print it to the console for now.

    Step 4: Can you modify that simple program to store the value from Arduino into a variable?

    Step 5: Can you combine the ideas from your two separate programs to store the variable (which you're getting from Arduino) into a file?

    Step 6: Modify your program to send whatever you want from the Arduino, then store that in a file in Processing.

    Take these steps one at a time, and post an MCVE if you get stuck on a specific step. You'll have much better luck programming this way rather than trying to get random code you found on the internet to work. Good luck.

Sign In or Register to comment.