Please notice: Working code for the Attiny85, with power saving to give a very long battery life, is in reply #18
At work I have a locker with extension cords, power supplies, plugs, connectors, network cables and a lot of other nice stuff needed to make the daily life function.
It is there for anybody in my part of the house to have, whenever they need it.
Unfortunately, that led to people from other parts of the house "coming shopping" when they needed something, and the locker was an awful mess.
In the end I had to remove the handle on the locker to limit the access. The handle is in my office now, and anyone needing it, can borrow it.
It has kept the locker nice and tidy for several weeks.
But this week I found one of our student halfway inside the open locker, and he insisted, that it wasn't locked when he came, and then he could just as well help himself to a couple of cables, and see what else was in the locker?
I'm certain that I locked it last time I was in the locker, meaning that someone has had an unauthorized access.
(Probably that very student, I know him quite well!)
So I made a simple little setup with an ATtiny, an LDR and an awfully loud (and very cheap) "window alarm".
(The alarms were cheaper than the batteries in them, so I bought a handful and used the batteries for other things. I simply soldered leads to the springs in the battery compartment for this setup)
Before you flame me for coding with a shovel:
This is an early version, primarily made to have "something working"
In later versions, I'll look into making the code more elegant, and take measures to save power.
- It beeps when switched on, to give ample time to close the locker
- then it calibrates to the light level in the locker, and gives a long beep to signal "armed"
If the light level is raised (the door is opened) - it gives some short beeps to warn me, waits for two seconds.... and if I have not switched it off by that time, fires the siren.
If the door is closed again, it will keep wailing for another 30 seconds.
/*
Cable storage locker alarm
ATTINY VERSION 2
Callibration is based on
http://arduino.cc/en/Tutorial/Calibration
David A Mellis and Tom Igoe
By Peter_I
2014
The circuit:
* LDR attached to analog input 1 and 5V with 10k pull down to GND
* Siren attached digital pin 0
* Piezo beeper attached to pin 1 (PWM)
(If used on Arduino, adjust pins accordingly)
Only sensorMax is interesting, and an "intrusion" is considered to be when
"sensorMax + treshold" is exceeded.
"Intrusion" results short beeps, 2 sec delay followed by the siren.
The short beeps will remind me to switch off the alarm when I open the locker.
*/
//The constants:
const int sensorPin = A1; // pin that the sensor is attached to
const int sirenPin = 0; // pin that the LED is attached to, 0 on ATTiny
const int beepPin = 1; // piezobeeper on PWM pin
const int treshold = 100; // treshold value
const int beepLength = 1000; // A long beep, max 1500ms
const int shortBeep = 100; // and a shorter beep
const int sirenTime = 30000; // How long to wail
// variables:
int sensorValue = 0; // the sensor value
int sensorMax = 0; // maximum sensor value
long previousMillis = 0; // for timing
unsigned long currentMillis = 0; // for timing
void setup()
{
// Serial.begin(9600); // comm, disable on ATTiny
// turn on LED to signal the start of the calibration period:
pinMode(sirenPin, OUTPUT);
pinMode (beepPin, OUTPUT);
analogWrite(beepPin, 128); //5 beeps to give time to close box
delay (beepLength);
analogWrite(beepPin, 0);
delay (beepLength);
analogWrite(beepPin, 128);
delay (beepLength);
analogWrite(beepPin, 0);
delay (beepLength);
analogWrite(beepPin, 128);
delay (beepLength);
analogWrite(beepPin, 0);
// calibrate during the next five seconds
while (millis() < 10000)
{
sensorValue = analogRead(sensorPin);
// record the maximum sensor value
if (sensorValue > sensorMax)
{
sensorMax = sensorValue;
// Serial.print("sensorMax= ");
// Serial.println(sensorMax);
}
}
// signal the end of the calibration period
analogWrite(beepPin, 128); //Long beep to signal "ARMED"
delay (2*beepLength);
analogWrite(beepPin, 0);
}
void loop()
{
sensorValue = analogRead(sensorPin); // read the sensor
if (sensorValue > treshold + sensorMax) //is there light from an open door?
{
// Serial.println(sensorValue);
//3 quick beeps to signal "Alarm tripped!"
analogWrite(beepPin, 128);
delay (shortBeep);
analogWrite(beepPin, 0);
delay (2*shortBeep);
analogWrite(beepPin, 128);
delay (shortBeep);
analogWrite(beepPin, 0);
delay (2*shortBeep);
analogWrite(beepPin, 128);
delay (shortBeep);
analogWrite(beepPin, 0);
delay (2*shortBeep);
// end of beepery. Now, you have 2 more seconds to switch off, or I'll scream
delay (2000); // said 2 seconds
digitalWrite (sirenPin, HIGH); // You didn't make it: Wheeeeeeeeeeeeeeeeeeeeeeeeeee!
delay (sirenTime);
}
else
digitalWrite (sirenPin, LOW); // off
delay (1);
}
I'll booby trap the locker with it in the week to come, and see if someone gets a surprise.
(I'll let you know if something funny happens)