DFPlayer is not random?
Hello, I have a problem with my script for a miniDFplayer. I want to play a random file at the touch of a button, but unfortunately the player always plays the same songs in the same order.
here is my script
#include <SoftwareSerial.h>
#include <DFPlayerMini_Fast.h>
#define RX2
#define TX3
SoftwareSerial mySerial(RX, TX);
DFPlayerMini_Fast myDFPlayer;
int buttonPin = 4;
int lastButtonState = HIGH;
int buttonState;
int buttonCount = 0;
void setup() {
mySerial.begin(9600);
myDFPlayer.begin(mySerial);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) {
if (buttonState == LOW) {
buttonCount++;
int fileNumber = random(1, 10);
myDFPlayer.play(fileNumber);
}
delay(50);
}
lastButtonState = buttonState;
}
random returns a long variable back no int. The random function in arduino is rather bad.
grab the random times directly into the df command.
So: myDFPlayer.play(Random(1, 10));