User login

Powered by Drupal, an open source content management system

Theory and Practice of Tangible User Interfaces

Bashful duck, Jewelry box

Submitted by bobacita on Mon, 10/06/2008 - 17:07

Assignment: Input / Output Coincidence Lab Assignment

Collaborators:

Assignment: Input / Output Coincidence Lab Assignment
Collaborators:

Description:

For this input/output coincidence assignment, I made two projects. The first is a musical jewelry box. When you open the lid, it plays a traditional melody.

The second is Dilbert the Bashful Duck. If you kiss him on the beak, his cheeks turn bright red. If you press his foot, he plays the song 'Six Little Ducks'.  If you want to sing along, the lyrics are here: http://www.kididdles.com/lyrics/s009.html.

 

Video:

See a video of the musical jewelry box here: http://people.ischool.berkeley.edu/~carolc/MVI_3035.avi.

See a video of the duck here: http://people.ischool.berkeley.edu/~carolc/MVI_3033-rot.mov.

 

Photos:

 

 

Components:

Stuffed duck

Jewelry container

1 Arduino board

2 FSRs

2 LEDs

1 Piezo speaker

2 220-ohm resistors

2 10-ohm resistors

Many wires

 

Code

Jewelry box

 

/* Jewelry box

 * -----------

 *

 * Program to play a traditional jewelry box melody

 * The calculation of the tones is made following the mathematical

 * operation:

 *

 *       timeHigh = 1/(2 * toneFrequency) = period / 2

 *

 * where the different tones are described as in the table:

 *

 * note frequency period PW (timeHigh)

 * c        261 Hz        3830 1915

 * d        294 Hz        3400 1700

 * e        329 Hz        3038 1519

 * f        349 Hz        2864 1432

 * g        392 Hz        2550 1275

 * a        440 Hz        2272 1136

 * b        493 Hz        2028 1014

 * C        523 Hz        1912 956

 *

 * (cleft) 2005 D. Cuartielles for K3

 * Adapted by Carol Chen from the play_melody.txt program

 */

 

int photoPin = 0;

int speakerOut = 7;

// la so# la so% la mi so fa re

// La re fa so la mi so la

 

byte names[] = {'A', 'c', 'd', 'e', 'f', 'g', 'h', 'a', 'b', 'C'};  

int tones[] = {2295, 1915, 1700, 1519, 1432, 1275, 1206, 1136, 1014, 956};

//byte melody[] = "2f2h2f2h2f2c2e2dc";

byte melody[] = "4a4h4a4h4a4e4g4f8d4A4d4f8g4A4e4g8a";

// count length: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0

//                                10                  20                  30

int count = 0;

int count2 = 0;

int count3 = 0;

int val = 0;

int MAX_COUNT = 24;

int statePin = LOW;

 

void setup() {

 pinMode(speakerOut, OUTPUT);

 Serial.begin(9600);

}

 

void loop() {

  val = analogRead(photoPin);

  Serial.println(val);

  if(val > 800) {

    digitalWrite(speakerOut, LOW);     

    for (count = 0; count < MAX_COUNT; count++) {

      statePin = !statePin;

      for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3++) {

        for (count2=0;count2<8;count2++) {

          if (names[count2] == melody[count*2 + 1]) {       

            digitalWrite(speakerOut,HIGH);

            delayMicroseconds(tones[count2]);

            digitalWrite(speakerOut, LOW);

            delayMicroseconds(tones[count2]);

          } 

          if (melody[count*2 + 1] == 'p') {

            // make a pause of a certain size

            digitalWrite(speakerOut, 0);

            delayMicroseconds(500);

          }

        }

      }

    }    

  }

}

 

Duck

 

// Duck friend
// Pressing Duck's beak will cause its cheeks to turn red.
// Pressing Duck's foot will cause it to sing a song.
int ledPin1 = 11;
int ledPin2 = 10;
int speakerOut = 7;
int fsrPin1 = 0; // music
int fsrPin2 = 5; // blush
byte names[] = {'c', 'd', 'e', 'f', 'g', 'z', 'a', 'y', 'b', 'C', 'D', 'x', 'E', 'F', 'G', 'w', 'A', 'v', 'B', 'Z'};  
int tones[] = {1915, 1700, 1519, 1432, 1275, 1205, 1136, 1073, 1014, 956, 852, 804, 759, 716, 638, 602, 568, 536, 506, 426};
//byte melody[] = "2d2a1f2c2d2a2d2c2f2d2a2c2d2a1f2c2d2a2a2g2p8p8p8p";
byte melody[] = "8G4y4y4y8G8G8F8F1p8F8y4F4F4y8F8x8x1p4G4G4G4y4y4g4G4G4G4F4F4F8F1p4v4v4v4v4Z4v4w4G8x8x";
// count length: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
//                                10                  20                  30
int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 42; // melody[] size divided by 2
int statePin = LOW;
int val1 = 0;
int val2 = 0;
int blushVal = 255;
void setup() {
 pinMode(ledPin1, OUTPUT);
 pinMode(ledPin2, OUTPUT);
 pinMode(speakerOut, OUTPUT);
 Serial.begin(9600);
}
void loop() {
  val1 = analogRead(fsrPin1);
  val2 = analogRead(fsrPin2);
  Serial.print("Belly FSR val: ");
  Serial.println(val1);
  Serial.print("Beak FSR val: ");
  Serial.println(val2);
  
  if(val2 > 50) {
    analogWrite(ledPin1, blushVal);
    analogWrite(ledPin2, blushVal);
    blushVal -= 1;
    digitalWrite(speakerOut, 0);     
  } else {
    analogWrite(ledPin1, 0);
    analogWrite(ledPin2, 0);
  }
  
  if(val1 > 50) {
    analogWrite(ledPin1, 0);
    analogWrite(ledPin2, 0);
    digitalWrite(speakerOut, 0);     
    for (count = 0; count < MAX_COUNT; count++) {
      statePin = !statePin;
      for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3++) {
        for (count2=0;count2<20;count2++) { // 20 is number of notes
          if (names[count2] == melody[count*2 + 1]) {       
            digitalWrite(speakerOut,HIGH);
            delayMicroseconds(tones[count2]);
            digitalWrite(speakerOut, LOW);
            delayMicroseconds(tones[count2]);
          } 
          if (melody[count*2 + 1] == 'p') {
            // make a pause of a certain size
            digitalWrite(speakerOut, 0);
            delayMicroseconds(500);
          }
        }
      }
      for (count3 = 0; count3 <= 30; count3++) {
        digitalWrite(speakerOut, 0);
            delayMicroseconds(80);
      }
    }
  }
}