Introduction: Arduino Laser System-Tripwire Tutorial

I made it using an Arduino, a laser diode, a photocell (light dependent resistor) and a piezo buzzer.



Code :


const int PHOTOCELL_PIN = A0;
const int BUZZER_PIN = 3;
// voltage readings are in 0-1023 range
const int THRESHOLD = 500;

void setup() {
  pinMode(PHOTOCELL_PIN, INPUT);
  Serial.begin(9600);
}

long alarmEndTime = 0;

void loop() {
  int level = analogRead(PHOTOCELL_PIN);
  Serial.println(level);
  long time = millis();
  if (time < alarmEndTime) {
    long timeLeft = alarmEndTime - time;
    if (timeLeft % 1000 > 300) {
      tone(BUZZER_PIN, 4000);
    } else {
      noTone(BUZZER_PIN);
    }
  } else {
    noTone(BUZZER_PIN);
    if (level < THRESHOLD) {
      alarmEndTime = time + 3000;
    }
  }
}