Wednesday, February 10, 2010

Last week in Interactive Exhibit Design we were playing with firmata, or so we thought. Braden, Jordan and I set up a circuit, first with a button press as shown below and the following two codes run separately on Arduino and Processing (which should have been our first hint that we were not actually sucessfully using firmata), then with the same two codes and a potentiometer.

Our button circuit connected to the Arudino

The potentiometer circuit. The gator clips were unwieldly but much easier to use.




Processing Code: Many Thanks to Tom Igoe
/*
Sensor Graphing Sketch
 This sketch takes raw bytes from the serial port at 9600 baud and graphs them.
 Created 20 April 2005
 Updated 5 August 2008
 by Tom Igoe
 */
import processing.serial.*;
Serial myPort;        // The serial port
int graphXPos = 1;    // the horizontal position of the graph:
void setup () {
  //size(400, 300);        // window size
  size(800,100);
  // List all the available serial ports
  println(Serial.list());
  // I know that the fisrt port in the serial list on my mac
  // is usually my Arduino module, so I open Serial.list()[0].
  // Open whatever port is the one you're using.
  myPort = new Serial(this, Serial.list()[0], 9600);
  // set inital background:
  background(48,31,65);
}
void draw () {
  // nothing happens in draw.  It all happens in SerialEvent()
}
void serialEvent (Serial myPort) {
  // get the byte:
  int inByte = myPort.read(); 
    // print it:
  println(inByte);
  // set the drawing color. Pick a pretty color:
  stroke(123,189,158);
  // draw the line:
  line(graphXPos, height, graphXPos, height - inByte);
  // at the edge of the screen, go back to the beginning:
  if (graphXPos >= width) {
    graphXPos = 0;
    // clear the screen:
    background(48,31,65); 
  } 
  else {
    // increment the horizontal position for the next reading:
    graphXPos++;
  }
}
Arduino Code: Thanks DojoDave!
/* Basic Digital Read
 * ------------------ 
 *
 * turns on and off a light emitting diode(LED) connected to digital  
 * pin 13, when pressing a pushbutton attached to pin 7. It illustrates the
 * concept of Active-Low, which consists in connecting buttons using a
 * 1K to 10K pull-up resistor.
 *
 * Created 1 December 2005
 * copyleft 2005 DojoDave <http://www.0j0.org>
 *
 */
int ledPin = 13; // choose the pin for the LED
int inPin = 7;   // choose the input pin (for a pushbutton)
int val = 0;     // variable for reading the pin status
void setup() {
  pinMode(ledPin, OUTPUT);  // declare LED as output
  pinMode(inPin, INPUT);    // declare pushbutton as input
}
void loop(){
  val = digitalRead(inPin);  // read input value
  if (val == HIGH) {         // check if the input is HIGH (button released)
    digitalWrite(ledPin, LOW);  // turn LED OFF
  } else {
    digitalWrite(ledPin, HIGH);  // turn LED ON
  }
}

When we connected the potentiometer we saw this:



Unfortunately through all of this we thought we were using Firmata, a firmware that can be uploaded to the arduino to allow Processing to talk directly to it without having to run two programs at once. As anyone who knows this stuff can see, we weren’t. The first and biggest hint is that we were running both Arduino and Processing to make our visualization work. What we did learn is that firmata is much harder to get working than we had thought!

No comments:

Post a Comment