Tuesday, February 10, 2009

Animal House


Abstract: The proposal for this project was to integrate an interactive puzzle into an environment in which it was placed. To begin the puzzle, a series of screens in the room would show animal scenes with a place for an animal puzzle piece to fit in. Once the habitat lights up, the child would have to find the animal shaped piece that matches the scene from a circulating tray located in the room. Once the correct animal and habitat match up, another scene lights up. When all scenes are completed correctly, a celebration of lights activates the room. As a demonstration, we chose a cow on a farm, an elephant in the jungle, a bird in the sky and a crab in the sea.

                      

 

Scenario

 

Hardware

 

2 Arduino Prototyping board

1 Arduino Motor Shield

4 Push button switches

4 LEDs (1 Orange, 1 Green, 1 Red, 1 Yellow)

1 Servo Motor

Wire

 

Code

Part 1: Sequential Puzzle Evaluator

 

int inPin[5]; // Setting the variable for the input from the 4 switches

int outPin[5]; // Seting the variable  for the output to the 4 LEDs

int val[5]; // State of the switch: either HIGH(pressed) or LOW(open)

int flag[5]; // Not used here, required for random number code.

 

int i, j, k, l, m, n; // Counter variables: only “I” and ”l” used here

long time; // Variable used to check current time

long debounce = 2000; // Debounce time to prevent noise from being read

 

void setup()

{

  inPin[1] = 2; // Input to pin 2 from switch 1

  inPin[2] = 4; // Input to pin 4 from switch 2

  inPin[3] = 6; // Input to pin 6 from switch 3

  inPin[4] = 8; // Input to pin 8 from switch 4

 

  outPin[1] = 10; // Output to pin 10 for LED 1 (Red)

  outPin[2] = 11; // Output to pin 11 for LED 2 (Orange)

  outPin[3] = 12; // Output to pin 12 for LED 3 (Green)

  outPin[4] = 13; // Output to pin 13 for LED 4 (Yellow)

 

 

// Setting up the modes of the input and output pins and initializing    // the status variable val.

 

  for (i=1;i<5; class="Apple-style-span" style=" ">++)

  {

    pinMode(inPin[i], INPUT);

    pinMode(outPin[i], OUTPUT);

    val[i] = 0;

  }

 a = random(5); // Not used here

  count = 0; // Not used here

}

 

// In this part of the code, we are always reading the status of the

// input switches. The program works sequentially, in that only the

// completion of the first task allows the second task to be reached.

// The first LED glows requiring the first switch to be pressed,

// resulting in successful completion of the first task), before the

// second LED glows and a similar pattern follows for the second, third // and fourth LED and switch pairs.

// After all four tasks are completed successfully, the program moves // into the celebration function where all the LEDs blink, before

// returning to the initial state of the program.

 

void loop()

{

  val[1] = digitalRead(inPin[1]);

  val[2] = digitalRead(inPin[2]);

  val[3] = digitalRead(inPin[3]);

  val[4] = digitalRead(inPin[4]);

 

// the “millis() – time > debounce” part of the attempts to ensure that

// a noise signal is not incorrectly for a successful completion of a // task. millis() is an Arduino function that provides elapsed time.

 

  if((val[1]==LOW) && (millis() - time > debounce))

  {

    digitalWrite(outPin[1],HIGH); // LED 1 glows

    digitalWrite(outPin[2],LOW); // LED 2 is off

    digitalWrite(outPin[3],LOW); // LED 3 is off

    digitalWrite(outPin[4],LOW); // LED 4 is off

    time = millis();// timestamp for this scenario occuring

  }

  if((val[1]==HIGH) && (millis() - time > debounce))

  {

      // val[1]==HIGH means first switch is pressed, now move to 2nd

      // puzzle.

    if(val[2]==LOW && (millis() - time > debounce))

    {

      digitalWrite(outPin[1],LOW); // LED 1 is off

      digitalWrite(outPin[2],HIGH); // LED 2 glows

      digitalWrite(outPin[3],LOW); // LED 3 is off

      digitalWrite(outPin[4],LOW); // LED 4 is off

      time = millis();

    }

    if((val[2]==HIGH) && (millis() - time > debounce))

    {

      if(val[3]==LOW && (millis() - time > debounce))

      {

        digitalWrite(outPin[1],LOW);

        digitalWrite(outPin[2],LOW);

        digitalWrite(outPin[3],HIGH);

        digitalWrite(outPin[4],LOW);

        time = millis();

      }

      if((val[3]==HIGH) && (millis() - time> debounce))

      {

        if(val[4]==LOW && (millis() - time > debounce))

        {

          digitalWrite(outPin[1],LOW);

          digitalWrite(outPin[2],LOW);

          digitalWrite(outPin[3],LOW);

          digitalWrite(outPin[4],HIGH);

          time = millis();

        }

        if((val[4]==HIGH) && (millis() - time > debounce))

        {

          digitalWrite(outPin[1],LOW);

          digitalWrite(outPin[2],LOW);

          digitalWrite(outPin[3],LOW);

          digitalWrite(outPin[4],LOW);

          celebration();

        }

      }

    }

  }

}

 

// The simple blink code provided in the Arduino examples is applied

// in the celebration code to get all the LEDs to blink 5 times.

 

void celebration()

{

  for(l=1;l<5;l++)

  {

    digitalWrite(outPin[1],HIGH);

    digitalWrite(outPin[2],HIGH);

    digitalWrite(outPin[3],HIGH);

    digitalWrite(outPin[4],HIGH);

    delay(500);

    digitalWrite(outPin[1],LOW);

    digitalWrite(outPin[2],LOW);

    digitalWrite(outPin[3],LOW);

    digitalWrite(outPin[4],LOW);

    delay(500);

  }

  digitalWrite(outPin[1],LOW);

  digitalWrite(outPin[2],LOW);

  digitalWrite(outPin[3],LOW);

  digitalWrite(outPin[4],LOW);

  delay(10000);

}

 

Part 2: Tray Rotator

 

#include >

 

Servo tray;

 

int pos = 0;

 

void setup()

{

  tray.attach(9);

 } 

 

void loop()

{

  for(pos = 0; pos <>

  {                                  

    tray.write(pos);

    delay(150);

  }

}

 

 

Issues

One issue that could not completely be resolved at the time of submission of the project was that of noise from the pushbutton switches. This turned out to be crucial for the system to work properly. Attempts were made to resolve this issue using software, but they all were unsuccessful. As pointed out by Ivan, a simple snubber circuit could be applied to possibly solve this issue using hardware, which was considered, but from simple observation, it was difficult to see how given that a change of state from LOW (button not pressed) to HIGH (button pressed) was occurring and even passing the software noise filter.

 

On occasion, it was also noticed that a change of state was registered simply be moving a finger in the vicinity of the switch without making contact with any part of the circuit. That said, we think it would be only fair to try the snubber circuit before declaring the problem as insurmountable.

 

Finally, we would like to use this situation as an example to warn users in this course about the danger of relying solely on the tiny pushbutton toggle switches as a sensor.





                          

No comments:

Post a Comment