You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
int relayPin = 7; //Relay Control - Lights
const int buttonPin = 2; // the number of the pushbutton pin
Encoder myEnc(3, 4);
int encoderPos = 0;
const int MIN_POSITION = 0;
const int MAX_POSITION = 30;
boolean encoderALast = LOW; //remembers the previous state of the encoder pin A
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
int playing; //Track playing state
int pause; //is track paused?
int fin; //for interrupting audios
unsigned long pauseTime; //Way to track pause length
unsigned long timeUntilReset = 60000; //how long to wait before reset? (60s)
long currentPosition = 0;
long previousPosition = 0;
boolean printed = false;
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
// Use pins 2 and 3 to communicate with DFPlayer Mini
static const uint8_t PIN_MP3_TX = 6; // Connects to module's TX
static const uint8_t PIN_MP3_RX = 5; // Connects to module's RX
SoftwareSerial softwareSerial(PIN_MP3_RX, PIN_MP3_TX);
// Create the Player object
DFRobotDFPlayerMini myDFPlayer;
void setup() {
pinMode(relayPin, OUTPUT); // Set RelayPin as an output pin
pinMode(buttonPin, INPUT_PULLUP); // Set buttonPin as Input
softwareSerial.begin(9600); //Serial Port for DFPlayer Mini
if (myDFPlayer.begin(softwareSerial)) // Start communication with DFPlayer Mini
{
Serial.println("OK");
}
else {
Serial.println("Connecting to DFPlayer Mini failed!");
}
Serial.begin(9600);
}
void loop() {
int reading = digitalRead(buttonPin); //Read Button State
if (reading != lastButtonState) {
lastDebounceTime = millis(); // reset the debouncing timer
}
volumeControl();
if (myDFPlayer.available()) {
printDetail(myDFPlayer.readType(), myDFPlayer.read()); //Print the detail message from DFPlayer to handle different errors and states.
} //Quick check (is track finished?)
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading; //Set Buttonstate to current (i.e. 0 or 1)
if (buttonState == HIGH) { //if button pressed, do xyz:
if (playing == 1) { //If track is begun (paused or playing), do xyz:
if (pause == 1) {
myDFPlayer.advertise(3); //Play 'Resume'
delay(1000);
myDFPlayer.start(); // Resume from pause
pause = 2;
}
if (pause == 0) {
fin = 1;
myDFPlayer.stopAdvertise(); //stop advertise to allow 1st track continuation
myDFPlayer.advertise(2); //play 'pause'
delay(1000);
myDFPlayer.pause(); //pause the mp3
pauseTime = millis(); //Reset pause timer
pause = 1;
}
if (pause == 2) {
myDFPlayer.stopAdvertise(); //important so that 'track end' doesnt trigger with end of this track
pause = 0;
fin = 0; //also important, same reason
}
}
if (playing == 0) { //If track not playing, begin playback
myDFPlayer.playMp3Folder(1); // Play the "0001.mp3" in the "mp3" folder on the SD card
digitalWrite(relayPin, HIGH); //Turn Lights on
myDFPlayer.volume(15); // Set volume to maximum (0 to 30).
playing = 1;
}
}
}
}
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState = reading;
if ((pause == 1) && ((millis() - pauseTime) > timeUntilReset)) { //if its paused, check if pausetime has elapsed resetTime
playing = 0;
pause = 0;
myDFPlayer.pause(); //pause the mp3
digitalWrite(relayPin, LOW); //Turn Lights off
}
}
void volumeControl() {
long currentPosition = myEnc.read() / 4;
// If it goes beyond 30, set it back to 30
if (currentPosition > MAX_POSITION) {
currentPosition = MAX_POSITION;
myEnc.write(currentPosition * 4); // Update the encoder position
}
// If it goes below 0, set it back to 0
if (currentPosition < MIN_POSITION) {
currentPosition = MIN_POSITION;
myEnc.write(currentPosition * 4); // Update the encoder position
}
// Check if the position has changed
if (currentPosition != previousPosition) {
// Print the clamped position
Serial.println(currentPosition);
//Change Volume
myDFPlayer.volume(currentPosition);
// Update the previous position
previousPosition = currentPosition;
printed = true;
} else {
// If it hasn't changed, reset the printed flag
printed = false;
}
}
void printDetail(uint8_t type, int value) { //Controls various things depending on mp3 state
switch (type) {
case TimeOut:
Serial.println(F("Time Out!"));
break;
case WrongStack:
Serial.println(F("Stack Wrong!"));
break;
case DFPlayerCardInserted:
Serial.println(F("Card Inserted!"));
break;
case DFPlayerCardRemoved:
Serial.println(F("Card Removed!"));
break;
case DFPlayerCardOnline:
Serial.println(F("Card Online!"));
break;
case DFPlayerUSBInserted:
Serial.println("USB Inserted!");
break;
case DFPlayerUSBRemoved:
Serial.println("USB Removed!");
break;
case DFPlayerPlayFinished:
Serial.print(F("Number:"));
Serial.print(value);
Serial.println(F(" Play Finished!"));
if (fin != 1) {
playing = 0;
digitalWrite(relayPin, LOW); //Turn Lights off
myDFPlayer.pause(); //pause the mp3
}
break;
case DFPlayerError:
Serial.print(F("DFPlayerError:"));
switch (value) {
case Busy:
Serial.println(F("Card not found"));
break;
case Sleeping:
Serial.println(F("Sleeping"));
break;
case SerialWrongStack:
Serial.println(F("Get Wrong Stack"));
break;
case CheckSumNotMatch:
Serial.println(F("Check Sum Not Match"));
break;
case FileIndexOut:
Serial.println(F("File Index Out of Bound"));
break;
case FileMismatch:
Serial.println(F("Cannot Find File"));
break;
case Advertise:
Serial.println(F("In Advertise"));
break;
default:
break;
}
break;
default:
break;
}
}
`
The board is getting 5V and is wired up as 5V to 5V, GND pins to GND, D5 to a 1k ohm resistor to RX, D6 to TX
The text was updated successfully, but these errors were encountered:
Here is my code:
`#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
#include <Encoder.h>
int relayPin = 7; //Relay Control - Lights
const int buttonPin = 2; // the number of the pushbutton pin
Encoder myEnc(3, 4);
int encoderPos = 0;
const int MIN_POSITION = 0;
const int MAX_POSITION = 30;
boolean encoderALast = LOW; //remembers the previous state of the encoder pin A
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
int playing; //Track playing state
int pause; //is track paused?
int fin; //for interrupting audios
unsigned long pauseTime; //Way to track pause length
unsigned long timeUntilReset = 60000; //how long to wait before reset? (60s)
long currentPosition = 0;
long previousPosition = 0;
boolean printed = false;
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
// Use pins 2 and 3 to communicate with DFPlayer Mini
static const uint8_t PIN_MP3_TX = 6; // Connects to module's TX
static const uint8_t PIN_MP3_RX = 5; // Connects to module's RX
SoftwareSerial softwareSerial(PIN_MP3_RX, PIN_MP3_TX);
// Create the Player object
DFRobotDFPlayerMini myDFPlayer;
void setup() {
pinMode(relayPin, OUTPUT); // Set RelayPin as an output pin
pinMode(buttonPin, INPUT_PULLUP); // Set buttonPin as Input
softwareSerial.begin(9600); //Serial Port for DFPlayer Mini
if (myDFPlayer.begin(softwareSerial)) // Start communication with DFPlayer Mini
{
Serial.println("OK");
}
else {
Serial.println("Connecting to DFPlayer Mini failed!");
}
Serial.begin(9600);
}
void loop() {
int reading = digitalRead(buttonPin); //Read Button State
if (reading != lastButtonState) {
lastDebounceTime = millis(); // reset the debouncing timer
}
volumeControl();
if (myDFPlayer.available()) {
printDetail(myDFPlayer.readType(), myDFPlayer.read()); //Print the detail message from DFPlayer to handle different errors and states.
} //Quick check (is track finished?)
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
}
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState = reading;
if ((pause == 1) && ((millis() - pauseTime) > timeUntilReset)) { //if its paused, check if pausetime has elapsed resetTime
playing = 0;
pause = 0;
myDFPlayer.pause(); //pause the mp3
digitalWrite(relayPin, LOW); //Turn Lights off
}
}
void volumeControl() {
long currentPosition = myEnc.read() / 4;
// If it goes beyond 30, set it back to 30
if (currentPosition > MAX_POSITION) {
currentPosition = MAX_POSITION;
myEnc.write(currentPosition * 4); // Update the encoder position
}
// If it goes below 0, set it back to 0
if (currentPosition < MIN_POSITION) {
currentPosition = MIN_POSITION;
myEnc.write(currentPosition * 4); // Update the encoder position
}
// Check if the position has changed
if (currentPosition != previousPosition) {
// Print the clamped position
Serial.println(currentPosition);
//Change Volume
myDFPlayer.volume(currentPosition);
} else {
// If it hasn't changed, reset the printed flag
printed = false;
}
}
void printDetail(uint8_t type, int value) { //Controls various things depending on mp3 state
switch (type) {
case TimeOut:
Serial.println(F("Time Out!"));
break;
case WrongStack:
Serial.println(F("Stack Wrong!"));
break;
case DFPlayerCardInserted:
Serial.println(F("Card Inserted!"));
break;
case DFPlayerCardRemoved:
Serial.println(F("Card Removed!"));
break;
case DFPlayerCardOnline:
Serial.println(F("Card Online!"));
break;
case DFPlayerUSBInserted:
Serial.println("USB Inserted!");
break;
case DFPlayerUSBRemoved:
Serial.println("USB Removed!");
break;
case DFPlayerPlayFinished:
Serial.print(F("Number:"));
Serial.print(value);
Serial.println(F(" Play Finished!"));
if (fin != 1) {
playing = 0;
digitalWrite(relayPin, LOW); //Turn Lights off
myDFPlayer.pause(); //pause the mp3
}
break;
case DFPlayerError:
Serial.print(F("DFPlayerError:"));
switch (value) {
case Busy:
Serial.println(F("Card not found"));
break;
case Sleeping:
Serial.println(F("Sleeping"));
break;
case SerialWrongStack:
Serial.println(F("Get Wrong Stack"));
break;
case CheckSumNotMatch:
Serial.println(F("Check Sum Not Match"));
break;
case FileIndexOut:
Serial.println(F("File Index Out of Bound"));
break;
case FileMismatch:
Serial.println(F("Cannot Find File"));
break;
case Advertise:
Serial.println(F("In Advertise"));
break;
default:
break;
}
break;
default:
break;
}
}
`
The board is getting 5V and is wired up as 5V to 5V, GND pins to GND, D5 to a 1k ohm resistor to RX, D6 to TX
The text was updated successfully, but these errors were encountered: