Theory and Practice of Tangible User Interfaces

Announcements

November 24, 2007
Reading for November 27th, are now posted. Enjoy!

October 2, 2007
To upload your thoughtless acts, create a new assignment page like any other lab. You'll see "Thoughtless Acts" listed as one of the assignment options.

May 24, 2008
This site has been archived and is no longer editable. Stay tuned for the next version, coming in the fall!

Lab 6: WindyFlight

Project Members:
Ken-ichi Ueda

Description

As you fly around a Google Earth, the pinwheel spins in proportion to your horizontal speed.

• DC Motor
• Transistor
• Resistor
• Diode

Arduino Code

I reused some code from some of the lab examples in my Arduino code

`// serial input vars`
`char serInString[100];`
`char cmd;`
`int motorPin = 9; // select the pin for the Motor`
`int val;`
`void setup() {`
`  Serial.begin(9600);`
`  analogWrite(motorPin, 0);`
`}`
`void loop() {`
`  readSerialString(serInString, 100);`
`  cmd = serInString[0];`
`  `
`  if (cmd == 's') { // if we should spin`
`    val = atoi(serInString+1);`
`    analogWrite(motorPin, val);`
`    Serial.print("Spin: ");`
`    Serial.println(val);`
`  }`
`  `
`  resetSerialString(serInString, 100); `
`  delay(100);          // Python does weird things if the lines come in too fast`
`}`
`//read a string from the serial and store it in an array`
`//you must supply the array variable`
`void readSerialString (char *strArray, int maxLength) {`
`  int i = 0;`
`  if(!Serial.available()) {`
`    return;`
`  }`
`  while (Serial.available() && i < maxLength) {`
`    strArray[i] = Serial.read();`
`    i++;`
`  }`
`}`
`void resetSerialString (char *strArray, int length) {`
`  for (int i = 0; i < length; i++) {`
`    strArray[i] = '\0';`
`  }`
`}`

Python Code

Requires the pyserial module for Python (http://pyserial.sourceforge.net) and the appscript module (http://appscript.sourceforge.net) . . . and a computer running OS X. Many thanks to Hannes Hesse for showing me how to control Arduino with Python (my code was partiall lifted from his). I should also point out that this code works fine, but freezes after a few minutes and leaves a process that seems unkillable.

`from serial import Serial`
`from appscript import *`
`from math import sqrt`
`def normalize(x):`
`    """Normalizes x to a value in the 0 to 255 range"""`
`    n = x / 1 * 255`
`    if n > 255: n = 255`
`    if n < 0: n = 0`
`    `
`    return n`
`if __name__ == '__main__':`
`    ser = Serial('/dev/tty.usbserial-A4001nLM', 9600)`
`    gearth = app('Google Earth')`
`    `
`    viewinfo = gearth.GetViewInfo()`
`    lastx = viewinfo[k.longitude]`
`    lasty = viewinfo[k.latitude]`
`    while 1:`
`        viewinfo = gearth.GetViewInfo()`
`        currx = viewinfo[k.longitude]`
`        curry = viewinfo[k.latitude]`
`        currvelo = sqrt(abs(currx - lastx) + abs(curry - lasty))`
`        print 'current velocity: %d' % currvelo`
`        ser.write('s%d' % normalize(currvelo))`
`        lastx = currx`
`        lasty = curry`
`    ser.close()`

WindyFlight