As computing becomes more ubiquitous in our objects, designers need to be more aware of how to design meaningful interactions into electronically enhanced objects. At the University of Washington, a class of junior Interaction Design majors is exploring this question. These pages chronicle their efforts.

Thursday, April 26, 2012

Miles & Kim: Motion Sensor Experiment

For our sensor we chose to use a PIR Motion Sensor. The sensor detects motion using infrared and lights up during movement. We started with a sketch called PIR Sensor Tester, which detects motion and prints "Motion Detected" in the serial monitor. We altered the code to make the LED fade in and out, and have a blue LED be on when no motion was detected.

Here is the code we used:


/*
 * PIR sensor tester
 */

const int ledPin = 9;           // choose the pin for the LED
const int bluePin = 6;
int inputPin = 2;               // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status
int i = 0;

void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(bluePin, OUTPUT);
  pinMode(inputPin, INPUT);     // declare sensor as input

  Serial.begin(9600);
}

void loop(){
  val = digitalRead(inputPin);  // read input value

  if (val == HIGH) {            // check if the input is HIGH
   
    if (pirState == LOW) {      // we have just turned on
      Serial.println("Motion detected!");  // We only want to print on the output change, not state
      pirState = HIGH;
     
      for(i = 255; i >= 0; i--) {
        analogWrite(bluePin, 0);
        analogWrite(ledPin, (255 + -i));
        delay(10);
      }
     
      /*for(i = 0; i < 255; i++) {
        analogWrite(ledPin, i);
        delay(10);
      }
      */
    }
     
  } else {
    //digitalWrite(ledPin, LOW); // turn LED OFF
   
    if (pirState == HIGH){     // we have just turned of
      Serial.println("Motion ended!"); // We only want to print on the output change, not state
      pirState = LOW;
     
      for(i = 255; i >= 0; i--) {
        analogWrite(ledPin, 0);
        analogWrite(bluePin, (255 + -i));
        delay(10);
      }
     
     /* for(i = 0; i < 255; i++) {
        analogWrite(bluePin, i);
        delay(10);
      } */
    }
  }
}

Here is a video of our code in action:


No comments:

Post a Comment