Expanded my previous circuit with the DFPlayer Mini to now activate an arduino servo from 0 – 180 degrees while playing an audio file of a garage door. When pressed again, the servo reverts back from 180 – 0 and again plays the audio file. Six of these will be used to control the bay doors on the Walthers diesel house.
Servo code
// Diesel shop controls via push button and sound
// Author: Michael Peters Bond - Merrimack Valley Railroad
#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h>
#include <Servo.h>
// LEDS
int WELDERLED = 8;
// BUTTONS
int WELDERBUTTON = 4;
int BAY1FRONTBUTTON = 22;
// SERVOS
Servo BAY1FRONTSERVO;
// Servo positions
int BAY1FRONTPOS = 0;
// DFPlayer
SoftwareSerial mySoftwareSerial(11, 12); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void setup() {
// Setup leds
pinMode(WELDERLED, OUTPUT);
// Setup buttons
pinMode(WELDERBUTTON, INPUT);
pinMode(BAY1FRONTBUTTON, INPUT);
// Setup Servos
BAY1FRONTSERVO.attach(30);
BAY1FRONTSERVO.write(0);
// Setup DFPlayer Mini
mySoftwareSerial.begin(9600);
Serial.begin(115200);
Serial.println();
Serial.println(F("DFRobot DFPlayer Mini Demo"));
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
Serial.println(F("Unable to begin:"));
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
while(true);
}
Serial.println(F("DFPlayer Mini online."));
myDFPlayer.volume(20); //Set volume value. From 0 to 30
}
void loop() {
// Welder Button
if(digitalRead(WELDERBUTTON) == HIGH)
{
myDFPlayer.play(1);
int i,count;
//count=random(10,60);
for (i=0;i<95;i++)
{
digitalWrite(WELDERLED, HIGH); // set the LED on
delay(random(60));
digitalWrite(WELDERLED, LOW); // set the LED off
delay(random(200));
}
}
else
{
digitalWrite(WELDERLED, LOW);
}
// Bay 1 front door button open
if(digitalRead(BAY1FRONTBUTTON) == HIGH && BAY1FRONTPOS <= 5)
{
Serial.print("Bay 1 position: ");
Serial.println(BAY1FRONTPOS);
BAY1FRONTPOS = MoveBayDoor(BAY1FRONTSERVO, BAY1FRONTPOS);
Serial.print("Bay 1 position: ");
Serial.println(BAY1FRONTPOS);
}
// Bay 1 front door button close
if(digitalRead(BAY1FRONTBUTTON) == HIGH && BAY1FRONTPOS >= 160)
{
Serial.print("Bay 1 position: ");
Serial.println(BAY1FRONTPOS);
BAY1FRONTPOS = MoveBayDoor(BAY1FRONTSERVO, BAY1FRONTPOS);
Serial.print("Bay 1 position: ");
Serial.println(BAY1FRONTPOS);
}
}
// Method that moves the any bay door.
int MoveBayDoor(Servo BAYSERVO, int BAYCURRENTPOS)
{
if(BAYCURRENTPOS > 90)
{
myDFPlayer.play(2); // Play the garage door sound
for (BAYCURRENTPOS = 180; BAYCURRENTPOS >= 0; BAYCURRENTPOS -= 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
BAYSERVO.write(BAYCURRENTPOS); // tell servo to go to position in variable 'pos'
delay(80); // waits 15ms for the servo to reach the position
}
}
else
{
myDFPlayer.play(2); // Play the garage door sound
for (BAYCURRENTPOS = 0; BAYCURRENTPOS <= 180; BAYCURRENTPOS += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
BAYSERVO.write(BAYCURRENTPOS); // tell servo to go to position in variable 'pos'
delay(80); // waits 15ms for the servo to reach the position
}
}
return BAYCURRENTPOS;
}
I’m looking to do a really similar thing to this project, would you be able to show how you’ve set the boards up?