modified timeout and DFPlayer handling

This commit is contained in:
Jens Noack 2025-03-05 16:18:52 +01:00
parent 83582e6420
commit 9be74cc721
3 changed files with 397 additions and 235 deletions

BIN
mp3files/Nr5.mp3 Normal file

Binary file not shown.

View file

@ -1,3 +1,60 @@
{ {
"svn.ignoreMissingSvnWarning": true "svn.ignoreMissingSvnWarning": true,
"C_Cpp_Runner.cCompilerPath": "c:/wingw64/bin/gcc.exe",
"C_Cpp_Runner.cppCompilerPath": "g++",
"C_Cpp_Runner.debuggerPath": "c:/wingw64/bin/gdb.exe",
"C_Cpp_Runner.cStandard": "",
"C_Cpp_Runner.cppStandard": "",
"C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/VR_NR/Community/VC/Auxiliary/Build/vcvarsall.bat",
"C_Cpp_Runner.useMsvc": false,
"C_Cpp_Runner.warnings": [
"-Wall",
"-Wextra",
"-Wpedantic",
"-Wshadow",
"-Wformat=2",
"-Wcast-align",
"-Wconversion",
"-Wsign-conversion",
"-Wnull-dereference"
],
"C_Cpp_Runner.msvcWarnings": [
"/W4",
"/permissive-",
"/w14242",
"/w14287",
"/w14296",
"/w14311",
"/w14826",
"/w44062",
"/w44242",
"/w14905",
"/w14906",
"/w14263",
"/w44265",
"/w14928"
],
"C_Cpp_Runner.enableWarnings": true,
"C_Cpp_Runner.warningsAsError": false,
"C_Cpp_Runner.compilerArgs": [],
"C_Cpp_Runner.linkerArgs": [],
"C_Cpp_Runner.includePaths": [],
"C_Cpp_Runner.includeSearch": [
"*",
"**/*"
],
"C_Cpp_Runner.excludeSearch": [
"**/build",
"**/build/**",
"**/.*",
"**/.*/**",
"**/.vscode",
"**/.vscode/**"
],
"C_Cpp_Runner.useAddressSanitizer": false,
"C_Cpp_Runner.useUndefinedSanitizer": false,
"C_Cpp_Runner.useLeakSanitizer": false,
"C_Cpp_Runner.showCompilationTime": false,
"C_Cpp_Runner.useLinkTimeOptimization": false,
"C_Cpp_Runner.msvcSecureNoWarnings": false
} }

View file

