Decision: If the cat is on the counter, play an alarm to scare him with a blinking LED.
Action: Execute script that plays the alarm and blinks the LED.
I started my experiment by finding a YouTube video that utilized Arduino to create an alarm that would sound in the event that light came into contact with the LDR.
However, this was the opposite of what I was hoping my project would do, as I imagined my cat would be covering up the LDR when he climbed onto the kitchen counter. I ended up making a very small change to the code by switching the variables so that the alarm would sound in the absence of light. I also added some text to the serial monitor as further indication that my cat was being bad.
//set pin numbers
const int ledPin = 13;
const int buzzerPin = 12;
const int ldrPin = A0;
void setup(){
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(ldrPin, INPUT);
}
void loop() {
int ldrStatus = analogRead(ldrPin); //read the state of LDR value
if (ldrStatus <= 400) {
tone(buzzerPin, 300);
digitalWrite(ledPin, HIGH);
delay(100);
noTone(buzzerPin);
digitalWrite(ledPin, LOW);
delay(100);
Serial.println("CAT IS ON THE COUNTER DANGER DANGER");
}
else {
noTone(buzzerPin);
digitalWrite(ledPin, LOW);
Serial.println("No cat detected");
}
}
No comments:
Post a Comment