help with ir, pir motion, and dfplayer logic
im a 4th year student with no prior knowledge on robotics/arduino programming, however i do have basic knowledge on c++
im trying to make a system:
2 pir sensors (pir1 and pir2)
2 ir sensors (ir1 and ir2)
1 dfplayer (mp3 player) - (rx, tx) - (3, 2)
if pir 1 detects, audio 1 will play.
ir pir 2 detects, audio 5 will play.
if pir1 and ir1 both detect, audio 2 will play
if ir2 detects, all audio will stop OR all pir sensors will turn off while its detecting.
#include <SoftwareSerial.h>
#include "DFRobotDFPlayerMini.h"
const int pir1 = 4;
const int pir2 = 6;
const int ir1 = 5;
const int ir2 = 8;
SoftwareSerial mp3Serial(3, 2);
DFRobotDFPlayerMini mp3;
unsigned long lastplay1 = 0, lastplay2 = 0, lastplay3 = 0;
const unsigned long cd1 = 3500;
const unsigned long cd2 = 2000;
const unsigned long cd3 = 3500;
void setup()
{
pinMode(pir1, INPUT);
pinMode(pir2, INPUT);
pinMode(ir1, INPUT_PULLUP);
pinMode(ir2, INPUT_PULLUP);
Serial.begin(9600);
mp3Serial.begin(9600);
if (mp3.begin(mp3Serial))
{
mp3.volume(25);
Serial.println("DFPlayer ready");
}
else
{
Serial.println("DFPlayer not found");
}
delay(2000);
}
void playIfCooldown(int track, unsigned long &last, unsigned long cd)
{
unsigned long now = millis();
if (now - last >= cd)
{
mp3.play(track);
last = now;
Serial.print("Playing track: ");
Serial.println(track);
}
}
void loop()
{
bool zone1 = digitalRead(pir1) == HIGH;
bool zone2 = digitalRead(pir2) == HIGH;
bool trainin = digitalRead(ir1) == LOW;
bool trainstop = digitalRead(ir2) == LOW;
if (trainstop) { mp3.stop(); Serial.println("Train stopped -> stop audio"); } else if (zone1) { if (trainin) { playIfCooldown(2, lastplay3, cd3); } else { playIfCooldown(1, lastplay1, cd1); } } else if (zone2) { playIfCooldown(3, lastplay2, cd2); } } issue: the pir somehow just keeps detecting?
bool trainstop = digitalRead(ir2) == LOW;
if (trainstop) { mp3.stop(); Serial.println("Train stopped -> stop audio"); } else if (zone1) { if (trainin) { playIfCooldown(2, lastplay3, cd3); } else { playIfCooldown(1, lastplay1, cd1); } } else if (zone2) { playIfCooldown(3, lastplay2, cd2); } } issue: the pir somehow just keeps detecting?
7 Replies
You need to break it down and show what each sensor is getting.
Example print out the value of the sonic sensor, and print out the value of the IR sensor.
here, i changed the code. but what happens is that pir2 is constantly switching from 0 to 1 and 0 to 1. its acting independently. even if theres motion in front of the sensor, it just keeps printing 01010101
#include <SoftwareSerial.h>
#include "DFRobotDFPlayerMini.h"
// Pin assignments
const int PIR1 = 4;
const int PIR2 = 6;
const int IR1 = 8;
const int IR2 = 5;
SoftwareSerial mp3Serial(3, 2); // RX, TX
DFRobotDFPlayerMini mp3;
bool isPlaying = false;
void setup() {
Serial.begin(9600);
mp3Serial.begin(9600);
// Use INPUT_PULLUP so idle = HIGH, triggered = LOW
pinMode(PIR1, INPUT);
pinMode(PIR2, INPUT);
pinMode(IR1, INPUT_PULLUP);
pinMode(IR2, INPUT_PULLUP);
if (mp3.begin(mp3Serial)) {
mp3.volume(25);
Serial.println("DFPlayer ready");
} else {
Serial.println("DFPlayer not found");
while (true);
}
}
void loop() {
// Read sensors
int pir1 = digitalRead(PIR1);
int pir2 = digitalRead(PIR2);
int ir1 = digitalRead(IR1);
int ir2 = digitalRead(IR2);
// Debugging
Serial.print("PIR1: "); Serial.print(pir1);
Serial.print(" PIR2: "); Serial.print(pir2);
Serial.print(" IR1: "); Serial.print(ir1);
Serial.print(" IR2: "); Serial.println(ir2);
// DFPlayer event handling
if (mp3.available()) {
uint8_t type = mp3.readType();
if (type == DFPlayerPlayFinished) {
Serial.println("Track finished → ready for next trigger");
isPlaying = false;
}
}
if (ir2 == LOW) { // IR2 active → stop audio
if (isPlaying)
{
mp3.stop();
Serial.println("IR2 active → Audio stopped");
isPlaying = false;
}
}
else if (ir1 == LOW && ir2 == HIGH) { // IR1 ON, IR2 OFF
if (pir1 == LOW && !isPlaying) {
mp3.play(2);
isPlaying = true;
Serial.println("Audio2 (IR1 + PIR1)");
}
else if (pir1 == HIGH && !isPlaying) {
mp3.play(5);
isPlaying = true;
Serial.println("Audio5 (IR1 only)");
}
}
else if (ir1 == HIGH && ir2 == HIGH) { // Both IR OFF
if (pir1 == LOW && !isPlaying) {
mp3.play(1);
isPlaying = true;
Serial.println("Audio1 (PIR1 only)");
}
else if (pir1 == HIGH && !isPlaying) {
mp3.play(5);
isPlaying = true;
Serial.println("Audio5 (Idle default)");
}
}
delay(100); // small delay for stability
}
this smells like AI code.
alright man my bad but i did put in the logic i wanted, i had a brief c++ code fragment of the if else logic i wanted, and i did ask ai to help me implement that into the code and check for errors because as i said i dont have any prior knowledge in arduino/robotics programming. i checked the code too and i thought it looked fine and i havent been programming long enough to really see errors, so thats why im asking for help here
Well thats why its all failing.
And as we dont help users with AI code, you can close this thread, and work on it your self with your chat bot frien.
Good luck @sen 👋🏽
alright man