From 360897bda98b985134262a91a345bb7af0802666 Mon Sep 17 00:00:00 2001 From: Rsclub2_2 Date: Sat, 23 Mar 2024 17:36:41 +0100 Subject: [PATCH] =?UTF-8?q?src/Lock-Module-Source.ino=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Lock-Module-Source.ino | 159 +++++++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 src/Lock-Module-Source.ino diff --git a/src/Lock-Module-Source.ino b/src/Lock-Module-Source.ino new file mode 100644 index 0000000..e5e64cf --- /dev/null +++ b/src/Lock-Module-Source.ino @@ -0,0 +1,159 @@ +#include "Arduino.h" +#include "SoftwareSerial.h" +#include "DFRobotDFPlayerMini.h" + +SoftwareSerial mySoftwareSerial(4, 5); +DFRobotDFPlayerMini myDFPlayer; + +const int mosfetPin = 12; +const int buttonSoundPin = 10; //button to change open sound +const int buttonClosedCheck = 9; //button to check, if door is closed +const int sendPin = 3; //sends to Pin 71 +const int receivePin = 2; //receives from Pin 69 + +byte tracknumber = 1; +bool alreadyPushed = false; //was the "change sound"-button already pushed? + +unsigned long timerWarningSound = 0; //timer for warning sounds +bool setMillisWarningSound = false; +int timeUntilWarningFinal = 20000; //warning- or "remember-to-close-door"-sound after 20sek (20000ms) +int timeUntilWarning = timeUntilWarningFinal; //variable to decrease time between warning sounds +byte warningSoundPlayed = 0; //gets inc when warning sound was played (to limit amount of warnings) + +unsigned long timerLockOpen = 0; //how long before lock gets closed again +boolean openFunctionTriggered = false; //was the funtion open already called? +bool open = false; //gets triggered with interrupt from other arduino + + +void isrPin2Receive() { + static unsigned long lastReceiveTime = 0; + if (millis() - lastReceiveTime >= 50) { + if (digitalRead(receivePin) == 1) { + open = true; + } + lastReceiveTime = millis(); + } +} + +void OpenLock() { + myDFPlayer.volume(30); + myDFPlayer.play(tracknumber); + if (tracknumber == 3) { + delay(8000); + } + //OPEN LOCK digitalWrite(mosfetPin, HIGH); + asm volatile( + "sbi %0, %1 \n\t" // sets Bit in PORTB to HIGH + : + : "I"(_SFR_IO_ADDR(PORTB)), "I"(PORTB4)); +} + + +void setup() { + // put your setup code here, to run once: + + Serial.begin(9600); + mySoftwareSerial.begin(9600); + if (!myDFPlayer.begin(mySoftwareSerial)) { + while (true) { + delay(0); + } + } + + myDFPlayer.volume(30); // max volume is 30 + pinMode(mosfetPin, OUTPUT); + pinMode(buttonSoundPin, INPUT_PULLUP); + pinMode(buttonClosedCheck, INPUT_PULLUP); + pinMode(sendPin, OUTPUT); + pinMode(receivePin, INPUT); + attachInterrupt(digitalPinToInterrupt(receivePin), isrPin2Receive, RISING); +} + + +void loop() { + // put your main code here, to run repeatedly + + //change the sound when opening the door with the push of a button + if (digitalRead(buttonSoundPin) == 0) { + //default sound --> serious sound + if (tracknumber == 1 && alreadyPushed == false) { + tracknumber = 4; + myDFPlayer.volume(20); + myDFPlayer.play(tracknumber); + alreadyPushed = true; + } + //serious sound --> zelda sound + if (tracknumber == 4 && alreadyPushed == false) { + tracknumber = 3; + myDFPlayer.volume(20); + myDFPlayer.play(tracknumber); + alreadyPushed = true; + } + //zelda sound --> default sound + if (tracknumber == 3 && alreadyPushed == false) { + tracknumber = 1; + myDFPlayer.volume(20); + myDFPlayer.play(tracknumber); + alreadyPushed = true; + } + } + + //prevents weird behaviour when changing sounds + if (digitalRead(buttonSoundPin) == 1) { + alreadyPushed = false; + } + + + //DOOR CLOSED: resets warningSoundsPlayed and sends low to other microcontroller + if (digitalRead(buttonClosedCheck) == 0) { + warningSoundPlayed = 0; + digitalWrite(sendPin, LOW); + timeUntilWarning = timeUntilWarningFinal; + setMillisWarningSound = false; + } + + //DOOR NOT CLOSED + if (digitalRead(buttonClosedCheck) == 1) { + digitalWrite(sendPin, HIGH); + + if (warningSoundPlayed < 3) { + + if (setMillisWarningSound == false) { + timerWarningSound = millis(); + setMillisWarningSound = true; + } + + if (millis() - timerWarningSound >= timeUntilWarning) { + myDFPlayer.volume(20); + myDFPlayer.play(2); + setMillisWarningSound = false; + warningSoundPlayed = warningSoundPlayed + 1; + + if (timeUntilWarning > 20000) { + timeUntilWarning = timeUntilWarning - 15000; + } + } + } + } + + //OPENS LOCK + if (open == true) { + + if (openFunctionTriggered == false) { + OpenLock(); + openFunctionTriggered = true; + timerLockOpen = millis(); + } + + if (millis() - timerLockOpen >= 8000) { + timerLockOpen = millis(); + //CLOSE LOCK digitalWrite(mosfetPin, LOW); + asm volatile( + "cbi %0, %1 \n\t" // sets Bit in PORTB to LOW + : + : "I"(_SFR_IO_ADDR(PORTB)), "I"(PORTB4)); + open = false; + openFunctionTriggered = false; + } + } +} \ No newline at end of file