Making Things Interactive

March 27, 2008

Boomboxes: State Machine

Filed under: 8: State Machine, Assignments, Jesse Chorng — Jesse @ 5:24 am
Tags:

For assignment 8b, I created a state machine that I will work on for the next two weeks to improve for the final project. In this one, there are three states: Listening, Audio Detected, and Party (or dancing lights).  Using a stereo input, the Arduino waits for an incoming signal. Once an audio source is connected and playing music, the Arduino detects it and changes states. Here’s the code:

/*
Boomboxes - Social Interaction Through Music
Small Undergraduate Research Grant

Jesse Chorng

*/

const int sListening = 0;          // The system is waiting for audio to be plugged in. Speakers/Lights are off
const int sAudioDetected = 1;      // The system detects audio being played. Speakers/Lights are on
const int sParty = 3;              // If there's enough people around, party time! Lights dance to music

int currentState = sListening;
int nextState = sAudioDetected;

int audioPin = 1;                  // Audio signal in from stereo jack
int partySwitch = 2;               // Push button to activate 'party' state

int listeningLed = 9;              // LED to signal 'listening' state
int detectedLed = 10;              // LED to signal audio signal detected; 'detected' state
int partyLed = 12;                 // LEDs start to pulse with music, 'party' state. IC3915 activated

int audioLevel = 0;                // Incoming audio level

void setup()  {

    pinMode(audioPin, INPUT);
    pinMode(partySwitch, INPUT);
    pinMode(listeningLed, OUTPUT);
    pinMode(detectedLed, OUTPUT);
    pinMode(partyLed, OUTPUT);
    Serial.begin(9600);

}

void loop()  {

    //digitalWrite(partyLed, HIGH);
    //AudioLevelCheck();
    //Serial.println(audioLevel);

    switch(currentState) {
       case sListening:                                // Listening State
           digitalWrite(listeningLed, HIGH);           // Listening LED on
           digitalWrite(detectedLed, LOW);
           digitalWrite(partyLed, LOW);
           AudioLevelCheck();                          // Check incoming audio level
           if (audioLevel > 1) {                       // If there is incoming audio, move to 'detected' state
             currentState = sAudioDetected;
           }
           break;

       case sAudioDetected:                            // Audio Detected State
           digitalWrite(detectedLed, HIGH);            // Audio Detected LED on
           digitalWrite(listeningLed, LOW);
           digitalWrite(partyLed, LOW);
           AudioLevelCheck();                          // Check incoming audio level
           if (audioLevel = 500) {       // If push button pressed, go to 'party' state
             currentState = sParty;
           }
           break;

       case sParty:                                    // Party State
           digitalWrite(partyLed, HIGH);               // Party LEDs (motion) on
           digitalWrite(listeningLed, LOW);
           digitalWrite(detectedLed, LOW);
           AudioLevelCheck();
           if (audioLevel < 1) {                       // Return to 'listening' state if no audio
             currentState = sListening;
           }
           if (analogRead(partySwitch) <= 500) {       // Return to 'detected' state if no pushbutton
             currentState = sAudioDetected;
           }
           break;

       default:                                        // Default Case aka Error Case
           Serial.println("ERROR: default state");
           currentState = sListening;                  // Reset back to 'listening' state
           nextState = sAudioDetected;
           break; 

    }
}

void AudioLevelCheck()  {                              // Checks incoming Audio Level by taking
    int count = 0;                                     // 5 input values in 1 second intervals
    for (int i = 0; i < 5; i++)  {                     // then takes the average.
         count += analogRead(audioPin);
         delay(1000);
    }
    audioLevel = count / 5;                            // Audio Level stores the average
}

 

For the final, I hope to replace the pushbutton with sensors that will detect activity. The Arduino will then sense how many people are using Boomboxes and interact by dancing lights when more people are around and/or moving. 

No Comments Yet »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

You must be logged in to post a comment.

Blog at WordPress.com.