Laser Alarm System

Hi! I am making a laser-alarm system using arduino (How to make a laser security system with Arduino (Easy Tutorial, incl. Sketch Code) - YouTube, I included the link so that you can view the source of my project.) I did exactly what is on this video and I got my project working. But my professor asked me to upgrade this project by adding 2 more lasers, and because of that, I need to add 2 more LRDs too. And my problem is I don’t know what am I going to change in the code for me to add 2 LDRs. Thanks in advance for your help. :slight_smile: Here is the code for the project:

const int triggeredLED = 7;
const int triggeredLED2 = 8;
const int RedLED = 4;        
const int GreenLED = 5;      
const int inputPin = A0;   //this is where I put the first LDR and I don't know how to add 2 more LDR  
const int speakerPin = 12;  
const int armButton = 6;     

boolean isArmed = true;      
boolean isTriggered = false;
int buttonVal = 0;           
int prev_buttonVal = 0;     
int reading = 0;             
int threshold = 0;           


const int lowrange = 2000;   
const int highrange = 4000; 

void setup(){
  
  pinMode(triggeredLED, OUTPUT);
  pinMode(triggeredLED2, OUTPUT);
  pinMode(RedLED, OUTPUT);
  pinMode(GreenLED, OUTPUT);
  pinMode(armButton, INPUT);
  digitalWrite(triggeredLED, HIGH);
  delay(500);
  digitalWrite(triggeredLED, LOW);  

   calibrate();
   setArmedState();  
}

void loop(){

 
  reading = analogRead(inputPin);

 
  int buttonVal = digitalRead(armButton);
  if ((buttonVal == HIGH) && (prev_buttonVal == LOW)){
    setArmedState();
    delay(500);
  }

  if ((isArmed) && (reading < threshold)){
    isTriggered = true;}

  if (isTriggered){

     for (int i = lowrange; i <= highrange; i++)
    {
      tone (speakerPin, i, 250);
    }
   
    for (int i = highrange; i >= lowrange; i--)
    {
      tone (speakerPin, i, 250);
    }

 
    digitalWrite(triggeredLED, HIGH);
    delay(50);
    digitalWrite(triggeredLED, LOW);
    delay (50);
    digitalWrite(triggeredLED2, HIGH);
    delay (50);
    digitalWrite(triggeredLED2, LOW);
    delay (50);
    }

  delay(20);
}

void setArmedState(){

  if (isArmed){
    digitalWrite(GreenLED, HIGH);
    digitalWrite(RedLED, LOW);
    isTriggered = false;
    isArmed = false;
  } else {
    digitalWrite(GreenLED, LOW);
    digitalWrite(RedLED, HIGH);
    tone(speakerPin, 220, 125);
    delay(200);
    tone(speakerPin, 196, 250);
    isArmed = true;
  } 
}

void calibrate(){

  int sample = 0;             
  int baseline = 0;            
  const int min_diff = 200; 
  const int sensitivity = 50;
  int success_count = 0;
  
  digitalWrite(RedLED, LOW);
  digitalWrite(GreenLED, LOW);

  for (int i=0; i<10; i++){
    sample += analogRead(inputPin);
    digitalWrite(GreenLED, HIGH);
    delay (50);
    digitalWrite(GreenLED, LOW);
    delay (50); 
  }

  do
  {
    sample = analogRead(inputPin);    

    if (sample > baseline + min_diff){
      success_count++;
      threshold += sample;

      digitalWrite(GreenLED, HIGH);
      delay (100);                    
      digitalWrite(GreenLED, LOW);
      delay (100);                    
    } else {
      success_count = 0;             
      threshold = 0;
    }

  } while (success_count < 3);

  threshold = (threshold/3) - sensitivity;

  tone(speakerPin, 196, 250);
  delay(200);
  tone(speakerPin, 220, 125);
}

Unfortunately your code has no useful comments and the variable names aren't helpful so I can't easily see what it is currently doing E.g. there is no mention of lasers anywhere that I can see.

And there is the question of what 2 extra lasers and 2 extra LDRs are supposed to do? Different alarms? The current alarm goes off if ANY beam is interrupted? Extra LEDs to tell you WHICH beam was interrupted? The alarm goes off if more than one beam is interrupted?

Steve

@wilbertAaron, do not cross-post. Other thread removed.

Hi,
I think this is where your professor gets you to do what he originally wanted you to do.
Write your own code.
Do you understand any of the code you have cut and pasted?

Do you know what A0 is and how the code uses it?
Have you tried to make your own code, just to read the LDR?
What is the name of this subject that requires you to program a microcontroller?

Tom.... :slight_smile:

Your problem if obvious you copied instead of create, that way you do not learn and this is not the result, in the future your lively hood could be dependent on this type of action. Your prof has many other students do this over the years, he is not a dummy. It is possible I am him and answering this but you will never really for sure know. Start by writing a new program, discard all the code you now have. TomGeorge is correct! When you get it working it will be patently obvious how to make the additions. This response is to help you get started in solving your problem, not solve it for you.
Good Luck & Have Fun!
Gil