Wednesday, December 3, 2014

Arduino Uses Motion Detection to Toggle an LED

=====================================
/*
Assumes a Parallax PIR on pin PIR (11 here) - no pullup resistor needed
http://www.parallax.com/product/28032
 */

int led = 10;           // the pin that the LED is attached to
int PIR = 11;    // pin that PIR is connected to..

int statePIR;
int prevPIR;
boolean lamp = false;


// the setup routine runs once when you press reset:
void setup()  {
  // declare pin 9 to be an output:
  pinMode(led, OUTPUT);
  pinMode( PIR, INPUT );
  digitalWrite( led, false );
}

// the loop routine runs over and over again forever:
void loop()  {

  statePIR = digitalRead( PIR );
  if( HIGH == statePIR && LOW == prevPIR) {
    lamp = !lamp;
  }
  prevPIR = statePIR;
  digitalWrite( led, lamp );
  delay( 10);
                          
}

No comments:

Post a Comment