I build photo gate, to detect throwing ball. It works fine when delay time in main loop for max 40 milliseconds. Problems start when I delay every loop only for 20 milliseconds, now my LED which indicates if something broke path between IR led and IR receiver, blinks occasionally, if I set delay for 10 milliseconds and turn on, then LED is off for 2 seconds, then start blinking very fast and is ON for rest time, until I break IR path. I think some physics is going on here which I don't understand.
I also would like to ask if it is possible to narrow IR wave path to be more like laser (going strait into one direction) and how I can control sensitivity of IR detection
This is schematic enter image description here
Code:
//define pins. I used pins 4 and 5
#define irLedPin 4 // IR Led on this pin
#define irSensorPin 5 // IR sensor on this pin
#define buzzerPin 2 // buzzer on this pin
int irRead(int readPin, int triggerPin); //function prototype
void setup()
{
pinMode(irSensorPin, INPUT);
pinMode(irLedPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600);
// prints title with ending line break
Serial.println("Program Starting");
// wait for the long string to be sent
delay(100);
}
void loop()
{
bool isOn = irRead(irSensorPin, irLedPin);
//Serial.println(isOn); //display the results
if(isOn == true)
{
digitalWrite(buzzerPin, HIGH);
}
else
{
digitalWrite(buzzerPin, LOW);
}
delay(10);
}
int irRead(int readPin, int triggerPin)
{
int halfPeriod = 13; //one period at 38.5khZ is aproximately 26 microseconds
int cycles = 23; //26 microseconds * 23 is more or less 600 us
int i;
for (i=0; i <=cycles; i++)
{
digitalWrite(triggerPin, HIGH);
delayMicroseconds(halfPeriod);
digitalWrite(triggerPin, LOW);
delayMicroseconds(halfPeriod - 1); // - 1 to make up for digitaWrite overhead
}
return digitalRead(readPin);
}
-
You can get IR laser diodes if you want a beam.pjc50– pjc5011/05/2015 22:26:41Commented Nov 5, 2015 at 22:26
-
Or you can use red laser beam and use that as a IR source.Avamander– Avamander11/06/2015 07:54:04Commented Nov 6, 2015 at 7:54
-
@Avamander, wow that's strange, I just checked and it works, but how? my red laser has 570 nm and IR reciver 950nm and it works, how ?kosnkov– kosnkov11/06/2015 18:18:49Commented Nov 6, 2015 at 18:18
-
The laser has close enough wavelength, the same thing works with red LEDs too.Avamander– Avamander11/06/2015 18:37:45Commented Nov 6, 2015 at 18:37
1 Answer 1
While you are doing a delay()
you can't be doing anything else - that includes looking to see if the beam has been broken by the ball.
You must not use delay()
in your sketch, instead use millis()
as in the BlinkWithoutDelay example sketch in the IDE's File -> Examples menu.
Also how you are dealing with your IR system is incorrect. You are first generating a brief square wave and then afterwards you are looking to see what was received. Instead you need to be generating the square wave and at the same time see if that square wave is being received. Normally that is achieved by using one of the PWM outputs of the Arduino set to the right frequency to generate a constant waveform which the receiver then senses all the time.