176 lines
5.1 KiB
C++
176 lines
5.1 KiB
C++
/*
|
|
Copyright (C) 2024 Riccardo Henning and Philipp Wagner
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#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;
|
|
}
|
|
}
|
|
} |