Programming

To summarize the general framework for our approach, we:

  1. Created a Fitbit API account to access our test user’s Fitbit data.
  2. Ported the data over to a Google spreadsheet – we followed this very helpful tutorial to help get over this hurdle.
  3. Created a Processing sketch that called the Google spreadsheet API to extract the subsets of data we needed. Jer Thorpe’s tutorials on working with the Google Spreadsheets API and beginning with Processing and data viz were instrumental in this step.
  4. Built out conditionals to break down the dataset and encoded each possible action with a character to tell Arduino what to do. 
  5. Set up serial communication with Arduino, which could then switch on or off the lightbox for an amount of time based on the characters it read from Processing. Here we also added functions that control a small strip of 4 LEDs at the bottom of our lightbox. The LEDs provide a visual indicator of how long the session will last and progress in time. 

Admittedly, we’re still programming novices so some of the code here can definitely be cleaned up, but it works as a proof-of-concept! 

 

Processing Code

import processing.serial.*;
Serial port;

int m = month();
int d = day()-1;
int y = year ();
int h = hour();
int activityScore;
int sleepScore;

SimpleSpreadsheetManager sm; 

void setup() {
size(100,100);
background(255);
//set up serial port
//println(Serial.list()); 
//port = new Serial(this, Serial.list()[8], 9600);
port = new Serial(this, “/dev/cu.usbmodem1421”, 9600);

SimpleSpreadsheetManager sm = new SimpleSpreadsheetManager();
sm.init(“fitbitData”,”testsarahandmelody@gmail.com”, “melodyandsarah”);
sm.fetchSheetByKey(“0AovW6Hn1k3yfdE03QlhUdzNvQnQ1R1c2cWlOUVAyYnc”, 0);

//variables
String yesterday = m + “/” + d + “/” + y;
int sleepScore;
int timeOfDay;

boolean found_day = false;

//find the cell with yesterday’s date
for (int i=1; i<sm.getNumberOfRows(); i++){
if(sm.getCellValue(0,i).equals(yesterday)){
found_day = true;

//get activity and sleep scores
activityScore = int(sm.getCellValue(3,i));
println(activityScore);
sleepScore = int(sm.getCellValue(12,i));
}
}

if (!found_day) {
println(“ERROR FINDING DAY!!!”);
}

//Convert scores to corresponding chars

}

void draw() {

//serial communication for time of day
if(h<= 18){
port.write(‘X’);
println(“X = Day”);
} else{port.write(‘Y’);
println(“X = Evening”);
}

//ACTIVITY CONDITIONALS

if(activityScore>=1000){
port.write(‘A’);
println(“A”);
}

if(activityScore<=999 && activityScore>=750){
port.write(‘B’);
println(“B”);
}

if(activityScore<=749 && activityScore>=500){
port.write(‘C’);
println(“C”);
}

if(activityScore<=449 && activityScore>=0){
port.write(‘D’);
println(“D”);
}

//SLEEP CONDITIONALS

//Good sleep range: 7.5 - 9 hours
if(sleepScore>=450 && sleepScore<=540){
port.write(‘E’);
println(“E”);
}

//Fair sleep range: 6.5 - 7.5 hours OR 9 - 10.5 hours
if(sleepScore>=390 && sleepScore<=449){
port.write(‘F’);
println(“F”);
}

if(sleepScore>=541 && sleepScore<=630){
port.write(‘F’);
println(“F”);
}

//Not great sleep range: 5 - 6.5 hours OR 10.5 - 11.5 hours
if(sleepScore>=300 && sleepScore<=389){
port.write(‘G’);
println(“G”);
}

if(sleepScore>=631 && sleepScore<=690){
port.write(‘G’);
println(“G”);
}

//Bad sleep range: under 5 hours OR over 11.5 hours
if(sleepScore<=299){
port.write(‘H’);
println(“H”);
}

if(sleepScore>=691){
port.write(‘H’);
println(“H”);
}

 

Arduino Code

int led1 = 8;
int led2 = 9;
int led3 = 10;
int led4 = 11;
int blueBulb = 12;
int orangeBulb = 3;

int incomingByte; 
int activity;
int sleep;
int time;

int delayS = 1000;
int delayA = 5000;
int delayB = 10000;
int delayC = 15000;
int delayD = 20000;
int forever = 3600000;

void setup() {
// initialize serial communication:
Serial.begin(9600);
delay(1000);

// initialize the bulbs as outputs:
pinMode(blueBulb, OUTPUT);
pinMode(orangeBulb, OUTPUT);
}

void loop() {

// see if there’s incoming serial data:
if (Serial.available() > 0) {

//add a buffer:
delay(100);

// read the oldest byte in the serial buffer:
incomingByte = Serial.read();

//if if’s a char that matches any in the ‘activity’ set, store it:
if (incomingByte == ‘A’ || incomingByte == ‘B’|| incomingByte == ‘C’ || incomingByte == ‘D’){
activity = incomingByte; 
} else if (incomingByte == ‘E’ || incomingByte == ‘F’|| incomingByte == ‘G’|| incomingByte == ‘H’){
sleep = incomingByte;
// x = before 6pm, y = after 6pm 
} else if (incomingByte == ‘X’|| incomingByte == ‘Y’){
time = incomingByte;

//add 32 statements here
if(activity == ‘A’ && sleep ==’H’ && time == ‘X’){
digitalWrite(blueBulb, HIGH);
delay(delayA);
digitalWrite(blueBulb, LOW);
delay(forever);
}
}
}}

 

< Back