User login

Powered by Drupal, an open source content management system

Theory and Practice of Tangible User Interfaces

Lab 05: Input/Output Coincidence Lab Assignment

Submitted by xiaomeng on Wed, 10/08/2008 - 23:44

Assignment: Input / Output Coincidence Lab Assignment

Collaborators:

For this assignment, I made a musical jewelry box. When the lid of the box is open, the photocell detects the light, turns the LED on and starts playing random "music." When the lid is closed, both the LED and the speaker is off.

 

Components Used

* 1 Light Emitting Diode (LED)
* 1 220 OHM resistor
* Wires
* Arduino board
* Breadboard
* 1 photocell
* 1 piezo

 

 

 

Code:

/* Musical Jewelry Box
* --------
*
*
* Created 10/09/08
* Modified by Xiaomeng Zhong
*
*/

int potPin = 0;   
int speakerPin = 7;
int ledPin = 9;

int val = 0;

void setup() {
pinMode(speakerPin, OUTPUT);
pinMode(ledPin, OUTPUT); 
beginSerial(9600);
Serial.println("ready");
}

void loop() {
//Speaker and LED: OFF

digitalWrite(speakerPin, LOW);
digitalWrite(ledPin, LOW);

val = analogRead(potPin);    // read value from the sensor
val = val*2;                 // process the value a little
//val = val/2;                 // process the value a little
Serial.println(val);
if (val>40) //when the box is opened, turn on LED and play sounds
{

digitalWrite(ledPin, HIGH); 
for( int i=0; i<500; i++ ) {  // play it for 50 cycles
digitalWrite(speakerPin, HIGH);
delayMicroseconds(val);
digitalWrite(speakerPin, LOW);
delayMicroseconds(val);
}
}
}