User login

Powered by Drupal, an open source content management system

Theory and Practice of Tangible User Interfaces

Assignment 6: Fun with Piezo I/O

Submitted by michael_lee on Tue, 10/07/2008 - 00:00

Assignment: Input / Output Coincidence Lab Assignment

Collaborators:

Description

For this lab, I followed the directions given in class. I thought it would be interesting to use the piezo as an input device like Kimiko mentioned in class. It took me quite a while to figure it out because the example provided by the Arduino site provided a misleading picture, and code that didn't seem to work (the passed values appeared to be strange characters instead of integer values). Once I finally got it figured out, I was able to create the following:

My arduino was used with one piezo speaker as output, and another as input. When the input piezo speaker is struck, the output piexo speaker produces a tone. This is in conjunction with a (yellow) LED flash. The pitch of the tone can be adjusted by the potentiometer. The potentiometer is also linked with a (red) LED that changes intensity based on the pot setting.

Finally, if you run the provided processing script I created, the ardunio should send a value to processing, which in turn talks to the Pleo. Pleo doesn't like high pitched noises, so if he hears one, he'll yelp. If it's a low pitched noise, he likes it and will yell "ta-da!". Unfortuntately, the communications with the pleo serial monitor are kind of funky so your milage may vary. Also, Pleo doesn't like having too many commands sent to him at once, so make sure you pace yourself! (...or else he'll terminate the connection).

 

Components Used

  • x1 1M Ohm resistor
  • x1 10k Ohm resistor
  • x2 220 Ohm resistors
  • x2 Piezo Speakers (one for input, one for output)
  • x2 LEDs - red, & yellow
  • x1 Potentiometer
  • Wires
  • 1 Arduino NG board
  • 1 Breadboard
  • 1 Pleo

Images

Full shot of the device. Inputs towards the front (pot and piezo), and output towards the back (LEDs and piezo).

 

Closeup view of the wiring. Going from bottom-to-top, the first resistor (1M) is used for the piezo input. The resistors in the middle are used for the LEDs, and the topmost one is used to quiet down the piezo output speaker (I think my neighbors were getting annoyed at the constant high pitched beeping).

 

Here the pot is set at ~mid-intensity as you can tell by the red LED. When I tap the input piezo, the device makes the appropriate beeping sound and momentarily lights up the yellow LED.

 

The ardunio is plugged into a USB port, and Pleo is plugged into a seperate USB port. The arduino sends a serial data to processing which in turn reads that, and sends a command to pleo. Pleo will/should either yelp or say "tada!" accordingly.

 

Ardunio Source Code

int ledPin = 13;     // select the pin for the LED (digital)
int ledPinFade = 9; // select the pin for the LED (digital+PWM)
int speakerPin = 7; // piezo output pin (digital)
int knockPin = 0; // piezo input pin (analog)
int potPin = 5; // pot pin to control pitch (analog)

int pzoVal = 0; // variable to store the value coming from the piezo sensor
int potVal = 0; // variable to store the value coming from the pot sensor
int THRESHOLD = 60; // Threshold to count as a knock

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

void loop() {
digitalWrite(speakerPin, LOW); // Reset speaker to OFF
digitalWrite(ledPin, LOW); // Reset LED to OFF

pzoVal = analogRead(knockPin); // Read value from Piezo input
potVal = analogRead(potPin); // Read value from pot

analogWrite(ledPinFade, potVal/4); // Set fader LED

if (pzoVal >= THRESHOLD) // If knock detected
{
digitalWrite(ledPin, HIGH); // Turn on momentary LED

Serial.println(potVal/4); // Send pot value

for(int i = 0; i < 500; i++ ) // Turn on momentary sound (pot-based pitch sound)
{
digitalWrite(speakerPin, HIGH);
delayMicroseconds(potVal/2);
digitalWrite(speakerPin, LOW);
delayMicroseconds(potVal/2);
}
}
}

 

Processing Source Code


import processing.serial.*;

Serial inPort;
Serial outPort;

String buf="";
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10


void setup() {
size(300,300);
frameRate(10);
smooth();
background(40,40,40);
noStroke();
inPort = new Serial(this, "COM11", 9600); // Input (Ardunio) port
outPort = new Serial(this, Serial.list()[0], 115200); // Output (Pleo) port
println(Serial.list());
}
void draw() {
}

// called whenever serial data arrives
void serialEvent(Serial p) {
int c = inPort.read();
if (c != lf && c != cr) {
buf += char(c);
}
if (c == lf) {
int val = int(buf);
println("val="+val);

if (val < 110)
{
outPort.write("sound play ta_daa");
outPort.write(cr);

}
else if (val < 220)
{
outPort.write("sound play surprised_yipe");
outPort.write(cr);
}
else
{
outPort.write("sound play coughs");
outPort.write(cr);
}

buf = "";
}
}