Need help with IR detectors

I’m having trouble with a project I’m trying to do. I want to have a IR detector connected to arduino, and a separate IR emitter attached to a different battery. When the detector detects the emitter moving away from the emitter, a Peizo will start blaring a klaxon like noise. (the emitter will be attached to a door, and the arduino+IRdetector will be attached to the door frame. When the door moves, the Piezo will make a noise, like a door alarm.)
Whenever I try to do this, The detector only detects once (one number in the Serial Port) and the Piezo blares. I want the detector to keep detecting continuously, not just once. Here’s the code. I don’t know what’s wrong: [a lot of the code is copied from various websites. I’m a beginner]

int speakerPin = 9; //this sets the pin tha the piezo element should use.

int length = 45; //this shows the length of the notes being played
char notes ="Cg "; //note markers so that when it is run in a for() loop it can catch
//the wanted note
int beats = { 3,2 }; //how many times a note is played
int tempo = 300; //how long it is played

int IRsensor = 16;
int dist = 0;

void playTone(int tone, int duration) { //this function plays the note according to the tone needed
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH); //this makes the tone
delayMicroseconds(tone); //this sets how long the tone will last
digitalWrite(speakerPin, LOW); //this sets the ton eoff
delayMicroseconds(tone);
}
}

void playNote(char note, int duration) { //this funciton confirms which tone is needed
char names = { ‘C’, ‘g’ }; //the list of possible tones
int tones = { 950, 1350 }; //list of tone frequencies
// for each tone

for (int i = 0; i < 8; i++) { //this statement tries to find the tone you want
if (names == note) { //if a tone matches then it would send the tone that the

  • //piezo can make to another function to run it*
    _ playTone(tones*, duration);_
    _
    }_
    _
    }_
    _
    }_
    void setup() { //this gives a pin power
    _
    pinMode(speakerPin, OUTPUT);_
    _
    pinMode (IRsensor, INPUT);_
    _
    Serial.begin(9600);_
    _
    }_
    void loop() { //this is where the program runs
    _
    int dist = analogRead(16);_
    _
    Serial.println(dist);_
    _
    if (dist > 980) {_
    _
    for (int i = 0; i < length; i++) { //this statement tries to find whether a note is a tone or a rest*_
    _ if (notes == ’ ') { //if rest it would give a rest
    delay(beats * tempo);
    * } else { //if tone it would see which tone*
    playNote(notes, beats * tempo);
    * }
    delay(tempo / 300);
    }
    }
    }*_

delay(tempo / 300);

are you sure its getting stuck, or is it just holding the last note for 300ms and by the time it loops back around it hits the last note and delay again?

(delay is the devil)

I edited a bit, all at the end. I just added a while loop with a ridiculous expression, because i was trying to do a normal loop, but an error keeps coming up. I also think i changed some of the circuit because now i am getting numbers in the 30s - 60s instead of 900s, so i changed the if statement to >50. Anyway, I took away the delay statement, but now it works, until it senses above 50. then it makes 1 loop of the sound i want it to make (a series of two noises), then sits there for about a minute, then makes a really high-pitched noise for about another minute, then starts again. I am not sure why this is happening. Here is the updated code:

int speakerPin = 9; //this sets the pin tha the piezo element should use.

int length = 45; //this shows the length of the notes being played
char notes ="Cg "; //note markers so that when it is run in a for() loop it can catch
//the wanted note
int beats = { 3,2 }; //how many times a note is played
int tempo = 300; //how long it is played

int IRsensor = 16;
int dist = 0;

void playTone(int tone, int duration) { //this function plays the note according to the tone needed
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH); //this makes the tone
delayMicroseconds(tone); //this sets how long the tone will last
digitalWrite(speakerPin, LOW); //this sets the ton eoff
delayMicroseconds(tone);
}
}

void playNote(char note, int duration) { //this funciton confirms which tone is needed
char names = { ‘C’, ‘g’ }; //the list of possible tones
int tones = { 950, 1350 }; //list of tone frequencies
// for each tone

for (int i = 0; i < 8; i++) { //this statement tries to find the tone you want
if (names == note) { //if a tone matches then it would send the tone that the

  • //piezo can make to another function to run it*
    _ playTone(tones*, duration);_
    _
    }_
    _
    }_
    _
    }_
    void setup() { //this gives a pin power
    _
    pinMode(speakerPin, OUTPUT);_
    _
    pinMode (IRsensor, INPUT);_
    _
    Serial.begin(9600);_
    _
    }_
    void loop() { //this is where the program runs
    while (dist < 999999){
    _
    int dist = analogRead(16);_
    _
    Serial.println(dist);_
    _
    if (dist > 50) {_
    _
    for (int i = 0; i < length; i++) { //this statement tries to find whether a note is a tone or a rest*_
    _ if (notes == ’ ') { //if rest it would give a rest
    delay(beats * tempo);
    * } else { //if tone it would see which tone*
    playNote(notes, beats * tempo);
    * }
    }
    }
    }
    }*_

