Sensitivity issue with laser security system

Hello everyone, i have a problem with the sensitivity of an LDR in a laser security system.
The main aim of my setup is to provide some extra security to a cupboard door with a servo, laser, LDR, a bluetooth module, and LCD.
There aren't any errors in the IDE, but in the program, the ldr is ver sensitive and even though the laser is shining on the LDR the whole time, the program still reports a small instance of the interruption. I think it is because of the initial analog value of the LDR.
Here is my code-

#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
Servo myservo;

SoftwareSerial MyBlue(3, 2); // RX | TX
char relay = ' ';
const int rs = 13, en = 12, d4 = 11, d5 = 10, d6 = 9, d7 = 8;
bool arm = false;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup()
{ myservo.attach(4);
  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);
  MyBlue.begin(9600);
  lcd.begin(16, 2);
  lcd.print("Good day!");
  lcd.setCursor(0, 1);
  lcd.print("No Entry.");
}
void loop()
{ 
  delay(500);
  relay = MyBlue.read();
  flip(relay);
  int value = analogRead(A0);
  
    

  if (value < 50 && arm == true) {
    delay(30);
    MyBlue.write('x');
    digitalWrite(5, HIGH);
    lcd.clear();
    lcd.print("INTRUDER");
    delay(5000);
    lcd.setCursor(0,1);
    lcd.print("Reset prog.");
    arm = false;
    relay = '1';
    flip(relay);
  
  
  }
}
void flip(char yee){
if (yee == '0') {            // Checks whether value of relay is equal to 1
      digitalWrite(7, HIGH);
      arm = true;
    }
    else if (yee == '1')             // Checks whether value of relay is equal to 1
    { digitalWrite(7, LOW);
      arm = false;
    }
    else if (yee == '2') {
      myservo.write(90);
      lcd.clear();
      lcd.write("Please Enter.");
    }
    else if (yee == '3') {            // Checks whether value of relay is equal to 1
      myservo.write(0);
      lcd.print("Good day!");
      lcd.setCursor(0, 1);
      lcd.print("No Entry.");
    }
}

Also, i have a problem with me wanting to keep the function going after the intruder message is displayed.
Is it possible to fix that, or should I just press the reset button on the arduino every time an intruder is detected??
Me is a newbie so PLEEEEEZ help me :pleading_face:

don't understand the purpose of the delays when you trigger. it seems like you trigger an alarm ("INTRUDER") for just 5 seconds. should there be a command sent over the serial I/F as well and should there be some command to reset the alarm?

re: sensitivity

maybe better to recognize a detection only after the level is below some threshold several times or over some period of times. rather than sampling once every 1/2 second

leaky integration is one approach averaging

    avg += (samp - avg) * K;     // K < 1.0

re: blocking
is there a need for any type of delay or timer?   A timer could be used to check for the passing of some time without "blocking" the execution of other code

Can you please elaborate?
I didn't get the way you did the averaging.

Hi,

 digitalWrite(5, HIGH);

Where do you declare pin 5 as an output.

Hint: Use variable names for your pins, that way your code will red easier.

For example instead of

 int value = analogRead(A0);

Put before your setup.

int LDRPinA0 = A0;

then change your analogRead to:

 int value = analogRead(LDRPinA0);

The same for pins 6 and 7, what do they do, what output do they accomplish?
Also mysterious pin 5.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Oof, I'll fix that
It does look kind of easier with variables....
This, i think is an older version of the code, and removed pin 6 later.
Pin 5 is an active buzzer and pin 7 is a laser.
But, i am still struggling with the sensitivity.
Please show and explain the code here because i am doing this for door security.:money_mouth_face:

const byte PinInp = A1;

const float K = 1.0 / 8;
float avg;

int   n;

// -----------------------------------------------------------------------------
void
loop (void)
{
    int samp = analogRead (PinInp);

    avg += (samp - avg) * K;

    Serial.print   (n++);
    Serial.print   (" ");
    Serial.print   (samp);
    Serial.print   (" ");
    Serial.println (avg);

    delay (100);
}

// -----------------------------------------------------------------------------
void
setup (void)
{
    Serial.begin (9600);
}

samp in red, avg in cyan

Thanks, I get it now.
As i said, I'm a noob at this.

What's in the cupboard?

My raspberry pi and arduino sensors, i have to protect them from my little brother.

OK, I got the averaging code, but is there a way to make the script of the bluetooth still run??