@ -1,234 +1,339 @@
#include <Arduino.h> #include <Arduino.h>
#define MV_ONE_IS_PLAYING 3100 #define MV_ONE_IS_PLAYING 3100 //voltage level in mV of the busy signal - if there is one player is playing
#define MS_TO_WAIT_BEFORE_NEXT_MOVE 120000 //ms to wait before new movement is valid #define MS_MAX_PLAYING_LENGTH 60000 //if there is one SD card signaling busy all the time - this is the time out for the play detection
#define MS_TO_WAIT_BEFORE_VALID_NO_MOVEMEVENT 5000 //ms a no movement phase must be #define MS_TO_WAIT_BEFORE_NEXT_MOVE 120000 //ms to wait before new movement is valid
#define MS_TO_WAIT_BEFORE_NEXT_PRESS 200 #define MS_TO_WAIT_BEFORE_VALID_NO_MOVEMEVENT 10000 //ms a no movement phase must be
#define MS_TO_WAIT_BEFORE_NEXT_PRESS 200
const uint8_t vol_poti_pin = 35;
const uint8_t vol_down_out = 17; const uint8_t vol_poti_pin = 35;
const uint8_t button_vol_down = 5; const uint8_t vol_down_out = 17;
const uint8_t button_vol_up = 18; const uint8_t button_stop_pin = 18;
const uint8_t button_pin = 2; const uint8_t button_play_pin = 2;
const uint8_t movesensor_pin = 4; const uint8_t movesensor_pin = 4;
const uint8_t busyanalog_pin = 36; // to get status of all players const uint8_t busyanalog_pin = 36; // to get status of all players
const uint8_t blue_isplaying_led_pin = 22;
const uint8_t red_button_pressed_led_pin = 19; const uint8_t green_led_pin = 5;
const uint8_t red_movement_detected_led_pin = 19; const uint8_t red_led_pin = 0;
const uint8_t mp3player_control_pin[] = {15,13,12,14,27,26,25,33,32}; const uint8_t blue_led_pin = 16;
const uint8_t mp3player_nr = sizeof(mp3player_control_pin)/sizeof(mp3player_control_pin[0]);
const uint8_t mp3player_control_pin[] = {15,13,12,14,27,26,25,33,32};
const uint8_t mp3player_nr = sizeof(mp3player_control_pin)/sizeof(mp3player_control_pin[0]);
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
#include "SoftwareSerial.h"
SoftwareSerial mySoftwareSerial(19, 23); // RX, TX #include "DFRobotDFPlayerMini.h"
DFRobotDFPlayerMini myDFPlayer;
SoftwareSerial mySoftwareSerial(19, 23); // RX, TX
void play_sequence(void); DFRobotDFPlayerMini myDFPlayer;
void check_players_state(void);
void check_button(void); void play_sequence(void);
void check_movement(void); void check_players_state(void);
void set_volume(void); void check_button(void);
void check_movement(void);
void setup() void set_volume(void);
{ void set_leds(void);
Serial.begin(115200);
Serial.println("Setting button,movement sensor pin to input."); void setup()
pinMode(vol_down_out, INPUT); {
digitalWrite(vol_down_out,HIGH); Serial.begin(115200);
pinMode(button_vol_up, INPUT_PULLUP); Serial.println("Setting button,movement sensor pin to input.");
pinMode(button_pin, INPUT_PULLUP); digitalWrite(vol_down_out,HIGH);
pinMode(movesensor_pin, INPUT_PULLDOWN); pinMode(button_stop_pin, INPUT_PULLUP);
Serial.println("Setting all control pins to outputs and disable playing."); pinMode(button_play_pin, INPUT_PULLUP);
for(uint8_t ix = 0; ix < mp3player_nr; ix++) pinMode(movesensor_pin, INPUT_PULLDOWN);
{
pinMode(mp3player_control_pin[ix], OUTPUT); pinMode(green_led_pin, OUTPUT);
digitalWrite(mp3player_control_pin[ix],HIGH); pinMode(red_led_pin, OUTPUT);
pinMode(mp3player_control_pin[ix], INPUT); pinMode(blue_led_pin, OUTPUT);
}
digitalWrite(red_led_pin, HIGH);
mySoftwareSerial.begin(9600); delay(500);
myDFPlayer.begin(mySoftwareSerial); digitalWrite(red_led_pin, LOW);
if (!myDFPlayer.begin(mySoftwareSerial, false)) { //Use softwareSerial to communicate with mp3. delay(500);
Serial.println(F("Unable to begin:")); digitalWrite(blue_led_pin,HIGH);
Serial.println(F("1.Please recheck the connection!")); delay(500);
Serial.println(F("2.Please insert the SD card!")); digitalWrite(blue_led_pin,LOW);
while(true){ delay(500);
delay(0); // Code to compatible with ESP8266 watch dog. digitalWrite(green_led_pin,HIGH);
} delay(500);
} digitalWrite(green_led_pin,LOW);
Serial.println(F("DFPlayer Mini online.")); delay(500);
set_volume();
Serial.println("Setting all control pins to outputs and disable playing.");
for(uint8_t ix = 0; ix < mp3player_nr; ix++)
{
pinMode(mp3player_control_pin[ix], OUTPUT);
digitalWrite(mp3player_control_pin[ix],HIGH);
} pinMode(mp3player_control_pin[ix], INPUT);
}
bool movement = false;
bool there_was_no_movement_in_between = true; mySoftwareSerial.begin(9600);
bool is_playing = false; myDFPlayer.begin(mySoftwareSerial);
bool button_pressed = false; if (!myDFPlayer.begin(mySoftwareSerial, false)) { //Use softwareSerial to communicate with mp3.
bool button_upvol_pressed = false; Serial.println(F("Unable to begin:"));
bool button_downvol_pressed = false; Serial.println(F("1.Please recheck the connection!"));
unsigned long last_move_ms = 0; Serial.println(F("2.Please insert the SD card!"));
unsigned long last_no_move_ms = 0; while(true){
unsigned long last_button_ms = 0; digitalWrite(red_led_pin, HIGH);
unsigned long last_upvol_button_ms = 0; delay(500);
unsigned long last_downvol_button_ms = 0; digitalWrite(red_led_pin, LOW);
delay(500);
void loop() }
{ }
check_players_state();
check_movement(); myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD); // Setzt SD-Karte als Quelle
check_button(); set_volume();
set_volume(); myDFPlayer.EQ(DFPLAYER_EQ_NORMAL); // Equalizer auf normal setzen
play_sequence(); myDFPlayer.disableDAC();
delay(100); myDFPlayer.stop(); // Sicherstellen, dass nichts ungewollt abgespielt wird
} // myDFPlayer.sleep();
Serial.println(F("DFPlayer Mini online and sleeping."));
void set_volume(void)
{ }
static long last_vol = 0;
uint16_t vol_poti = analogRead(vol_poti_pin); bool movement = false;
long vol_mp3 = map(vol_poti, 0, 4095, 2 , 30); bool there_was_no_movement_in_between = true;
if(last_vol != vol_mp3) bool is_playing = false; // forced by software - signals that the play sequence is started
{ bool is_busy = false; // status of all the busy pins coming from the mp3players itself
myDFPlayer.volume(vol_mp3); //Set volume value. From 0 to 30 bool button_play_pressed = false;
last_vol = vol_mp3; bool button_stop_pressed = false;
} unsigned long last_move_ms = 0;
unsigned long last_no_move_ms = 0;
} bool movement_detected = false;
bool next_playing_possible = false;
void check_button(void)
{
if( MS_TO_WAIT_BEFORE_NEXT_PRESS < millis() - last_button_ms) void loop()
{ {
if(false == button_pressed && LOW == digitalRead(button_pin)) check_players_state();
{ check_movement();
Serial.println("Button pressed ..."); check_button();
last_button_ms = millis(); set_leds();
button_pressed = true; set_volume();
} play_sequence();
} delay(100);
}
if( MS_TO_WAIT_BEFORE_NEXT_PRESS < millis() - last_upvol_button_ms)
{ void set_volume(void)
if(false == button_upvol_pressed && LOW == digitalRead(button_vol_up)) {
{ static long last_vol = 0;
Serial.println("Volume up button pressed ..."); uint16_t vol_poti = analogRead(vol_poti_pin);
last_upvol_button_ms = millis(); long vol_mp3 = map(vol_poti, 0, 4095, 2 , 30);
button_upvol_pressed = true; if(last_vol != vol_mp3)
} {
} digitalWrite(green_led_pin,LOW);
Serial.printf("Over all Volume set from %d to %d \n", last_vol, vol_mp3 );
if( MS_TO_WAIT_BEFORE_NEXT_PRESS < millis() - last_downvol_button_ms) myDFPlayer.volume(vol_mp3); //Set volume value. From 0 to 30
{ last_vol = vol_mp3;
if(false == button_downvol_pressed && LOW == digitalRead(button_vol_down)) // myDFPlayer.sleep();
{ }
Serial.println("Volume down button pressed ...");
last_downvol_button_ms = millis(); }
button_downvol_pressed = true;
}
} void check_button(void)
{
} static unsigned long last_play_button_ms = 0;
static unsigned long last_stop_button_ms = 0;
void check_movement(void)
{ unsigned long current_ms = millis();
int act_movement = digitalRead(movesensor_pin); if( MS_TO_WAIT_BEFORE_NEXT_PRESS < current_ms - last_play_button_ms)
if(act_movement == HIGH) {
Serial.println("Movement."); if(false == button_play_pressed && LOW == digitalRead(button_play_pin))
// Serial.println("No movement."); {
//else Serial.println("Play button pressed ...");
last_play_button_ms = current_ms;
// button_play_pressed = true;
if(LOW == act_movement ) }
{ }
if( false == there_was_no_movement_in_between)
{ if( MS_TO_WAIT_BEFORE_NEXT_PRESS < current_ms - last_stop_button_ms)
if(MS_TO_WAIT_BEFORE_VALID_NO_MOVEMEVENT < millis()-last_no_move_ms) {
{ if(false == button_stop_pressed && LOW == digitalRead(button_stop_pin))
there_was_no_movement_in_between = true; {
Serial.printf("No movement anymore ...\n"); Serial.println("Stop button pressed ...");
} last_stop_button_ms = current_ms;
} button_stop_pressed = true;
} }
else }
{
last_no_move_ms = millis(); }
}
void check_movement(void)
if( true == is_playing) {
{ int act_movement = digitalRead(movesensor_pin);
last_move_ms = millis();
} if(MS_TO_WAIT_BEFORE_NEXT_MOVE < millis()-last_move_ms)
else {
{ if(next_playing_possible == false)
if(HIGH == act_movement && true == there_was_no_movement_in_between) {
{ Serial.printf("Playing the sounds after next movement is possible now.\n");
there_was_no_movement_in_between = false; }
if(MS_TO_WAIT_BEFORE_NEXT_MOVE < millis()-last_move_ms) next_playing_possible = true;
{ }
last_move_ms = millis(); else
movement = true; {
Serial.printf("New movement detected ...\n"); next_playing_possible = false;
} }
}
} if(LOW == act_movement )
} {
if(movement_detected == true)
void check_players_state(void) {
{ Serial.printf("Changed to No movement at %dms.\n", millis());
// 0 ... 0V - 4095 ... 3.3V }
uint16_t voltage_dig = analogRead(busyanalog_pin); movement_detected = false;
uint16_t voltage_mV = map(voltage_dig,0,4095,0,3300); if( false == there_was_no_movement_in_between)
//Serial.printf("Voltage at busy pins is: dig=%d , ana=%dmV\n", voltage_dig, voltage_mV); {
if(voltage_mV < MV_ONE_IS_PLAYING) if(MS_TO_WAIT_BEFORE_VALID_NO_MOVEMEVENT < millis()-last_no_move_ms)
{ {
if(false == is_playing) there_was_no_movement_in_between = true;
{ Serial.printf("No movement anymore ...\n");
Serial.println("At least one player is playing now."); }
} }
is_playing = true; }
} else
else {
{ if(movement_detected == false)
if(true == is_playing) {
{ Serial.printf("Changed to Movement at %dms.\n", millis());
Serial.println("All players off now."); }
} movement_detected = true;
is_playing = false;
} if( false == is_playing)
} {
if(HIGH == act_movement && true == there_was_no_movement_in_between)
void play_sequence(void) {
{ Serial.printf("Movement detected after a while of no movemwnt (last movement before %dms)...\n", millis()-last_move_ms);
if(false == is_playing) there_was_no_movement_in_between = false;
{ if(true == next_playing_possible)
//Serial.println("All players still off ."); // if(MS_TO_WAIT_BEFORE_NEXT_MOVE < millis()-last_move_ms)
if(true == button_pressed || true == movement ) {
{ last_move_ms = millis();
Serial.println("All players starting now."); movement = true;
for(uint8_t ix= 0; ix < mp3player_nr; ix++) Serial.printf("New movement detected ...\n");
{ }
pinMode(mp3player_control_pin[ix],OUTPUT); }
digitalWrite(mp3player_control_pin[ix],LOW); }
} else
Serial.println("All players started."); {
delay(500); last_move_ms = millis();
for(uint8_t ix= 0; ix < mp3player_nr; ix++) }
{ last_no_move_ms = millis();
digitalWrite(mp3player_control_pin[ix],HIGH); }
pinMode(mp3player_control_pin[ix],INPUT);
} }
is_playing = true;
} void check_players_state(void)
} {
button_pressed = false; // 0 ... 0V - 4095 ... 3.3V
movement = false; uint16_t voltage_dig = analogRead(busyanalog_pin);
} uint16_t voltage_mV = map(voltage_dig,0,4095,0,3300);
//Serial.printf("Voltage at busy pins is: dig=%d , ana=%dmV\n", voltage_dig, voltage_mV);
if(voltage_mV < MV_ONE_IS_PLAYING)
{
if(false == is_busy)
{
Serial.println("At least one player is in busy mode (Playing or SD card problem?).");
}
is_busy = true;
}
else
{
if(true == is_busy)
{
Serial.println("All players off now.");
}
is_busy = false;
is_playing = false;
myDFPlayer.stop();
// myDFPlayer.sleep();
}
}
void play_sequence(void)
{
static unsigned long start_playing_ms = 0;
if(true == button_stop_pressed)
{
myDFPlayer.stop();
// myDFPlayer.sleep();
is_playing = false;
last_move_ms = 0;
there_was_no_movement_in_between = true;
Serial.println("All players stopped.");
}
else
{
if(true == is_playing)
{
if(MS_MAX_PLAYING_LENGTH < millis()-start_playing_ms){
is_playing = false;
myDFPlayer.stop();
// myDFPlayer.sleep();
}
}
else
{
//Serial.println("All players still off .");
if(true == button_play_pressed || true == movement )
{
Serial.println("All players starting now.");
myDFPlayer.play();
Serial.println("All players started.");
start_playing_ms = millis();
is_playing = true;
}
}
}
button_play_pressed = false;
button_stop_pressed = false;
movement = false;
}
void set_leds(void)
{
static unsigned long last_update_blue_led = 0;
bool next_play_possible = false;
if(millis() - last_update_blue_led > 1000)
{
next_play_possible = next_playing_possible;
last_update_blue_led = millis();
}
if(is_playing == true)
{
digitalWrite(blue_led_pin, HIGH);
}
else
{
if(next_play_possible == true)
{
digitalWrite(blue_led_pin, HIGH);
}
else
{
digitalWrite(blue_led_pin, LOW);
}
}
if(there_was_no_movement_in_between == false)
{
digitalWrite(red_led_pin, HIGH);
}
else
{
digitalWrite(red_led_pin, LOW);
}
digitalWrite(green_led_pin,HIGH);
}