Snack theft detector
Has it ever happened to you, that someone stole your favorite
snack or chocolate?
I have a solution for you!
What you will need:
- arduino
- a breadboard
- an infrared obstacle avoidance sensor
- a passive buzzer
- some wires
- some LEDs
Code:
int led1 = 2; // First LED to D2
int led2 = 3; // Second LED to D3
int infraredPin = 4; // IR sensor to D4
int buzzerPin = 5; // Passive buzzer to D5
int objectDetection;
void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(infraredPin, INPUT);
pinMode(buzzerPin, OUTPUT);
}
void loop() {
objectDetection = digitalRead(infraredPin); //We save the value of the IR obstacle avoidance sensor in this variable. It can be only 0 or 1.
// When the sensor detects an object the arduino plays a melody and turns on the LEDs.
if (objectDetection == 0){
tone(buzzerPin, 1000, 100);
digitalWrite(led1, 1);
digitalWrite(led2, 0);
delay(50);
tone(buzzerPin, 970, 100);
digitalWrite(led1, 0);
digitalWrite(led2, 1);
delay(50);
}
else{
noTone(buzzerPin);
digitalWrite(led1, 0);
digitalWrite(led2, 0);
}
}
Just upload the code to your arduino, and put the sensor under your snack!
