Laser Security : Quick object detection issue

Hello to All,

I have code for Light Controller/Saver using LDR/Photo diode which is working perfectly.
Planned to implement same code for Laser Security System, is responding as supposed to be.But What I have observed
if Object crosses Laser & LDR/Photo Diode Quickly.

System failed to detect those quick/short pulse from object.Please help me.

Blink without delay code was also tried but not fruitful

Following code is being used:

int relay_Pin = 5;        
int ldr_Pin = A0;   
int lightReading = 0; 
        
void setup () {
  
  pinMode(relay_Pin, OUTPUT);
  Serial.begin(9600);
  
}

void loop () {
  
  lightReading = analogRead(ldr_Pin);
  
    
  if (lightReading < 800)     
  {
    digitalWrite(relay_Pin, HIGH);  
    delay(1200);               
    digitalWrite(relay_Pin, LOW);    
    delay(1000);
    } 
  else
  {
    digitalWrite(relay_Pin, LOW); 
    delay(1000);
    
  }
  
}

delay() means do absolutely nothing, especially not "detect sensor activity".

Study this tutorial to learn how to get rid of all those delay statements: https://www.baldengineer.com/blink-without-delay-explained.html

Sir, that was too used ...issue is detecting fast/quick passing object

The code you posted can't possibly work, because it spends 99.999% of its time doing a useless delay(), instead of reading the sensor.

Post your best attempt at fixing the problem.

LDR or photodiode?

int relay_Pin = 5;

int ldr_Pin = A0;    


unsigned long waitDuration = 60000;           
unsigned long minReading = 40;                



int lightReading1 = 0;

unsigned long prev1 = 0,curr = 0;
bool on1 = 0;

void setup () {
  
  pinMode(relay_Pin, OUTPUT);
  Serial.begin(9600);
}

void loop () {
  
  
  lightReading1 = analogRead(ldr_Pin);
  
  if(lightReading1 < minReading){
    if(!on1){
      digitalWrite(relay_Pin, HIGH);
      prev1 = millis();
      on1 = 1;
      delay(1000);
    }
  } 
  curr = millis();
  if(on1 && curr - prev1 >= waitDuration){
    on1 = 0;
    prev1 = 0;
    digitalWrite(relay_Pin, LOW); 
    delay(1000);
  }
  
}

This code work better than what all I have tried so far,even it fails for quick object

LDR's are very slow. Photodiodes are fast. It matters which you are using.

Infact I started with LDR but it seems lazy nature . So I used photo diode with changes in hardware circuitary.

Sir, very true thats why now I have been using Photo diode

Please post a hand drawn, clearly labeled circuit diagram, with component values. If you are using sensor modules, post the links.

Why are you still using delay()?

Ok Sir, I will remove delay ,shall test on hardware circuit soon .

Not sure what your requirements are.

If you use a Photo Transistor, they can easily respond to an object that moves by in 5ms.

You would use a regular digital pin to monitor for these detections.

Example:
You can buy a 3DU5C Silicon Phototransistor for 10/$5.00

Thanks,I will try as you suggrested.

Typical sketch:

#define ENABLED                     true
#define DISABLED                    false

#define relayON                     HIGH
#define relayOFF                    LOW


const byte heartbeatLED           = 13;
const byte relay                  = 5;
const byte photoTransistor        = 2;         //set up as an emitter follower

bool Flag                         = DISABLED;

byte lastPhotoTransistorState;

unsigned long heartbeatMillis;
unsigned long switchMillis;
unsigned long relayMillis;
unsigned long onMillis;

unsigned long onInterval          = 3000ul;      //3 seconds


//********************************************^************************************************
void setup ()
{
  Serial.begin(9600);

  pinMode(heartbeatLED, OUTPUT);
  pinMode(relay, OUTPUT);

} //END of   setup()


//********************************************^************************************************
void loop ()
{
  //*********************************                         heartbeat TIMER
  //is it time to toggle the heartbeatLED (every 500ms)?
  if (millis() - heartbeatMillis >= 500ul)
  {
    //restart this TIMER
    heartbeatMillis = millis();

    //toggle the heartbeatLED
    digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
  }

  //*********************************                         checkSwitches TIMER
  //is time to check our switches (every 2ms) ?
  if (millis() - switchMillis >= 2)
  {
    //restart this TIMER
    switchMillis = millis();

    checkSwitches();
  }

  //*********************************                         relay TIMER
  //if the relay is ON is it time to turn it OFF ?
  if (Flag == ENABLED && millis() - relayMillis >= onInterval)
  {
    //disable this TIMER
    Flag = DISABLED;
    
    digitalWrite(relay, relayOFF);
  }

  //*********************************
  //Other non blocking code goes here
  //*********************************


} //END of   loop()


//********************************************^************************************************
void checkSwitches()
{
  byte currentState;

  //*********************************                         photoTransistor
  currentState = digitalRead(photoTransistor);

  //did the sensor change state ?
  if (lastPhotoTransistorState != currentState)
  {
    //update to the new state
    lastPhotoTransistorState = currentState;

    //if we are not timing, is the transistor turned OFF (emitter follower) ?
    if (Flag == DISABLED && currentState == LOW)
    {
      //enable the relay TIMER
      Flag = ENABLED;

      //reset this TIMER
      relayMillis = millis();

      digitalWrite(relay, relayON);
    }

  } //END of this switch


  //*********************************

} //END of checkSwitches()



1 Like

Thank You Sir, I shall try it.

Once again ,Many thanks...working smoothly in my project....God bless you greatly !!!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.