As computing becomes more ubiquitous in our objects, designers need to be more aware of how to design meaningful interactions into electronically enhanced objects. At the University of Washington, a class of junior Interaction Design majors is exploring this question. These pages chronicle their efforts.

Tuesday, May 15, 2012

Shelby/Bridget vs. Cutting in Line

Team Bridget/Shelby reporting!

Just as an update: when we last posted, we decided we wanted to create a social reproach for line-cutters. We planned to do so by embarrassing the line-cutter in some way, or to take a photo of them so that they wouldn’t be able to receive service when they reached the front of the line. Due to difficulty in pinpointing exactly who it is that has cut in line, we have since decided to take a different point of view: we will instead collect and display data about line cutting. We originally thought that we would sense the situation of line-cutting using sensors, but it would be difficult to get the sensors to actually recognize line-cutting, since the situation is a social one. In addition, we realized that a large number of very accurate multi-purpose sensors already exist within a line: people. People with smart phones. Tablets. Computers. Devices of all sorts, shapes, and sizes! We therefore decided to use Twitter to allow people to communicate that they had been cut in line. Through some research, we found some projects that others had already done that allowed them to communicate through Twitter to their Arduino, which then did something cool. This instructable made the process seem fairly straightforward, so we decided to adapt the process to our own project, using python to communicate between Twitter and the Arduino. Last Thursday, we decided to try to get our Arduino to blink an LED when @cut_in_line was mentioned.

We made a fair amount of progress this past weekend with this goal. We tried playing with the Python-Twitter library, as was our original plan, but upon further reflection realized that THIS WAS A VERY BAD IDEA, since we were not yet familiar with the… peculiarities of Python (though we did finally manage to figure out how to even install and import the Python-Twitter library and its dependencies… this is beside the point, but I thought I’d share). We discovered that it is possible to do the very same thing with Processing, and fortunately, we are already familiar with Processing!

We found this instructable, which proved to be very useful. We first downloaded the contributed libraries twitter4j and Arduino-Processing for Processing. Installing these libraries was somehow really confusing, due to conflicting documentation, and took much longer than it should have, but in the end, we found that this was the correct way to do it. We created a Twitter account, and registered it as an application. We then adapted the Processing code in the instructable to fit our own needs for this project. The code is as follows:

 import processing.serial.*;

import twitter4j.conf.*;
import twitter4j.internal.async.*;
import twitter4j.internal.org.json.*;
import twitter4j.internal.logging.*;
import twitter4j.json.*;
import twitter4j.internal.util.*;
import twitter4j.management.*;
import twitter4j.auth.*;
import twitter4j.api.*;
import twitter4j.util.*;
import twitter4j.internal.http.*;
import twitter4j.*;
import twitter4j.internal.json.*;

static String OAuthConsumerKey = "UiOEemSn7dB28nWFLJ78ng";
static String OAuthConsumerSecret = "jXAtBALSUU0zVZkt7HADmmxloCQfTXg2WJkqVsFxA";
static String AccessToken = "578379050-gl83Yc6JZ4d1XpDephnsRNkUOor3e0vdo7jBvdTl";
static String AccessTokenSecret = "G0NtjBx2qkqggHC4sLYigIk27RhKMXEyyLLqoeILjNE";

Serial arduino;
Twitter twitter = new TwitterFactory().getInstance();

String oldID = "";

void setup() {
size(125, 125);
frameRate(10);
background(0);
println(Serial.list());
String arduinoPort = Serial.list()[0];
arduino = new Serial(this, arduinoPort, 9600);
loginTwitter();
}

void loginTwitter() {
twitter.setOAuthConsumer(OAuthConsumerKey, OAuthConsumerSecret);
AccessToken accessToken = loadAccessToken();
twitter.setOAuthAccessToken(accessToken);
}

private static AccessToken loadAccessToken() {
return new AccessToken(AccessToken, AccessTokenSecret);
}

void draw() {
background(0);
text("@cut_in_line", 35, 65);
getMention();
delay(15000); // wait 15 seconds to avoid Twitter Rate Limit
}


void getMention() {
List mentions = null;
try {
mentions = twitter.getMentions();
}
catch(TwitterException e) {
println("Exception: " + e + "; statusCode: " + e.getStatusCode());
}
Status status = (Status)mentions.get(0);
String newID = str(status.getId());
if (oldID.equals(newID) == false){
oldID = newID;
println(status.getText()+", by @"+status.getUser().getScreenName());
arduino.write(1); // arduino gets 1
}
}


We then adapted the Arduino code in the same instructable to fit our needs, which were much simpler than in the instructable. This is the code:

 String state = "normal";
const int LED = 13;

void setup() {
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
}

void loop() {
  listenToSerial();
  setState(state);
}

void setState(String s) {
  if (s == "normal") normal();
  if (s == "mention") mention();
}

void listenToSerial() {
  int serialMsg = 0;
  if (Serial.available()) {
    serialMsg = Serial.read();
    if (serialMsg == 1) state = "mention"; 
  }
  else {
    normal();
  }
}

void normal() {
  state = "normal";
  if (state == "normal") {
    digitalWrite(LED, LOW);
  }
  else {
    mention();
  }
}

void mention() {
  state = "mention";
  if (state == "mention") {
    digitalWrite (LED, HIGH);
    delay (500);
    digitalWrite (LED, LOW);
    delay (500);
    digitalWrite (LED, HIGH);
    delay (500);
    digitalWrite (LED, LOW);
    delay (500);
    digitalWrite (LED, HIGH);
    delay (500);
    digitalWrite (LED, LOW);
    delay (500);
  }
}


We uploaded the code to the Arduino and plugged a LED into the 13 and ground pins. Then we convinced someone to mention @cut_in_line in a tweet (thanks Kelly!). When we run the Processing sketch, the LED blinks 3 times and then returns to the normal state.

Next, in terms of code, is probably to figure out how to make the output react appropriately for multiple tweets. So far, we have only gotten one person to spam their Twitter updates and mention @cut_in_line, so we're not quite sure what will happen when there are two "mentions" (will the output be the same, or will it happen twice?). We will also be attempting to figure out is whether our LED will blink when it notices a new "mention," because currently the LED blinks and returns to its normal state when we run the Processing sketch (as a result of our existing "mention"), but whether it will do this in real time, we are not yet sure. Also, we would like to explore whether it is possible for a person to tweet @cut_in_line and a number, to report that they had been cut in line by multiple people. We're working on making a LED number board that will eventually display the number of people who cut in line. We will be soldering and appropriately wiring LEDs and resistors to a perfboard.



COOL!

No comments:

Post a Comment