Sensor design with arduino

I'm doing a project on Arduino with a motion sensor that activates a sound signal, through a dfplayer module, I have some doubts if the code is correct, given that I'm a beginner in Arduino programming. Any tips?
follow the code

#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

int pinSensorPIR = 8;
int pinLed = 9;
int valorSensorPIR = 0;


SoftwareSerial mySoftwareSerial(10, 11);


DFRobotDFPlayerMini myDFPlayer;


char buf;
int pausa = 0;
int equalizacao = 0;

void setup() {
  Serial.begin(9600);


  pinMode(pinSensorPIR, INPUT);
  pinMode(pinLed, OUTPUT);


  mySoftwareSerial.begin(9600);


  myDFPlayer.begin(mySoftwareSerial);


  myDFPlayer.setTimeOut(500);
  myDFPlayer.volume(30);
  myDFPlayer.EQ(0);
}

void loop() {


  while (Serial.available() > 0) {
    buf = Serial.read();


    if (buf == '1') {
      buf = buf - 48;
      myDFPlayer.play(buf);
    }
  }

  if (digitalRead(valorSensorPIR)) {
    myDFPlayer.play(1);
    delay(4000);
  }
  else {
    myDFPlayer.stop();
  }
}

You are likely to get more and faster help if you post the code properly.
Read the forum guidelines to see how to properly post code and some good information on making a good post.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

thanks

You can go back and fix your original post by highlighting the code and clicking the </> in the menu bar.
code tags new

thank you so much

my tip: just upload.

1 Like

Did you get the DF Player to work by itself using the library example code?

yes, just not being able to match the sensor

Not sure what you mean. The motion sensor? Match, how?

the sensor will identify the movement and, in response, the led lights up and the beep sounds through the sound configured by the df player

What does the code actually do?

It is late here so I have to sleep.

its purpose is to activate the sensor with movement and soon after turning on the led and emitting the beep (music) and after a few seconds pause

Is this what it actually does, or what you want it to do?

It's not clear (to me anyway) what the problem is: ie, what's the "gap" between your requirement and what's actually happening.

Hello,

if (digitalRead(pinSensorPIR)) {

Not how you had it

if (digitalRead(valorSensorPIR)) {

Or do a PIR reading before the if

valorSensorPIR = digitalRead(pinSensorPIR);

You also need to turn on the led in the IF and turn it off in the else " digitalWrite (pinLed, 1 or 0); "
I don't understand the purpose of reading the serial port. Could you explain it?
Greetings.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.