Burglar alarm(resettable by light). Suggestions welcome!

This is a project I thought up in my spare time for an upcoming science fair. It is a burglar alarm, ahic once set off, can be reset by shining a light on an ldr. It took a few tries to get the code right. I am still in gr8 so try not to judge me too harshly. Any and all suggestions and criticism are welome. by the way, in the code , there are a few unnecessary variables which i will remove. :slight_smile: :slight_smile: :slight_smile:

/*
 * This the code for a burglar alarm.give proximity sensor to 5. ldr to reset alarm to A0.buzzer and led to 9. set calb for ambient light.
 * Burglar alarm V2.0 by Zahran Sajid.
 */
int ldr = A0;
int d = 0;
int led = 2;
int sen = 5;
int buz = 9;
int a = 0;
int b = 0;
int calb = 60; 
int alm = 0;
int peep = 0;


void setup() {
  Serial.begin(9600);
  pinMode(ldr,INPUT);
  pinMode(led,OUTPUT);
  pinMode(sen,INPUT);
  pinMode(buz,OUTPUT);
  digitalWrite(buz,HIGH);
  delay(500);
  digitalWrite(buz,LOW);

}
void loop() {
  
  reset();
  d = digitalRead(sen);
  if(d == LOW){
    while(alm == 0){
    
    alarm();
    reset();
    if(alm == 1){
      goto bailout;
    }
   }
  } 
  bailout:
  peep = 0;
  alm = 0;
}

void alarm() {
   
 digitalWrite(buz,HIGH);
  delay(500);
  digitalWrite(buz,LOW);
  delay(500); 
  

}
void reset() {
  a = analogRead(ldr);
  b = map(a, 0,1024, 0,255);
  Serial.println(b);
  if(b < calb){
    alm = 1;
  }
  
}

Burglar_Alarm_V2.0.ino (956 Bytes)

Most of these can be byte as the values will never be more than 255, save some SRAM.
int ldr = A0;
int d = 0;
int led = 2;
int sen = 5;
int buz = 9;
int a = 0;
int b = 0;
int calb = 60;
int alm = 0;
int peep = 0;
Looks like a is the only one that needs to be int.

It looks like you've created a very nice project. These are just some general suggestions that you might consider either for this project or your future endeavors:

Use Tools > Auto Format on your code regularly, especially before posting to the forum. The automatic indentation will make your code easier to read and make it easier to spot any bugs.

Don't use unnecessary blank lines. It's fine to add some to break code into sections but you have random blank lines that make for more scrolling and harder to read code.

Avoid using goto. It is very rarely useful and can cause your code to be harder to follow. Your code:

   while(alm == 0){
   
    alarm();
    reset();
    if(alm == 1){
      goto bailout;
    }
   }
  }
  bailout:

would work just the same if you wrote it as:

   while(alm == 0){
   
    alarm();
    reset();
   }
  }

because once alm==1 the while loop will exit. However, it is sometimes useful to be able to exit a while loop this could be done without goto like this:

   while(alm == 0){
   
    alarm();
    reset();
    if(alm == 1){
      break;
    }
   }
  }

See https://www.arduino.cc/en/Reference/Break for more information.

Use descriptive variable names. You may be able to remember what d, a, and b are now but if you come back to your program months later you'll have forgotten and need to look through the code to see what they are. This becomes much more important as you write more complex programs.

Don't use "magic numbers" an example of a magic number in your code is the 1 and 0 values of alarm. They are just arbitrary numbers. Much better would be to use a descriptive variable name for alm, such as alarmActive and set it to true or false.

alarmActive = false;

is much easier to understand than:

alm = 1;

Use const on variables that should never change. This can be very useful for avoiding bugs. For example, imagine if you accidentally wrote something like:

ldr = analogRead(ldr);

but you really meant to write:

ldrValue = analogRead(ldr);

with your current code you will have a strange bug that's hard to find. If you make ldr const then the first code would cause your program to not compile and you'll have a helpful error message that tells you exactly what the problem is. And don't assume that you'd never make a dumb mistake like that, it happens even to the pros. The goal is not to always write perfect code on the first attempt because that's impossible, but to write code in a way that makes finding and fixing the inevitable bugs as easy as possible. See https://www.arduino.cc/en/Reference/Const for more information.

Avoid using delay. You will need to hold the light on the LDR for at least a second while the alarm is going because the LDR is not being read for the 1000ms that alarm() takes to run. That's not a very big problem in your application but in other situations, such as trying to detect the press of a button that will only be held for <100ms, it is. So best to always avoid blocking code since you may want to add other features to your program later that need it to run fast. Once you get the hang of writing non-blocking code it's just as easy as using delay. It's a lot of work to go back and convert blocking code to non-blocking code. Check out File > Examples > 02.Digital > BlinkWithoutDelay for an example of how to do this.

Thanks a lot for all the feedback. I will try my best to improve based on your suggestions.I will improve the code and use it in my upcoming project.

Re: I updated the code to work with a ultrasound sensor instead of the IR proximity sensors. Used the NewPing library.

Re:Made it Non-Blocking!!!!!!!!!!!!!!!!!. Thanks Pert!!