Advertisement
Guest User

Untitled

a guest
Jan 19th, 2016
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.12 KB | None | 0 0
  1. /*
  2.   Saving Values from Arduino to a .csv File Using Processing - Pseduocode
  3.  
  4.   This sketch provides a basic framework to read data from Arduino over the serial port and save it to .csv file on your computer.
  5.   The .csv file will be saved in the same folder as your Processing sketch.
  6.   This sketch takes advantage of Processing 2.0's built-in Table class.
  7.   This sketch assumes that values read by Arduino are separated by commas, and each Arduino reading is separated by a newline character.
  8.   Each reading will have it's own row and timestamp in the resulting csv file. This sketch will write a new file a set number of times. Each file will contain all records from the beginning of the sketch's run.  
  9.   This sketch pseduo-code only. Comments will direct you to places where you should customize the code.
  10.   This is a beginning level sketch.
  11.  
  12.   The hardware:
  13.   * Sensors connected to Arduino input pins
  14.   * Arduino connected to computer via USB cord
  15.        
  16.   The software:
  17.   *Arduino programmer
  18.   *Processing (download the Processing software here: https://www.processing.org/download/
  19.   *Download the Software Serial library from here: http://arduino.cc/en/Reference/softwareSerial
  20.  
  21.   Created 12 November 2014
  22.   By Elaine Laguerta
  23.   http://url/of/online/tutorial.cc
  24.  
  25. */
  26.  
  27. import processing.serial.*;
  28. Serial myPort; //creates a software serial port on which you will listen to Arduino
  29. Table table; //table where we will read in and store values. You can name it something more creative!
  30.  
  31. int numReadings = 5; //keeps track of how many readings you'd like to take before writing the file.
  32. int readingCounter = 0; //counts each reading to compare to numReadings.
  33.  
  34. String fileName;
  35. void setup()
  36. {
  37.   String portName = Serial.list()[1];
  38.   //CAUTION: your Arduino port number is probably different! Mine happened to be 1. Use a "handshake" sketch to figure out and test which port number your Arduino is talking on. A "handshake" establishes that Arduino and Processing are listening/talking on the same port.
  39.   //Here's a link to a basic handshake tutorial: https://processing.org/tutorials/overview/
  40.  
  41.   myPort = new Serial(this, portName, 9600); //set up your port to listen to the serial port
  42.   table = new Table();
  43.   table.addColumn("id"); //This column stores a unique identifier for each record. We will just count up from 0 - so your first reading will be ID 0, your second will be ID 1, etc.
  44.  
  45.   //the following adds columns for time. You can also add milliseconds. See the Time/Date functions for Processing: https://www.processing.org/reference/
  46.   table.addColumn("year");
  47.   table.addColumn("month");
  48.   table.addColumn("day");
  49.   table.addColumn("hour");
  50.   table.addColumn("minute");
  51.   table.addColumn("second");
  52.  
  53.   //the following are dummy columns for each data value. Add as many columns as you have data values. Customize the names as needed. Make sure they are in the same order as the order that Arduino is sending them!
  54.   table.addColumn("sensor1");  
  55.  
  56. }
  57.  
  58. void serialEvent(Serial myPort){
  59.   String val = myPort.readStringUntil('\n'); //The newline separator separates each Arduino loop. We will parse the data by each newline separator.
  60.   if (val!= null) { //We have a reading! Record it.
  61.     val = trim(val); //gets rid of any whitespace or Unicode nonbreakable space
  62.     println(val); //Optional, useful for debugging. If you see this, you know data is being sent. Delete if  you like.
  63.     int sensorVals = int(val); //parses the packet from Arduino and places the valeus into the sensorVals array. I am assuming floats. Change the data type to match the datatype coming from Arduino.
  64.    
  65.     TableRow newRow = table.addRow(); //add a row for this new reading
  66.     newRow.setInt("id", table.lastRowIndex());//record a unique identifier (the row's index)
  67.    
  68.     //record time stamp
  69.     newRow.setInt("year", year());
  70.     newRow.setInt("month", month());
  71.     newRow.setInt("day", day());
  72.     newRow.setInt("hour", hour());
  73.     newRow.setInt("minute", minute());
  74.     newRow.setInt("second", second());
  75.    
  76.     //record sensor information. Customize the names so they match your sensor column names.
  77.     newRow.setInt("sensor1", sensorVals);
  78.    
  79.    
  80.     readingCounter++; //optional, use if you'd like to write your file every numReadings reading cycles
  81.    
  82.     //saves the table as a csv in the same folder as the sketch every numReadings.
  83.     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)
  84.     {
  85.       //saveTable(table, "data/new.csv");
  86.       fileName = "data/" + str(year()) + str(month()) + str(day()) + str(table.lastRowIndex()) + ".csv"; //this filename is of the form year+month+day+readingCounter
  87.       saveTable(table, fileName); //Woo! save it to your computer. It is ready for all your spreadsheet dreams.
  88.     }
  89.    }
  90. }
  91.  
  92. void draw()
  93. {
  94.    //visualize your sensor data in real time here! In the future we hope to add some cool and useful graphic displays that can be tuned to different ranges of values.
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement