Skip to content

Commit

Permalink
Fixed an issue with the sound loop, so it plays immediately on detect…
Browse files Browse the repository at this point in the history
…ion now
  • Loading branch information
jameselsey committed Dec 15, 2024
1 parent 13eb3fa commit c8ffaae
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions arduino_projects/r2d2_mailbox/r2d2_mailbox/r2d2_mailbox.ino
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,23 @@ 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
if (motionDetected == HIGH && !playing) {
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: "));
Expand All @@ -80,6 +78,7 @@ void loop() {
}
}


void printDetail(uint8_t type, int value){
switch (type) {
case TimeOut:
Expand Down

0 comments on commit c8ffaae

Please sign in to comment.