Étape 2: Le cerveau
L’alarme fonctionne comme suit :
- L’arduino est maintenu endormi pour économiser l’énergie.
- Toutes les 30 s, l’arduino se réveille pour voir si l’interrupteur de la porte est ouverte.
- Si c’est le cas, un timer est activé d’attendre quelques minutes.
- Si passé ce délai, la porte est toujours ouverte, l’alarme se fait entendre jusqu'à ce que la porte est refermée.
J’ai utilisé la bibliothèque de faible puissance rocketscream pour économiser l’énergie, en utilisant le temporisateur, qui est un peu maladroit. J’ai expérimenté à l’aide de l’interrupteur comme une interruption externe sans succès (peut-être quelqu'un pourrait aider avec mon code?). J’ai aussi essayé le narcoleptique dormir bibliothèque qui fournit un "retard de sommeil", mais pour une raison quelconque ce désactivé la possibilité de pwm de l’arduino donc ne fonctionnerait pas le piézo. Mon code ne peut pas être élégant, mais ça marche. Je suis encore à apprendre, toute critique constructive serait apprécié.
Le code de l’arduino est ci-dessous.
#include « LowPower.h »
const int buttonPin = 2 ; bouton de la porte ouverte. Faible lorsque la porte est ouverte. Haute quand la porte est fermée. const int ronfleur = 9 ; const int mené = 13 ; actuellement l’utilisation conduit interne
buttonState int = 0 ;
int doorstate = 0 ;
void setup() {}
pinMode (buttonPin, INPUT_PULLUP) ;
pinMode (vibreur, sortie) ;
pinMode (led, sortie) ;
}
void loop() {}
doorstate 0 = inactif
doorstate 1 = porte ouverte détectés, alarme ne déclenchée pas
doorstate 2 = alarme déclenchée
buttonState = digitalRead(buttonPin) ;
if(doorstate == 0) {}
digitalWrite (conduit, en haut) ; conduit à clignoter brièvement pour indiquer que l’appareil est toujours sur : LowPower.powerDown (SLEEP_30MS, ADC_OFF, BOD_OFF) ;
digitalWrite (led, faible) ; désactiver la LED :
LowPower.powerDown (SLEEP_8S, ADC_OFF, BOD_OFF) ;
LowPower.powerDown (SLEEP_8S, ADC_OFF, BOD_OFF) ;
LowPower.powerDown (SLEEP_8S, ADC_OFF, BOD_OFF) ;
LowPower.powerDown (SLEEP_8S, ADC_OFF, BOD_OFF) ;
LowPower.powerDown (SLEEP_8S, ADC_OFF, BOD_OFF) ;
}
Si (buttonState == LOW & & doorstate == 0) {//if la porte est ouverte
doorstate = 1 ;
LowPower.powerDown (SLEEP_8S, ADC_OFF, BOD_OFF) ; attendre quelques minutes pour déterminer si il s’agit d’une véritable alarme
LowPower.powerDown (SLEEP_8S, ADC_OFF, BOD_OFF) ;
LowPower.powerDown (SLEEP_8S, ADC_OFF, BOD_OFF) ;
LowPower.powerDown (SLEEP_8S, ADC_OFF, BOD_OFF) ;
LowPower.powerDown (SLEEP_8S, ADC_OFF, BOD_OFF) ;
LowPower.powerDown (SLEEP_8S, ADC_OFF, BOD_OFF) ;
LowPower.powerDown (SLEEP_8S, ADC_OFF, BOD_OFF) ;
LowPower.powerDown (SLEEP_8S, ADC_OFF, BOD_OFF) ;
LowPower.powerDown (SLEEP_8S, ADC_OFF, BOD_OFF) ;
LowPower.powerDown (SLEEP_8S, ADC_OFF, BOD_OFF) ;
LowPower.powerDown (SLEEP_8S, ADC_OFF, BOD_OFF) ;
LowPower.powerDown (SLEEP_8S, ADC_OFF, BOD_OFF) ;
LowPower.powerDown (SLEEP_8S, ADC_OFF, BOD_OFF) ;
LowPower.powerDown (SLEEP_8S, ADC_OFF, BOD_OFF) ;
LowPower.powerDown (SLEEP_8S, ADC_OFF, BOD_OFF) ;
LowPower.powerDown (SLEEP_8S, ADC_OFF, BOD_OFF) ;
Si (buttonState == faible) {//is la porte s’ouvre toujours après l’attente ?
ton (sonnerie, 500); / / tonalité de l’alarme :
doorstate = 2 ;
}
}
Si (doorstate == 2) {}
if(ButtonState == High) {}
noTone(buzzer); / / désactiver le signal sonore :
doorstate = 0 ; }
}
}