This is an old revision of the document!
A simple device implemented using an Arduino which plays a sound whenever a person enters a room. The device, which is to be positioned at the entrance of a room, utilizes infrared sensors to do this. The device only plays a sound when it detects a person entering.
The device uses 2 infrared sensors and an infrared emitter to detect movement. The sensors detect the infrared reflection reflecting off an object passing in front of them. The device is able to tell the direction of movement of the object depending on which infrared sensor gets triggered first. A short sound will be played by the use of an active buzzer only when detecting an entry.
Notes:
const int buzzer=5; //pin 5 is the buzzer output const int ir_led=4; //pin 4 is the ir_led output const int push_button=3; //pin 3 is the push_button input volatile byte alarm = LOW; //alarm state = on or off int noteArray[] = {740, 587}; void setup(){ Serial.begin(9600); pinMode(buzzer,OUTPUT); //configure pin 5 as OUTPUT pinMode(ir_led, OUTPUT); //configure pin 4 as OUTPUT //pinMode(push_button,INPUT); //configure pin 3 as INPUT attachInterrupt(digitalPinToInterrupt(push_button), on_off, RISING); } void loop(){ if (alarm){ int sensor0=analogRead(A0); int sensor1=analogRead(A1); Serial.println((String)"sensor0 = " + sensor0); Serial.println((String)"sensor1 = " + sensor1); if(sensor0 < 1000){ buzzer_sound(true); } else if (sensor1 < 1000) { buzzer_sound(false); } } } void on_off(){ static unsigned long last_interrupt_time = 0; unsigned long interrupt_time = millis(); // if interrupts come faster than 500ms, assume it's a bounce and ignore if (interrupt_time - last_interrupt_time > 500){ alarm = !alarm; //digitalWrite(ir_led, alarm); tone(ir_led, 38000); } last_interrupt_time = interrupt_time; } void buzzer_sound(bool in_out){ noTone(ir_led); delay(100); if(in_out){ for(int i = 0; i < 2; i++){ tone(buzzer, noteArray[i], 750); delay(500); } } else { for(int i = 1; i > -1; i--){ tone(buzzer, noteArray[i], 750); delay(500); } } noTone(buzzer); tone(ir_led, 38000); delay(2500); }
Working with IR technology was more complicated than anticipated. There's a lot of things that needs to be accounted for when choosing the proper IR component for your project.
If I were to work on another Arduino project in the future, I will definitely be doing more research into the needed components beforehand and try to minimize improvisation.