From c8ffaaed95b5529ac5645251e63f3eb83186f3a4 Mon Sep 17 00:00:00 2001 From: jelsey Date: Sun, 15 Dec 2024 20:51:31 +1100 Subject: [PATCH] Fixed an issue with the sound loop, so it plays immediately on detection now --- .../r2d2_mailbox/r2d2_mailbox/r2d2_mailbox.ino | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/arduino_projects/r2d2_mailbox/r2d2_mailbox/r2d2_mailbox.ino b/arduino_projects/r2d2_mailbox/r2d2_mailbox/r2d2_mailbox.ino index c772964..8f8250a 100644 --- a/arduino_projects/r2d2_mailbox/r2d2_mailbox/r2d2_mailbox.ino +++ b/arduino_projects/r2d2_mailbox/r2d2_mailbox/r2d2_mailbox.ino @@ -41,11 +41,9 @@ void setup() } void loop() { - static bool playing = false; + static bool playing = false; static unsigned long lastPlayTime = 0; static int soundsPlayed = 0; // Counter for the number of sounds played in the current detection - static int pirState = LOW; // Current state of the PIR sensor - int motionDetected = digitalRead(PIR_PIN); // Read PIR sensor state (HIGH or LOW) // If motion is detected and we're not already playing sounds @@ -53,13 +51,13 @@ void loop() { Serial.println(F("Motion detected! Starting playback...")); playing = true; // Start the playback sequence soundsPlayed = 0; // Reset the sounds played counter - lastPlayTime = millis(); // Reset the timer + lastPlayTime = 0; // Reset the timer to trigger immediate playback } // If in playback mode, handle the sound playback sequence if (playing) { if (soundsPlayed < PLAY_SOUND_COUNT) { // Play up to 3 sounds - if (millis() - lastPlayTime >= PLAY_SOUND_DELAY) { // delay between sounds + if (lastPlayTime == 0 || millis() - lastPlayTime >= PLAY_SOUND_DELAY) { int randomSound = random(1, 9); // Random sound between 1.mp3 and 8.mp3 myDFPlayer.play(randomSound); Serial.print(F("Playing sound: ")); @@ -80,6 +78,7 @@ void loop() { } } + void printDetail(uint8_t type, int value){ switch (type) { case TimeOut: