#include <DFPlayerMini_Fast.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial1(15, 14); // RX, TX
DFPlayerMini_Fast player1;
int IN22 = 22;
int IN48 = 48;
void setup() {
// put your setup code here, to run once:
mySerial1.begin(9600);
if(player1.begin(mySerial1, false))
{
player1.volume(22);
}
pinMode(22, INPUT_PULLUP);
pinMode(48, INPUT_PULLUP);
}
void loop() {
// put your main code here, to run repeatedly:
if(!digitalRead(22))
{
player1.play(1);
while(!digitalRead(22));
}
if(!digitalRead(48))
{
player1.stop();
while(!digitalRead(48));
}
}
With this above simple code for playing an audio file with a single and simple Push button from a DF Player module with the help of DF Player mini Fast library
I am having this one and only issue :-
When I press the button the audio starts playing immediately with no issues and no time delay
But on releasing the button , the audio file starts again from the starting point
twice.... You're lucky. It could have been more.
Set up an oscilloscope and monitor how switches/buttons make contact. They jump up and down plenty of times.
#include <DFPlayerMini_Fast.h>
//#include <SoftwareSerial.h>
//SoftwareSerial mySerial3(15, 14); // RX, TX
DFPlayerMini_Fast player1;
int IN22 = 22;
int LOCK = 47;
int IN48 = 48;
void setup() {
// put your setup code here, to run once:
Serial3.begin(9600);
if(player1.begin(Serial3, false))
{
player1.volume(22);
}
// while (!Serial3) {
// ; // wait for serial port to connect. Needed for native USB
// }
pinMode(22, INPUT_PULLUP);
pinMode(48, INPUT_PULLUP);
pinMode(LOCK, OUTPUT);
digitalWrite(LOCK,LOW);
}
void loop() {
// put your main code here, to run repeatedly:
if(!digitalRead(22))
{
player1.play(1);
while(!digitalRead(22));
}
if(!digitalRead(48))
{
player1.stop();
while(!digitalRead(48));
}
}
But still having same result !!!!!!!
Dual triggering !!!!!!
I did it with this one from state change detection example
#include <DFPlayerMini_Fast.h>
DFPlayerMini_Fast player1;
int count;
// this constant won't change:
const byte buttonPin = 22; // the pin that the pushbutton is attached to
const byte ledPin = 13; // the pin that the LED is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
boolean buttonState = 0; // current state of the button
boolean lastButtonState = 0; // previous state of the button
const byte LockInd = 47; // Button Lock
const byte Stopper1 = 48;
void setup()
{
Serial3.begin(9600);
if(player1.begin(Serial3, false))
{
player1.volume(22);
}
pinMode (LockInd, OUTPUT);
digitalWrite(LockInd, LOW);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(Stopper1, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
}
void loop()
{
static unsigned long timer = 0;
unsigned long interval = 100;
if (millis() - timer >= interval)
{
timer = millis();
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState)
{
if (buttonState == LOW)
{
buttonPushCounter++;
}
lastButtonState = buttonState;
}
}
if (buttonPushCounter % 2 == 0)// && buttonPushCounter > 0)
{
digitalWrite(ledPin, HIGH);
}
else
{
digitalWrite(ledPin, HIGH);
player1.play(1);
}
if(!digitalRead(Stopper1))
{
player1.stop();
}
}
Result :-
Ya its one for the first press it starts the file but the playing is completely abnormal !!
It is like , If a person Press and Release the Button with a speed of some 44 Khz then how ll be the sound get produce => Drrrrrrrrrr or Jrrrrrrrrr or Grrrrrrrr . .
(Depending on the starting point of the audio file)
Means an oscillation type thing is going on there after last ' else ' part
Then again after pressing the push button
The file starts playing normally within the ' if ' area
You are basing your reaction on the fact that the button has been pressed an odd number of times.
Which when true will be true until you press the button again.
I'm going to hand you this idea and I am then leaving you to it, I am sorry but I've followed along and I am exhausted by your progress.
I suggest you study this carefully and see where it differs, and why. But I know that is aksing quite a bit if you.
If this doesn't work, figure it out. I will entertain no protracted discussion of this, I'm about to go to the beach anyway.
#include <DFPlayerMini_Fast.h>
DFPlayerMini_Fast player1;
int count;
// this constant won't change:
const byte buttonPin = 22; // the pin that the pushbutton is attached to
const byte ledPin = 13; // the pin that the LED is attached to
// Variables will change:
int buttonGotPushed; // flag for noticing a button push
boolean buttonState = 0; // current state of the button
boolean lastButtonState = 0; // previous state of the button
const byte LockInd = 47; // Button Lock
const byte Stopper1 = 48;
void setup()
{
Serial3.begin(9600);
if(player1.begin(Serial3, false))
{
player1.volume(22);
}
pinMode (LockInd, OUTPUT);
digitalWrite(LockInd, LOW);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(Stopper1, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
}
void loop()
{
static unsigned long timer = 0;
unsigned long interval = 100;
if (millis() - timer >= interval)
{
timer = millis();
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState)
{
if (buttonState == LOW)
{
buttonGotPushed = 1; // set the flag. we take action on it elsewhere
}
lastButtonState = buttonState;
}
}
if (buttonPushGotPushed)
{
player1.play(1);
buttonGotPushed = 0; // reset the flag
}
if(!digitalRead(Stopper1))
{
player1.stop();
}
}
You are now approaching a real giant leap forward in your progress. I encourage you to learn what I will reveal (!) using tutorials or videos or whatever learning source works best for you not through an endless process of…
trying, failing, and asking here why in order to get ahead tiny steps at a time, wearing out the patience of those who want to help. "Fake it until you make it" is a miserable strategy for improving your coding skills.
That said, and appy polly loggies if I've been rude…
Any time you find yourself numbering variables to facilitate coding by copying, pasting and editing entire blocks of code, it is a clue that you need to know about and exploit
O o No issues sir . . . That is actually a way of motivating enough to get a better kick towards the learning and growing
Ya i was on that part only from some old Threads and designed it for 5 PINS
#include <DFPlayerMini_Fast.h>
DFPlayerMini_Fast player1;
int count;
int Buttons [] = {22,23,24,25,26};
const byte ledPin = 13; // the pin that the LED is attached to
int buttonGotPushed;
boolean buttonState = 0;
boolean lastButtonState = 0;
const byte LockInd = 47; // Button Lock
const byte Stopper1 = 48;
void setup()
{
Serial3.begin(9600);
if(player1.begin(Serial3, false))
{
player1.volume(22);
}
for (int i = 0; i <= 5; i++)
{
pinMode(Buttons[i], INPUT_PULLUP);
}
pinMode (LockInd, OUTPUT);
digitalWrite(LockInd, LOW);
pinMode(Stopper1, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
}
void loop()
{
StateDetectionUnitBOX(0);
{
if (buttonGotPushed)
{
player1.play(1);
buttonGotPushed = 0;
}
}
// StateDetectionUnitBOX(1);
// {
// if (buttonGotPushed)
// {
// player1.play(2);
// buttonGotPushed = 0;
// }
// }
//
// StateDetectionUnitBOX(2);
// {
// if (buttonGotPushed)
// {
// player1.play(3);
// buttonGotPushed = 0;
// }
// }
//
// StateDetectionUnitBOX(3);
// {
// if (buttonGotPushed)
// {
// player1.play(4);
// buttonGotPushed = 0;
// }
// }
//
// StateDetectionUnitBOX(4);
// {
// if (buttonGotPushed)
// {
// player1.play(5);
// buttonGotPushed = 0;
// }
// }
if(!digitalRead(Stopper1))
{
player1.stop();
}
}
void StateDetectionUnitBOX(byte pin)
{
static unsigned long timer = 0;
unsigned long interval = 5;
if (millis() - timer >= interval)
{
timer = millis();
buttonState = digitalRead(Buttons[pin]);
// while(!digitalRead(Buttons[pin]));
// while(buttonState);
if (buttonState != lastButtonState)
{
if (buttonState == LOW)
{
buttonGotPushed = 1; // set the flag. we take action on it elsewhere
}
lastButtonState = buttonState;
}
}
// while(buttonState);
// while(!digitalRead(Buttons[pin]));
}
Some Errors are here
Within void loop , after defining other PINS
1)Each and every PINS gets oscillated on press and holding the key
After pressing a key and with audio file running , if we press another key
then its not come into effect immediately
at least 2 3 times needed to get activated
My beach buddy is on her way, she who must not be kept waiting.
You need array variables instead of scalar variables for at at least the ones I list above.
Think about it - each button needs its own state, its own flag, its own timer and so forth.
At your level: use multiple arrays. All as big as the number of buttons.
int buttonGotPushed[NBUTTONS];
boolean buttonState[NBUTTONS];
boolean lastButtonState[NBUTTONS];
At your next level: use struct to make structured variables that hold all the disparate variables needed by each button. Arrays of those for multiple buttons
Somewhere in your future: C++ classes, that can wrap up code and variables in objects you can have an array of which.
But step by step. I still use parallel arrays when I am too lazy to go full-blown modern style.