Making Furniture Interactive

September 24, 2007

IntroFlirt (a “theremin” made with a photosensor and piezo buzzer)

Filed under: Exercise 4: Four State Machines,Jenn Gooch — jenngooch @ 10:27 pm

My four state project, IntroFlirt, uses a photoresistor and a piezo as its sensor and responder. Its four states are: sleep, attract (once it senses change in light, but then feels abandoned, it will beep for attention), reward (the piezo tonally reciprocates your hand blocking light from the photoresistor, a sort of theremin), and alarm (it freaks out and squeals if you get too close). There’s not a lot of transition between the states, but it’s been slow going for me.

Here’s video of me playing IntroFlirt: its “attract” and “alarm” states, as well as my attempting to play, “Daisy Bell”, a little 2001: A Space Odyssey reference, in light of last week’s discussion:

int piezo = 9; // piezo on PWM pin
int sensor = 5;// photsensor on analog pin

int duration = 16000; //for playTone loop
int sensorVal = 0;
int timer = 0; 
int sleeptimer = 0;
int i = 0;

void setup ()
{
  pinMode(sensor, INPUT);
  pinMode(piezo, OUTPUT);
  Serial.begin (9600);
}
  
void loop () 
{
  sensorVal=analogRead(sensor);
  Serial.print(sensorVal);
  Serial.print("   ");
 
 //Attract  (if INTROFLIRT has been waken, but then abandoned, it will beep for attention)
  if (sensorVal<200 && timer<2000)//sets up timer for attraction beep
  {
    timer=timer++;
    Serial.println(timer);
    Serial.print("   ");
    Serial.println(sleeptimer);
  }

  else if (sensorVal<200 &amp;&amp; timer>1999)//triggers attraction beep
  {
    for (i = 0; i < 15; i ++) playTone (2000);//sets length of attract beep
      timer=0;
      sleeptimer++;//counts how many times it tries to attract before giving up and sleeping
 //Sleep   (INTROFLIRT gives up beeping if abandoned for some time and sleeps)
      if (sleeptimer==5)
      {
        while(sensorVal+20>analogRead(sensor) &amp;&amp; analogRead(sensor)>sensorVal-20);
        sleeptimer=0;
      }
   }
 //Reward (INTROFLIRT reciprocate hand motions with sound equivalent) 
  else if (sensorVal>200 &amp;&amp; sensorVal<850)
  {
    playTone (sensorVal);
    delay (1);
  }
 //Alarm (INTROFLIRT freaks out if you smother it and squeals)
  else if (sensorVal>850)
  {
    playTone(230);
  }
}

// PLAY TONE from http://www.arduino.cc/en/Tutorial/PlayMelody
// uses input from photoresistor to output piezo tone, like a photosensor Theremin
void playTone(int tone) 
{
  long elapsed_time = 0;
  while (elapsed_time &lt; duration)
  {
    digitalWrite(piezo,HIGH);
    delayMicroseconds(tone / 2);
    digitalWrite(piezo, LOW);
    delayMicroseconds(tone / 2);
    
    elapsed_time += (tone);   // Keep track of how long we pulsed
  }
}

Advertisements

1 Comment »

  1. Very nice! looks like a lot of fun to play with! I especially likes how it squeals when you smother it. Can’t help imagining that combined with some movement like jumping up and down.

    Comment by tiagororke — September 25, 2007 @ 12:46 pm | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: