Browse Source

first commit, functional code

master
Barb 3 months ago
commit
f14404ced7
2 changed files with 95 additions and 0 deletions
  1. +10
    -0
      README.md
  2. +85
    -0
      allarme_aula_stud.ino

+ 10
- 0
README.md View File

@ -0,0 +1,10 @@
Allarme armadio aulastud
l'obiettivo è fare da dissuasore, non ci sono password, o segnalazioni automatiche. solo avviso acustico con sirena.
- attiny45
- un rele
- pulsante tattile per comando
- led per informazione stato (abilitato o meno l'allarme)
- microswitch nello sportello per accorgersi dell'apertura
- l'allarme si abilita di default alla connessione
- suona brevemente 2 volte per segnare abilitazione, 1 volta per disabilitazione
- in caso scatti, suonerà a volume massimo per 10s, poi si disabiliterà fino a riavvio/nuovo comando

+ 85
- 0
allarme_aula_stud.ino View File

@ -0,0 +1,85 @@
#define relayPin 1 // enable/disable relay powering the siren
#define switchPin 2 // tampering switch sensor
#define buttonPin 3 // button to be held down to enable/disable alarm
#define ledPin 4 // status LED to show when alarm is enabled
#define ringTime 20 // time to ring the siren for (in seconds)
int enableStatus = 1; // variable to store alarm status (1 = enabled); enabled at boot
int countDown = 2; // times to repeat siren blip sound in sound() function
void setup() {
// here we assign pins as in/out and pull-up the inputs
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW);
pinMode(switchPin, INPUT_PULLUP);
digitalWrite(switchPin, HIGH);
pinMode(buttonPin, INPUT_PULLUP);
digitalWrite(buttonPin, HIGH);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
sound(); // give audible feedback at boot
delay(10000); // wait 10s, then exit setup
}
void loop() {
LED();
ButtonCtrl();
//Main alarm logic follows
if (enableStatus == 1 && (digitalRead(switchPin) == LOW)){
delay(500); //switch debounce
if (digitalRead(switchPin) == LOW){
digitalWrite(relayPin, HIGH); //sound the siren!
delay(ringTime*1000); //let the siren sound for the duration of defined ringTime
digitalWrite(relayPin, LOW); //now switch off the siren
digitalWrite(ledPin, LOW); //switch off status LED
enableStatus = 0; //disable the alarm after sounding
}
}
}
void sound() {
// sound alert function used at boot and alarm enabling
if (enableStatus == 1) {
while (countDown > 0) {
digitalWrite(relayPin, HIGH); //sound the siren!
delay(5);
digitalWrite(relayPin, LOW); //stop the siren
delay(500);
countDown--;
}
}
}
void LED() {
// status LED control logic
if (enableStatus == 1) {
digitalWrite(ledPin, HIGH); //turn on status LED
}
else {
digitalWrite(ledPin, LOW); //turn off status LED
}
}
void ButtonCtrl() {
// check if button is pressed, switch state accordingly
if ((digitalRead(buttonPin) == LOW) && enableStatus != 1){
delay(500); //button debounce
if (digitalRead(buttonPin) == LOW){
enableStatus = 1;
countDown = 2;
sound();
}
}
else if ((digitalRead(buttonPin) == LOW) && enableStatus != 0){
delay(500); //button debounce
if (digitalRead(buttonPin) == LOW){
countDown = 1;
sound();
enableStatus = 0;
}
}
}

Loading…
Cancel
Save