Hello!
I have a small project in mind.
I want to incorporate electronics to a 3D printed droid I made, and I am looking for advice on what hardware to use.
Te requirements are:
-When a button is pressed, a short random audio file must be played and two LEDs are turned on, until the audio playback stops.
-When playback ends, nothing happens until the button is pressed again.
-The sequence repeats with each button press.
Do you think I can achieve this with just an arduino nano? Or do I also need to buy a DFplayer Mini board?
Or do you have different hardware suggestions?
Thanks!
blh64
November 23, 2022, 3:29pm
2
You will definitely need a DFplayer Mini to play any type of sound. If size is a concern, you could go with a micro v. a nano. Many, many examples out there
Here is an example that does what you describe using a DF Mini MP3 player, 2 buttons and an LED.
Press the random play button to play a file, the stop button stops playback. The LED flashes only while the file is playing.
// Press the random play button to play a file, the stop button stops playback.
// The LED flashes only while the file is playing.
// By c goulding aka groundFungus
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
const byte NUM_FILES = 5; // ********** change to your number ************
const byte busyPin = 4;
const byte buttonPin = 8;
const byte buttonStop = 9;
const byte pinLed = 10;
SoftwareSerial mySoftwareSerial(7, 6);
DFRobotDFPlayerMini myDFPlayer;
unsigned long currentMillis = 0;
void setup()
{
Serial.begin(115200);
Serial.println("Play random file if button pressed. Flash LED while playing.");
pinMode(buttonPin, INPUT_PULLUP);
pinMode(buttonStop, INPUT_PULLUP);
pinMode(pinLed, OUTPUT);
mySoftwareSerial.begin(9600);
randomSeed(analogRead(A0) * analogRead(A1));
myDFPlayer.begin(mySoftwareSerial);
myDFPlayer.setTimeOut(500);
myDFPlayer.volume(30);
myDFPlayer.EQ(2);
}
void loop()
{
currentMillis = millis();
static bool lastButtonState = HIGH;
bool buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState)
{
if (buttonState == LOW)
{
//digitalWrite(pinLed, HIGH);
byte fileToPlay = random(0, NUM_FILES + 1);
//Serial.println(fileToPlay);
myDFPlayer.play(fileToPlay);
}
lastButtonState = buttonState;
}
static bool lastStopButtonState = HIGH;
bool StopButtonState = digitalRead(buttonStop);
if (StopButtonState != lastStopButtonState)
{
if (StopButtonState == LOW)
{
//digitalWrite(pinLed, HIGH);
//byte fileToPlay = random(0, NUM_FILES + 1);
//Serial.println("stop");
myDFPlayer.stop();
}
lastStopButtonState = StopButtonState;
}
if (digitalRead(busyPin) == LOW)
{
flashLED();
}
else
{
digitalWrite(pinLed, LOW);
}
}
void flashLED()
{
static unsigned long timer = 0;
unsigned long interval = 100;
if (currentMillis - timer >= interval)
{
timer = currentMillis;
digitalWrite(pinLed, !digitalRead(pinLed));
}
}