@ChickenWing93:
By “Piezo”, I think you mean “magnetic buzzer”. http://arduino.cc/forum/index.php/topic,107117.0.html.
Please use code tags for your code. There is a # button for code tags. Auto format your code.
I am currently creating a page in my wiki: http://arduino720.wikispaces.com/Infrared+Remotes

What IR sensor are you using, what emitter? Instead of just the IR LED, you should use a TV remote (IR sensors with 3 legs do not sense steady IR light). If you are using IR phototransistor, it’s fine.
I can just make the code up for you and the instructions, but then you won’t learn anything.
Tell me more info about the hardware.


Try using tone() function: http://arduino.cc/en/Reference/Tone. I will ask you more questions later (I don’t understand what the problem is).
Can you post your Serial printouts?

  1. If detector is receiving the IR signal, exactly what happens?
  2. If detector is NOT in range of emitter, exactly what happens?

phototransistors also dont just flip on and off digitally, they ramp up and there may be enough ambient light to keep it triggered full time

Thanks, dkl65, with the tone(), I changed this big code into a short if statement. Here’s the new code:

int IRsensor = 16;
int IRled = 13;

int dist = 0;

void setup() {
  pinMode (IRsensor, INPUT);
  Serial.begin(9600);
  pinMode(IRled, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH);
  int dist = analogRead(16);
  Serial.println(dist);
  if (dist < 35) {
    tone(9, 150);
    delay(1000);
    noTone(9);
}
}

Now, whenever the IR emitter is away, it makes an annoying noise, and when it comes close again, it stops.
But now what I want to do is instead of a boring tone, I want to make a noise like an alarm, with different pitches instead of frequencies.

BTW, when I say IR emitter, I mean the object that looks like an LED except its tinted dark, and the IR detector is the same but is clear. They only have two legs.
This is my first time posting something. I am not sure how to post Serial printouts, but here are some copied and pasted:
(This is with the emitter close)

40
41
41
41
41
41
40
41
41
41
41
40
41
41
41
41
41
41
40
41
41
41
41
40
40
41
41

(With the emitter further away)

37
36
36
35
36
37
36
35
34
37
33
35
33

Sounds like you have an IR phototransistor. Why are you using the IR phototransistor on a digital pin? You don't need pinMode() on an analog pin.

ChickenWing93: But now what I want to do is instead of a boring tone, I want to make a noise like an alarm, with different pitches instead of frequencies.

Different frequencies make different tones. Higher frequency, higher pitch. Loudest for the magnetic buzzer is 2048Hz. What is the purpose of your project?

I want to make a sort of a makeshift alarm. When a door opens, an IR emitter that is attached to it moves away from the frame, which has the arduino and the phototransistor. When it moves away from the frame (the door is opened) an alarm blares. Just to make it more fun, I want to use a noise that sounds like an alarm, too. You know, those klaxon alarms in the movies or like a police siren. (This is a fun project, and I'm doing it because I'm bored).

Also, just as a side topic, if I wanted to make the arduino remember the date and time of every time the alarm went off (the door opened), what sort of hardware (and software if required) would I need for the easiest, cheapest and most beginner way of doing this? For example, if I needed an SD card, how would it work? Do I just plug it in to my computer and open it? Is there a specific program to open a document in this containing all dates? Do I need a separate shield or something? (this is an example).

Why are you using the IR phototransistor on a digital pin? You don't need pinMode() on an analog pin.

Because I don't really know how to use IR stuff, so I just tried whatever I could. A little into this, I just switched the IR pin from a digital to an analog to see if I got more reasonable results and not 1023s continuously, and I did, so I just kept everything the way it is.

Did you get the alarm sounding when the door is opened?

ChickenWing93: Also, just as a side topic, if I wanted to make the arduino remember the date and time of every time the alarm went off (the door opened), what sort of hardware (and software if required) would I need for the easiest, cheapest and most beginner way of doing this?

I think that your are gonna have to use something like this: http://www.ladyada.net/learn/breakoutplus/ds1307rtc.html. It will require soldering. If the Arduino is connected to the computer, it will be much easier. I don't know so much about SD cards. Iknow that there is a library to manipulate them and shields with SD card slots.

I can make a sound with the magnetic buzzer that sounds like a police siren.

Yes, i did get the alarm sounding. Its just a plain tone. I'll try finding out how to make a klaxon noise

Okay, so everything is working fine. You just want to find out how to make a siren noise? Well, that is very straight forward, so I will leave that to you. There are 2 types of siren I know.