post your latest code with the problem

OK, so the problem has changed. Now, the function(the one below) is being executed after the arm message and everything else is working fine. But now, there is a weird problem with it that the LCD shows some nonsensical values, like chars with accents when I tell it to move the servo and when it is armed and getting good values from the LDR. Here is my code.

#include <Servo.h>
#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
Servo myservo;
int value;
int xyc;
float avg;
const float K = 1.0 / 8;
int   n;

int samp = value;

SoftwareSerial MyBlue(3, 2); // RX | TX
char relay = ' ';
const int rs = 13, en = 12, d4 = 11, d5 = 10, d6 = 9, d7 = 8;
bool arm = false;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup()
{ Serial.begin(74880);
  myservo.attach(4);
  pinMode(5, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);
  MyBlue.begin(9600);
  lcd.begin(16, 2);
  lcd.print("Good day!");
  lcd.setCursor(0, 1);
  lcd.print("No Entry.");
}
void loop()
{ Serial.println(avg);
  relay = MyBlue.read();
  flip(relay, xyc);

  avg += (samp - avg) * K;
  value = analogRead(A0);
  Serial.println(relay);
  if (value < 30 && arm == true) {

    MyBlue.write('x');
    digitalWrite(5, HIGH);
    lcd.clear();
    lcd.print("IIINTRUDER");
    xyc = + 1;
    delay(5000);

    arm = false;
    relay = '0';
    flip(relay, xyc);


  }
  if (value > 30) {

    lcd.write("Armed.");
    lcd.setCursor(0, 1);
    lcd.write("Times stolen-");
    lcd.print(xyc);
    // Checks whether value of relay is equal to 1
    digitalWrite(7, HIGH);
    arm = true;
    flip(relay, xyc);
  }


}

void flip(char yee, int xyx) {
  value = analogRead(A0);


  if (yee == '0') {
    lcd.clear();
    lcd.write("Armed.");
    lcd.setCursor(0, 1);
    lcd.write("Times stolen-");
    lcd.print(xyx);
    // Checks whether value of relay is equal to 1
    digitalWrite(7, HIGH);
    arm = true;
  }
  else if (yee == '1')
    // Checks whether value of relay is equal to 1
  { lcd.clear();
    lcd.write("Disarmed.");

    digitalWrite(7, LOW);
    arm = false;
  }
  else if (yee == '2') {
    myservo.write(90);
    lcd.clear();
    lcd.write("Please Enter.");
  }
  else if (yee == '3') {            // Checks whether value of relay is equal to 1
    myservo.write(0);
    if (arm == true) {
      lcd.write("Armed.");
      lcd.setCursor(0, 1);
      lcd.write("Times stolen-", xyx);
    }
    else {
      lcd.clear();
      lcd.write("Disarmed.");
    }
  }
}

I think my code is fine, but if you have any suggestions, please feel free to let me know.

Hi,
A circuit diagram would help at this stage to see how your hardware is connected.

How are you powering your project?
Can you post link to specs/data of the servo?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

OK, it is a normal hobby servo. SG90, to be specific.
Here is the datasheet Servo Motor Micro SG90 - ProtoSupplies
I will send a fritzing diagram as soon as I can.
I am powering it with a USB port on a 21 in desktop.
I have-
A normal 650 nm laser diode
A hobby servo (SG90)
An LDR (hooked up to A0)
An LCD
An HC-05
And an active buzzer.
And after looking at the datasheet of the servo, I know now that I messed up here.

Hi,

Please no Fritzy.

Have you got the LDR setup in a potential divider network?

An image of a hand draw schematic will be fine, include ALL power supplies, component names and pin labels.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Hey, nice diagram.

You've already noticed the one potential problem, supplying the power for the servo.

So maybe think about powering the whole thing with an appropriately powerful 5 volt power supply or wall wart instead of through the USB and off the 5 volt output of the Arduino.

When are you going to add something like a pungent electric shock output for troublemakers? :wink:

FWIW I was the little brother, so here's me sticking my tongue out and never apologizing for reverse engineering your train set into a pile of worthless components.

a7

For the record, I scribbled that whole thing in like 15 minutes. I was doing my homework.

I'll get back to you with a spray of water from a 12v pump on 24v

Yes, I will take the juice from a power bank or, since its stationary, maybe a wall adaptor. Before the first post on the forum, I cranked out this whole project on a bleak and dusty Sunday, wiring and all.

So, wall wart it is.