For our project, we needed to get two sensors. A motion
detector (PIR sensor) and a break beam sensor. The PIR sensor detects motion in
a cone and detects levels of radiation. It detects motion by comparing one
frame with the next to see if there are changes. We got a Parallax 90 degrees
PIR sensor from Amazon and it took about a week to ship. There were not a lot
of mishaps, except that we realized the PIR sensor may have too wide of an
angle than we wanted, but we figured we'd be able to modify it later on with
our case.
We found simple codes online to test the functionality and for our
final code, we just combined the ones we needed. I took CSE 142 before, so I
modified a bit of the code so that it would work the way we wanted to. Originally, I wanted a fancy melody as the alarm, but the code got too complicated and we had to settle for a simple tone.
Below is our final code:
//breakbeam
#define LEDPIN 13
// Pin 13: Arduino has an LED connected on pin 13
// Pin 11: Teensy 2.0 has the LED on pin 11
// Pin 6: Teensy++ 2.0 has the LED on pin 6
// Pin 13: Teensy 3.0 has the LED on pin 13
#define SENSORPIN 4
// variables will change:
int sensorState = 0, lastState=0; // variable for reading the pushbutton status
//motion detector
int ledPin = 13; // choose the pin for the LED
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 pinSpeaker = 10; //Set up a speaker on a PWM pin (digital 9, 10, or 11)
int initialTime = 0;
int endTime = 0;
void setup() {
//breakbeam
// initialize the LED pin as an output:
pinMode(LEDPIN, OUTPUT);
// initialize the sensor pin as an input:
pinMode(SENSORPIN, INPUT);
digitalWrite(SENSORPIN, HIGH); // turn on the pullup
//motion detector
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
pinMode(pinSpeaker, OUTPUT);
Serial.begin(9600);
}
void loop(){
// read the state of the pushbutton value:
sensorState = digitalRead(SENSORPIN);
// check if the sensor beam is broken
// if it is, the sensorState is LOW:
if (sensorState == LOW) {
// turn LED on:
digitalWrite(LEDPIN, HIGH);
//begin motion detection
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
playTone(0, 0);
initialTime = millis();
} else {
endTime = millis();
digitalWrite(ledPin, LOW); // turn LED OFF
if ((endTime - initialTime) >= 5000) {
while (val == LOW){
val = (digitalRead(inputPin));
playTone(3, 160);
}
initialTime = millis();
}
}
}
else {
// turn LED off:
digitalWrite(LEDPIN, LOW);
}
}
// duration in mSecs, frequency in hertz
void playTone(long duration, int freq) {
duration *= 1000;
int period = (1.0 / freq) * 1000000;
long elapsed_time = 0;
while (elapsed_time < duration) {
digitalWrite(pinSpeaker,HIGH);
delayMicroseconds(period / 2);
digitalWrite(pinSpeaker, LOW);
delayMicroseconds(period / 2);
elapsed_time += (period);
}
}
No comments:
Post a Comment