just goin for a spin (my hamster tweets)

Yung Quinoa the hamster has a tendency to run on and off her wheel incessantly, mostly between the hours of 1 am to 4 am, so I attached an arduino with a pressure sensor to her wheel which tweets every time she hops on and off. I sometimes try to imagine her life, caged, where the only escape and use of time is to run on a stationary object for hours, spin out of control, return, repeat (sounds a bit familiar though).
I still need to work out some difficulties and adjust the code so that it register when she gets on and then off the wheel, as it now just tweets whenever she is on it, and so on for every minute.

Screen Shot 2014-11-19 at 7.15.23

<a href="http://cmuems.com/2014a/wp-content/uploads/2014/11/Screen-Shot-2014-11-12-at-15.24.07.png"><img class="alignnone  wp-image-3526" src="http://cmuems.com/2014a/wp-content/uploads/2014/11/Screen-Shot-2014-11-12-at-15.24.07-640x428.png" alt="Screen Shot 2014-11-12 at 15.24.07" width="440" height="294" /></a>

Processing Code:

import com.temboo.core.*;
import com.temboo.Library.Twitter.Tweets.*;
import processing.serial.*;
 
boolean justTweeted;
boolean neverTweeted = true;
int lastTweeted;
int TWEET_DELAY = 1000;
int TWEET_THRESHOLD = 320;
int valueA;
int valueB;
Serial myPort;  // Create object from Serial class
 
// Create a session using your Temboo account application details
TembooSession session = new TembooSession("yungquinoa", "yungquins", "Sc1TnGcVSeca7S8nikXg7poaElZ98wJi");
 
void setup() {
  String portName = Serial.list()[2];
  myPort = new Serial(this, portName, 9600);
  serialChars = new ArrayList();
}
 
void draw() {
  processSerial();
  int tweetDiff = frameCount - lastTweeted;
  println(valueA);
  if((valueA > TWEET_THRESHOLD) && 
     (neverTweeted || (tweetDiff > TWEET_DELAY && !justTweeted))) {
    neverTweeted = false;
    lastTweeted = frameCount;
    justTweeted = true;
    println("Tweeting");
    runStatusesUpdateChoreo();
  }
  else justTweeted = false;
}
 
void runStatusesUpdateChoreo() {
  // Create the Choreo object using your Temboo session
  StatusesUpdate statusesUpdateChoreo = new StatusesUpdate(session); 
 
  // Set inputs
  statusesUpdateChoreo.setAccessToken("2309191166-RysMktA6aSSNLQXCbegvHtq5bfdmMAlxXfxGBGG");
  statusesUpdateChoreo.setAccessTokenSecret("NyZC1NoPtBPZNfSNN8wt4Xxen3FOc2c21cNysjnp63riC");
  statusesUpdateChoreo.setConsumerSecret("DGsZu6jSWaaGnLnlETZH2K8C6Q0fJ3gQp70D45wsAUuNPdvYDK");
  statusesUpdateChoreo.setConsumerKey("6i95ReRsb13Cmlxr7nPF098pk");
 
  String message = "JUST GOIN FOR A SPIN" + random(10);
  statusesUpdateChoreo.setStatusUpdate(message);
 
  // Execute Choreo
  StatusesUpdateResultSet statusesUpdateResults = statusesUpdateChoreo.run();
}
 
//---------------------------------------------------------------
 
 
ArrayList serialChars;      // Temporary storage for received serial data
int whichValueToAccum = 0;  // Which piece of data am I currently collecting?
boolean bJustBuilt = false; // Did I just finish collecting a datum?
 
void processSerial() {
 
  while (myPort.available() > 0) {
    char aChar = (char) myPort.read();
 
    // You'll need to add a block like one of these
    // if you want to add a 3rd sensor:
    if (aChar == 'A') {
      bJustBuilt = false;
      whichValueToAccum = 0;
    } else if (aChar == 'B') {
      bJustBuilt = false;
      whichValueToAccum = 1;
    } else if (((aChar == 13) || (aChar == 10)) && (!bJustBuilt)) {
      // If we just received a return or newline character, build the number:
      int accum = 0;
      int nChars = serialChars.size();
      for (int i=0; i < nChars; i++) {
        int n = (nChars - i) - 1;
        int aDigit = ((Integer)(serialChars.get(i))).intValue();
        accum += aDigit * (int)(pow(10, n));
      }
 
      // Set the global variable to the number we captured.
      // You'll need to add another block like one of these
      // if you want to add a 3rd sensor:
      if (whichValueToAccum == 0) {
        valueA = accum;
        // println ("A = " + valueA);
      } else if (whichValueToAccum == 1) {
        valueB = accum;
        // println ("B = " + valueB);
      }
 
      // Now clear the accumulator
      serialChars.clear();
      bJustBuilt = true;
    } else if ((aChar >= 48) &&
    (aChar <  
    57)) {
      // If the char is between '0' and '9', save it.
      int aDigit = (int)(aChar - '0');
      serialChars.add(aDigit);
    }
  }
}

Arduino Code:

int sensorValue1 = 0;  // variable to store the value coming from the other sensor
 
void setup() {
  Serial.begin(9600);  // initialize serial communications    
}
 
void loop() {
 
  // Read the value from the sensor 
 
  sensorValue1 = analogRead (A1);  // reads value from Analog input 1    
 
 
  Serial.print ("A"); 
  Serial.println (sensorValue1);  
 
  delay (50);   // wait a fraction of a second, to be polite
}

Comments are closed.