Making Things Interactive

March 26, 2008

Acceleromter Toy

Filed under: 8: State Machine, Assignments, Christopher Bridgman — cbridgma @ 11:02 pm

So here is the accelerometer toy. This toy exhibits 4 states. Off, attract, play, and angry. Basically, you use a potentiometer switch to turn it on. Once it is on, it goes into attract mode and blinks a blue light. Next, if the accelerometer detects movement, it will go into play mode, and an orange led will light up. And if the accelerometer detects too much movement, it will go into angry mode, where it will light up a red light and set a buzzer off denoting to stop playing so hard.

State machine toy diagram- Circuit Diagram for the final set up

SM Toy BB set up- Picture of the final bread board set up

Code:

int statusPin = 13;                // LED connected to digital pin 13
int xPin = 3;                      // X input from accelerometer
int yPin = 4;                     // Y input from accelerometer
int zPin = 5;                     // Z input from accelerometer
int redPin = 9;                    // Red LED's connected to pin 6
int orangePin = 10;                 // Orange LED's connected to pin 7
int bluePin = 11;                   // Blue LED's connected to pin 5
int buzzPin = 12;                  // buzzer connected to pin 12
int potPin = 2;                    // potentiometer connected to pin 2
int xval;
int yval;
int zval;
int potval;
int val;

void setup()                    // run once, when the sketch starts
{
  pinMode(statusPin, OUTPUT);      // sets the digital pin as output
  pinMode(redPin, OUTPUT);         // sets the digital pin as output
  pinMode(orangePin, OUTPUT);      // sets the digital pin as output
  pinMode(bluePin,OUTPUT);         // sets the digital pin as output
  pinMode(buzzPin,OUTPUT);        // sets the digital pin as output
  pinMode(xPin, INPUT);            // sets the analog pin as input
  pinMode(yPin, INPUT);            // sets the analog pin as input
  pinMode(zPin, INPUT);            // sets the analog pin as input
  pinMode(potPin, INPUT);          //sets the analog pin as input
  Serial.begin(9600); 
}

void loop()                     // run over and over again
{
  xval = analogRead(xPin);        // set xval egual to the xPin reading
  yval = analogRead(yPin);        // set yval equal to the yPin reading
  zval = analogRead(zPin);        // set zval equal to the zPin reading
  potval = analogRead(potPin);    // set potval equalt to the potentiometer reading

  Serial.print("X Value: ");
  Serial.println(xval);   

  Serial.print("Y Value: ");
  Serial.println(yval);  

  Serial.print("Z Value: ");
  Serial.println(zval);  

  Serial.print("Potentiometer:");
  Serial.println(potval);

  digitalWrite(statusPin, HIGH);
  delay(15);
  digitalWrite(statusPin, LOW);
  delay(15);

  if ( potval > 1000){
//ATTRACT
    if ( (xval < 560) && (yval < 560) && (zval < 560))    // if x-y-z values are
    {
      digitalWrite(bluePin,HIGH);
      digitalWrite(orangePin,LOW);
      digitalWrite(redPin,LOW);
      digitalWrite(buzzPin,LOW);
      Serial.print("Attract: Come Play With ME!!");
    }

//PLAY
    if ( (xval > 560) && (xval < 660))     // if x-y-z values are
    {
      digitalWrite(orangePin,HIGH);
      digitalWrite(bluePin,LOW);
      digitalWrite(redPin,LOW);
      digitalWrite(buzzPin,LOW);
      Serial.print("Play Play Play!!");
    }
    else {
      if ( (yval > 560) && (yval < 660))
      {
        digitalWrite(orangePin,HIGH);
        digitalWrite(bluePin,LOW);
        digitalWrite(redPin,LOW);
        digitalWrite(buzzPin,LOW);
        Serial.print("Play Play Play!!");
      }

      else {
        if ( (zval > 560) && (zval < 660))
        {
          digitalWrite(orangePin,HIGH);
          digitalWrite(bluePin,LOW);
          digitalWrite(redPin,LOW);
          digitalWrite(buzzPin,LOW);
          Serial.print("Play Play Play!!");
        }
      } 
    }
    //ANGRY
    if (xval>700) {
      digitalWrite(redPin,HIGH);
      digitalWrite(bluePin,LOW);
      digitalWrite(orangePin,LOW);
      digitalWrite(buzzPin,HIGH);
      Serial.print("STOP IT!!");
    }
    else {
      if (yval > 700)
      {
        digitalWrite(redPin,HIGH);
        digitalWrite(bluePin,LOW);
        digitalWrite(orangePin,LOW);
        digitalWrite(buzzPin,HIGH);
        Serial.print("STOP IT!!");
      }

      else{
        if (zval > 700)
        {
          digitalWrite(redPin,HIGH);
          digitalWrite(bluePin,LOW);
          digitalWrite(orangePin,LOW);
          digitalWrite(buzzPin,HIGH);
          Serial.print("STOP IT!!");
        }
      }
    }
  }
  //OFF
  else {
    digitalWrite(redPin,LOW);
    digitalWrite(bluePin,LOW);
    digitalWrite(orangePin,LOW);
    digitalWrite(buzzPin,LOW);
  }

}

FINAL SHOW SCHEDULE (?? )

Filed under: Course Materials — mdgross @ 6:06 pm

We are scheduling the final show for Making Things Interactive.

Here are the times and dates in order of preference.
(a) Wednesday May 7, 4-6 PM

(b) Wednesday May 7, 5-7 PM

(c) Thursday May 8, 4-6 PM

(d) Thursday May 8, 5-7 PM

Please send Mark an email message no later than Thursday March 27 indicating which of these times and dates you CAN be present.

No response counts as “I can make it any of these times”.

Please note that your work must be installed and running at least one hour BEFORE the start of the show, so keep this in mind when indicating your time/date preferences.

More State Machine Examples

Filed under: Class Notes, Course Materials — jet @ 2:16 pm

There are two examples in the PDF:

  1. using the switch/case statement to implement a state machine.  It’s not crucial that you understand this, but if it makes sense to you, it’s cleaner than using repeated if/else statements.  There are explanations in the comments, but also read the arduino docs.
  2. changing PulseLed so that it can read a button while making the LED grow brighter and darker.  This involves havnig PulseLed return a “boolean” value of “true” or “false” and changing the statemachine to use this value to change state.  See the comments for full details.

state machine examples #2

Blog at WordPress.com.