arduino dfplayer soll zufälliges lied spielen

#7555333
Lesenswert?

Hallo zusammen, ich habe einen Arduino Nano CH340 mit einem DFPlayer und einem PIR verbunden. Sobald der Bewegung spürt, wird auch eine mp3 abgespielt. Den Code dazu findet ihr hier:

1
#include "SoftwareSerial.h"
2
#include "DFRobotDFPlayerMini.h"
3

4
// Use pins 2 and 3 to communicate with DFPlayer Mini
5
static const uint8_t PIN_MP3_TX = 10; // Connects to module's RX
6
static const uint8_t PIN_MP3_RX = 11; // Connects to module's TX
7
SoftwareSerial softwareSerial(PIN_MP3_RX, PIN_MP3_TX);
8

9
const int PIR = 8;
10
int input_val = 0;
11

12

13
// Create the Player object
14
DFRobotDFPlayerMini player;
15

16
void setup() {
17

18

19
  // Init USB serial port for debugging
20
  Serial.begin(9600);
21
  // Init serial port for DFPlayer Mini
22
  softwareSerial.begin(9600);
23

24
  // Start communication with DFPlayer Mini
25
  if (player.begin(softwareSerial)) {
26
    Serial.println("OK");
27

28
    // Set volume to maximum (0 to 30).
29
    player.volume(30);
30

31
  } else {
32
    Serial.println("Connecting to DFPlayer Mini failed!");
33
  }
34
    delay(50000);
35
}
36

37
void loop() {
38
  input_val = digitalRead(PIR);
39

40
  delay(1000);
41
  Serial.println(input_val);
42

43
  if (input_val > 0)
44
  {
45
    player.play(1);
46

47
    Serial.println("motion detected ");
48
     delay(600000);
49
  }
50

51
}

Hier steht ja klar:

1
if (input_val > 0)
2
  {
3
    player.play(1);

d.h. er spielt immer Lied Nummer 1.

Was muss ich am Code ändern, damit er sich zufällig aus der kompletten Liste an mp3s (0001.mp3 ..... 0020.mp3) jedes Mal wenn der PIR ein Signal schickt eine andere mp3 abspielt und auch nur 1 Lied, nicht alle. Nach dem einen Lied soll er stoppen. Ab dem Zeitpunkt der Detektion soll er mindestens 10min warten, bis er wieder auf ein Signal vom PIR reagiert und ein Lied abspielt.

Vielen Dank schon mal für eure Hilfe!!!

#7555353
Lesenswert?

ich habe den code jetzt mal so abgeändert. Beim Verifizieren gibt es schon mal keinen Fehler mehr. Ich werde nun auf dem Arduino testen.

1
#include "SoftwareSerial.h"
2
#include "DFRobotDFPlayerMini.h"
3

4
// Use pins 2 and 3 to communicate with DFPlayer Mini
5
static const uint8_t PIN_MP3_TX = 10; // Connects to module's RX
6
static const uint8_t PIN_MP3_RX = 11; // Connects to module's TX
7
SoftwareSerial softwareSerial(PIN_MP3_RX, PIN_MP3_TX);
8

9
const int PIR = 8;
10
int input_val = 0;
11

12
long randNumber;
13
// Create the Player object
14
DFRobotDFPlayerMini player;
15

16
void setup() {
17

18
  randomSeed(analogRead(0));
19
  // Init USB serial port for debugging
20
  Serial.begin(9600);
21
  // Init serial port for DFPlayer Mini
22
  softwareSerial.begin(9600);
23

24
  // Start communication with DFPlayer Mini
25
  if (player.begin(softwareSerial)) {
26
    Serial.println("OK");
27

28
    // Set volume to maximum (0 to 30).
29
    player.volume(30);
30

31
  } else {
32
    Serial.println("Connecting to DFPlayer Mini failed!");
33
  }
34
    delay(50000);
35
}
36

37
void loop() {
38
  input_val = digitalRead(PIR);
39
  randNumber = random(19);
40
  Serial.println(randNumber);
41

42
  delay(1000);
43
  Serial.println(input_val);
44

45
  if (input_val > 0)
46
  {
47
    player.play(randNumber);
48

49
    Serial.println("motion detected ");
50
     delay(600000);
51
  }
52

53
}

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren