plamoni
Arduino Motion Detector
This is an Arduino-based Motion Detector I created. Upon pressing the button, it will arm 10 seconds later. Then beep and blink the LEDs when motion is detected. It can be disarmed by pressing the same button. The code is as follows:
/*****
* By Pete Lamonica
* Released under a Creative Commons Non-Commerical/Attribution/Share-Alike
* license
* creativecommons.org/licenses/by-nc-sa/2.0/
****/
#define MOTION_PIN 0
#define SPEAKER_PIN 9
#define RED_LED_PIN 2
#define GREEN_LED_PIN 3
#define ARM_PIN 4
#define SECONDS_TO_ARM 10
//defines what "motion" is. There's a pull-up resistor on the
// motion sensor, so "high" is motion, while "low" is no motion.
// I allowed some fuzziness on the "motion"
#define MOTION (analogRead(MOTION_PIN)=1000)
//Plays a tone of a given pitch
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(SPEAKER_PIN, HIGH);
delayMicroseconds(tone);
digitalWrite(SPEAKER_PIN, LOW);
delayMicroseconds(tone);
}
}
//will beep for about 3 seconds and check for disarm in the meantime.
//Could arrange a hardware interrupt to do the same thing
void alarm() {
for(int i=0; i<3; i++) {
if(checkForDisarm()) return;
digitalWrite(RED_LED_PIN, HIGH);
playTone(1432, 300); //F
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, HIGH);
if(checkForDisarm()) return;
playTone(1915, 300); //C
if(checkForDisarm()) return;
digitalWrite(GREEN_LED_PIN, LOW);
delay(400);
if(checkForDisarm()) return;
}
}
boolean armed = false; //armed status
//arm the device.
void arm() {
armed = true;
for(int i=0; i<SECONDS_TO_ARM/2; i++) {
digitalWrite(RED_LED_PIN, LOW);
delay(1000);
if(checkForDisarm()) return;
digitalWrite(RED_LED_PIN, HIGH);
delay(1000);
if(checkForDisarm()) return;
}
}
//Check if the system should be disarmed and do so if that's the case.
boolean checkForDisarm() {
if(digitalRead(ARM_PIN) == HIGH && armed) {
armed = false;
digitalWrite(GREEN_LED_PIN, LOW);
digitalWrite(RED_LED_PIN, HIGH);
delay(1000);
return true;
}
return false;
}
void setup() {
pinMode(SPEAKER_PIN, OUTPUT);
pinMode(RED_LED_PIN, OUTPUT);
pinMode(GREEN_LED_PIN, OUTPUT);
pinMode(ARM_PIN, INPUT);
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, LOW);
}
int detected = 0;
void loop() {
if(MOTION && armed) { //If there's motion and it's armed, sound the alarm
alarm();
delay(1000);
} else if(armed) { //if it's armed, but there's no motion, show a green LED
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, HIGH);
} else { //if it's not armed, show a red LED
digitalWrite(GREEN_LED_PIN, LOW);
digitalWrite(RED_LED_PIN, HIGH);
}
//check to see if the "ARM" button has been pressed
if(digitalRead(ARM_PIN) == HIGH && !armed) {
arm();
}
//check for a disarm
checkForDisarm();
}
Arduino Motion Detector
This is an Arduino-based Motion Detector I created. Upon pressing the button, it will arm 10 seconds later. Then beep and blink the LEDs when motion is detected. It can be disarmed by pressing the same button. The code is as follows:
/*****
* By Pete Lamonica
* Released under a Creative Commons Non-Commerical/Attribution/Share-Alike
* license
* creativecommons.org/licenses/by-nc-sa/2.0/
****/
#define MOTION_PIN 0
#define SPEAKER_PIN 9
#define RED_LED_PIN 2
#define GREEN_LED_PIN 3
#define ARM_PIN 4
#define SECONDS_TO_ARM 10
//defines what "motion" is. There's a pull-up resistor on the
// motion sensor, so "high" is motion, while "low" is no motion.
// I allowed some fuzziness on the "motion"
#define MOTION (analogRead(MOTION_PIN)=1000)
//Plays a tone of a given pitch
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(SPEAKER_PIN, HIGH);
delayMicroseconds(tone);
digitalWrite(SPEAKER_PIN, LOW);
delayMicroseconds(tone);
}
}
//will beep for about 3 seconds and check for disarm in the meantime.
//Could arrange a hardware interrupt to do the same thing
void alarm() {
for(int i=0; i<3; i++) {
if(checkForDisarm()) return;
digitalWrite(RED_LED_PIN, HIGH);
playTone(1432, 300); //F
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, HIGH);
if(checkForDisarm()) return;
playTone(1915, 300); //C
if(checkForDisarm()) return;
digitalWrite(GREEN_LED_PIN, LOW);
delay(400);
if(checkForDisarm()) return;
}
}
boolean armed = false; //armed status
//arm the device.
void arm() {
armed = true;
for(int i=0; i<SECONDS_TO_ARM/2; i++) {
digitalWrite(RED_LED_PIN, LOW);
delay(1000);
if(checkForDisarm()) return;
digitalWrite(RED_LED_PIN, HIGH);
delay(1000);
if(checkForDisarm()) return;
}
}
//Check if the system should be disarmed and do so if that's the case.
boolean checkForDisarm() {
if(digitalRead(ARM_PIN) == HIGH && armed) {
armed = false;
digitalWrite(GREEN_LED_PIN, LOW);
digitalWrite(RED_LED_PIN, HIGH);
delay(1000);
return true;
}
return false;
}
void setup() {
pinMode(SPEAKER_PIN, OUTPUT);
pinMode(RED_LED_PIN, OUTPUT);
pinMode(GREEN_LED_PIN, OUTPUT);
pinMode(ARM_PIN, INPUT);
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, LOW);
}
int detected = 0;
void loop() {
if(MOTION && armed) { //If there's motion and it's armed, sound the alarm
alarm();
delay(1000);
} else if(armed) { //if it's armed, but there's no motion, show a green LED
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, HIGH);
} else { //if it's not armed, show a red LED
digitalWrite(GREEN_LED_PIN, LOW);
digitalWrite(RED_LED_PIN, HIGH);
}
//check to see if the "ARM" button has been pressed
if(digitalRead(ARM_PIN) == HIGH && !armed) {
arm();
}
//check for a disarm
checkForDisarm();
}