arduino‎ > ‎

laser sensor and buzzer

int ldrPin = A4;  // Photo-resistor

bool wasshot = false;

long ldrValue1, ldrValue2;

void setup(void) {
   pinMode (ldrPin,INPUT);       // Photo-resistor
   pinMode (A0, OUTPUT);      // LED
   pinMode (A1, OUTPUT);      // LED
   pinMode(7, OUTPUT);         // Laser
   pinMode(4, INPUT);             // Button
   pinMode(10, OUTPUT);         //Buzzer
   digitalWrite(4, HIGH);            // Turn Button on


}
 
void loop(void) {

  ldrValue1 = analogRead(ldrPin);  
  delay(10);
  ldrValue2 = analogRead(ldrPin);  

  if (ldrValue1-ldrValue2 > 20||ldrValue2-ldrValue1 > 20){ //Target is hit
  wasshot = true;
  digitalWrite(A0,HIGH);
  digitalWrite(A1,LOW);
  delay(1000);
  }
  else{
    digitalWrite(A0,LOW);
    digitalWrite(A1,HIGH);
  }

if (wasshot){   // Disable Gun and Buzz
                // tone(pin, frequency, duration)
  digitalWrite(7,LOW);
  tone(10, 3000, 1000); //1st beep
  delay(1000);
  noTone(10);
  delay(1000);
  tone(10, 2000, 1000); //2nd beep
  delay(1000);
  noTone(10);
  delay(1000);
  tone(10, 1000, 1000); //3nd beep
  delay(1000);
  noTone(10);
  delay(1000);
  wasshot = false;
}else{
                                               // Shoot Laser
 if (digitalRead(4) == LOW){
   digitalWrite(7, HIGH);
 }else{
   digitalWrite(7, LOW);
 }
}
}