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.
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.