The story of an Hour

Sooooooooooooooo….

Since I was using a microphone as my input and was sampling every 100 microseconds, then updating to a googledocs spreadsheet every two seconds… one day’s worth of data maxed out 200,000 cells in my sheet. Which led me to taking data for one hour only.

I hooked up my mic as the input to the arduino and took an average every 5 miliseconds, wrote the arduino input to serial in single bytes, then wrote the information from processing to google spreadsheets.

It worked…kind of? I am not sure if the arduino and the processing talked together accurately, and I should rework that. Another thing I should try to do is use the Xbee directly (without arduino), as a few people have suggested.

Anyway, here is the sound scape visualized over an hour.

And the code (there are three main pieces)

Arduino =====================================

#define NUMREADINGS 5
int readings[NUMREADINGS];
int index = 0;
int total = 0;
int average = 0;
int sensorPin = 2;
int val = 0;

void setup(){
Serial.begin(9600);
for (int i = 0; i < NUMREADINGS; i++){
readings[i] = 0;
}

}

void loop(){
total -= readings[index];
readings[index] = analogRead(sensorPin);
total += readings[index];
index ++;

if (index >= NUMREADINGS){
index = 0;
}

average = total/NUMREADINGS;

val = average/4;
Serial.print(val,BYTE);
delay(100);

}

===================================================

INTO Processing, OUT-TO Google docs ========================

import processing.serial.*;
import com.google.gdata.client.spreadsheet.*;
import com.google.gdata.data.*;
import com.google.gdata.data.spreadsheet.*;
import com.google.gdata.util.*;

// Variables structures for google spreadsheet API
SpreadsheetService service;  //Holds link to all your spreadsheets
WorksheetEntry worksheet;  //Holds link to the sensor log spreadsheet
String uname = “00000000000@gmail.com”;  //Your google account user name
String pwd = “f0000000000000000000”;  //Your google account password
String spreadsheet_name = “sensor log3”;  //Name of the spreadsheet you want to write data to.  Must match exactly, including case.
int spreadsheet_idx = 0; //Index for the “sensor log spreadsheet

//Variables for writing sensor data
Serial port;  // Create object from Serial class
int oldTime;  //timer variable
int reportingInterval = 1000;  //Number of miliiseconds between when sensor data is recorded

// Sends the data to the spreadsheet
void transmitData(float val) {
String date = day() + “/” + month() + “/” + year();  //Build the current date
String time = hour() + “:” + minute() + “:” + second(); //Build the current time
try {
//Create a new row with the name value pairs
ListEntry newEntry = new ListEntry();
newEntry.getCustomElements().setValueLocal(“date”, date);
newEntry.getCustomElements().setValueLocal(“time”, time);
newEntry.getCustomElements().setValueLocal(“reading”, Float.toString(val));
//Write it out to the google doc
URL listFeedUrl = worksheet.getListFeedUrl();
ListEntry insertedRow = service.insert(listFeedUrl, newEntry);
} catch (Exception e) {
println(e.getStackTrace());
}
}

float val = 0;
void setup() {
size(350,600);
//Set up the serial port to read data
//This code comes from example 11-8 of Getting Started with Processing
String arduinoPort = Serial.list()[0];
port = new Serial(this, arduinoPort, 9600);
oldTime = millis();
//Set up the google spreadsheet
service = new SpreadsheetService(“test”);
try {
service.setUserCredentials(uname,  pwd);
// Search for the spreadsheet named we’re looking for
// Note that according to the documentation you should be able to include the key in the URL, but I
// was unable to get this to work.  It looked like there was a bug report in.
// As a work around, this code pulls a list of all the Spreadheets in your acocunt and searches for the
// one with the matching name.  When it finds it, it breaks out of the loop and the index is set
URL feedURL = new URL(“http://spreadsheets.google.com/feeds/spreadsheets/private/full/&#8221;);
SpreadsheetFeed feed = service.getFeed(feedURL, SpreadsheetFeed.class);
for (SpreadsheetEntry entry: feed.getEntries()) {
if (entry.getTitle().getPlainText().equals(spreadsheet_name) ) {
break;
}
spreadsheet_idx += 1;
}
//Fetch the correct spreadsheet
SpreadsheetEntry se = feed.getEntries().get(spreadsheet_idx); //Fetch the spreadsheet we want
worksheet = se.getWorksheets().get(0);  //Fetch the first worksheet from that spreadsheet
println(“Found worksheet ” + se.getTitle().getPlainText());

} catch (Exception e) {
println(e.toString());
}
}

//Reads the port everye few seconds and sends the data back to Google
void draw() {
delay(100);
if (port.available() > 0) { // If data is available,
val = port.read();
}

val = map( val, 0, 255, 0, 1000);

if ((millis() – oldTime) > reportingInterval) {
oldTime = millis();
transmitData(val);
}

println(val);
}

=======================================================================

OUT FROM Google docs IN TO Processing OUT TO visualization ============================

float readVal;
float lineWidth;

SimpleSpreadsheetManager sm;
void setup() {
size(800,600);
background(255);
lineWidth = 3;

float prevVal = 0;

SimpleSpreadsheetManager sm = new SimpleSpreadsheetManager();
sm.init(“sensor log3″,”000000000000000000@gmail.com”, “0000000000”);
sm.fetchSheetByKey(“0Ai-_2FVf77didEVZODdhUlBFNUJIS2lVLTU5d0FUR3c”, 0);

for (int c=0; c < sm.currentTotalCols ; c++) {
for (int r=0; r < sm.currentTotalRows ; r++) {
float tempVal = Float.parseFloat(sm.getCellValue(2,r));
//float tempVal=6;
readVal = tempVal;
readVal = map(readVal,300,350,0,1);
lineWidth = (abs(readVal));
strokeWeight(5*lineWidth);
line(prevVal,0,prevVal,height);
prevVal += readVal+5;
}
}
}

void draw() {
}

==============================================