A simple sketch that can but used to sound an alarm when a laser beam is interrupted.
Hardware used:
- Button
- Light Sensor
- Piezo
Code Used:
- analogRead
- digitalWrite
- If Statement
/* Diyode CodeShield Constants */
#define ENCODER_A 14
#define ENCODER_B 15
#define ENCODER_PORT PINC
#define SWITCH 13
#define BUTTON 12
#define RGB_RED 11
#define RGB_GREEN 10
#define RGB_BLUE 9
#define LED 6
#define SERVO 5
#define PIEZO 3
#define RELAY 2
#define POT 2
#define HALL 3
#define THERMISTOR 4
#define PHOTOCELL 5
int lowL = -1;
int highL = -1;
int cutoff = 0;
int mode = 0;
void setup() {
pinMode(RELAY, OUTPUT);
pinMode(PIEZO, OUTPUT);
}
void loop() {
if (mode) { // highLevel and lowLevel have been set
if (analogRead(PHOTOCELL) < cutoff) {
startAlarm();
}
} else {
if (digitalRead(BUTTON)) {
configure();
}
}
delay(40);
}
void startAlarm() {
analogWrite(PIEZO, 128);
delay(100);
digitalWrite(PIEZO,LOW);
delay(100);
}
void configure() {
if (lowL == -1) {
lowL = analogRead(PHOTOCELL);
} else if (highL == -1) {
highL = analogRead(PHOTOCELL);
cutoff = ((lowL+highL)/2);
mode = 1;
}
analogWrite(PIEZO, 128);
delay(400);
digitalWrite(PIEZO,LOW);
delay(1000);
}