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