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,20 +1,22 @@
#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_VALID_NO_MOVEMEVENT 10000 //ms a no movement phase must be
#define MS_TO_WAIT_BEFORE_NEXT_PRESS 200 #define MS_TO_WAIT_BEFORE_NEXT_PRESS 200
const uint8_t vol_poti_pin = 35; const uint8_t vol_poti_pin = 35;
const uint8_t vol_down_out = 17; const uint8_t vol_down_out = 17;
const uint8_t button_vol_down = 5; const uint8_t button_stop_pin = 18;
const uint8_t button_vol_up = 18; const uint8_t button_play_pin = 2;
const uint8_t button_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 blue_led_pin = 16;
const uint8_t mp3player_control_pin[] = {15,13,12,14,27,26,25,33,32}; 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]); const uint8_t mp3player_nr = sizeof(mp3player_control_pin)/sizeof(mp3player_control_pin[0]);
@ -31,16 +33,34 @@ void check_players_state(void);
void check_button(void); void check_button(void);
void check_movement(void); void check_movement(void);
void set_volume(void); void set_volume(void);
void set_leds(void);
void setup() void setup()
{ {
Serial.begin(115200); Serial.begin(115200);
Serial.println("Setting button,movement sensor pin to input."); Serial.println("Setting button,movement sensor pin to input.");
pinMode(vol_down_out, INPUT);
digitalWrite(vol_down_out,HIGH); digitalWrite(vol_down_out,HIGH);
pinMode(button_vol_up, INPUT_PULLUP); pinMode(button_stop_pin, INPUT_PULLUP);
pinMode(button_pin, INPUT_PULLUP); pinMode(button_play_pin, INPUT_PULLUP);
pinMode(movesensor_pin, INPUT_PULLDOWN); pinMode(movesensor_pin, INPUT_PULLDOWN);
pinMode(green_led_pin, OUTPUT);
pinMode(red_led_pin, OUTPUT);
pinMode(blue_led_pin, OUTPUT);
digitalWrite(red_led_pin, HIGH);
delay(500);
digitalWrite(red_led_pin, LOW);
delay(500);
digitalWrite(blue_led_pin,HIGH);
delay(500);
digitalWrite(blue_led_pin,LOW);
delay(500);
digitalWrite(green_led_pin,HIGH);
delay(500);
digitalWrite(green_led_pin,LOW);
delay(500);
Serial.println("Setting all control pins to outputs and disable playing."); Serial.println("Setting all control pins to outputs and disable playing.");
for(uint8_t ix = 0; ix < mp3player_nr; ix++) for(uint8_t ix = 0; ix < mp3player_nr; ix++)
{ {
@ -56,35 +76,42 @@ void setup()
Serial.println(F("1.Please recheck the connection!")); Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!")); Serial.println(F("2.Please insert the SD card!"));
while(true){ while(true){
delay(0); // Code to compatible with ESP8266 watch dog. digitalWrite(red_led_pin, HIGH);
delay(500);
digitalWrite(red_led_pin, LOW);
delay(500);
} }
} }
Serial.println(F("DFPlayer Mini online."));
myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD); // Setzt SD-Karte als Quelle
set_volume(); set_volume();
myDFPlayer.EQ(DFPLAYER_EQ_NORMAL); // Equalizer auf normal setzen
myDFPlayer.disableDAC();
myDFPlayer.stop(); // Sicherstellen, dass nichts ungewollt abgespielt wird
// myDFPlayer.sleep();
Serial.println(F("DFPlayer Mini online and sleeping."));
} }
bool movement = false; bool movement = false;
bool there_was_no_movement_in_between = true; bool there_was_no_movement_in_between = true;
bool is_playing = false; bool is_playing = false; // forced by software - signals that the play sequence is started
bool button_pressed = false; bool is_busy = false; // status of all the busy pins coming from the mp3players itself
bool button_upvol_pressed = false; bool button_play_pressed = false;
bool button_downvol_pressed = false; bool button_stop_pressed = false;
unsigned long last_move_ms = 0; unsigned long last_move_ms = 0;
unsigned long last_no_move_ms = 0; unsigned long last_no_move_ms = 0;
unsigned long last_button_ms = 0; bool movement_detected = false;
unsigned long last_upvol_button_ms = 0; bool next_playing_possible = false;
unsigned long last_downvol_button_ms = 0;
void loop() void loop()
{ {
check_players_state(); check_players_state();
check_movement(); check_movement();
check_button(); check_button();
set_leds();
set_volume(); set_volume();
play_sequence(); play_sequence();
delay(100); delay(100);
@ -97,8 +124,11 @@ void set_volume(void)
long vol_mp3 = map(vol_poti, 0, 4095, 2 , 30); long vol_mp3 = map(vol_poti, 0, 4095, 2 , 30);
if(last_vol != vol_mp3) 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 );
myDFPlayer.volume(vol_mp3); //Set volume value. From 0 to 30 myDFPlayer.volume(vol_mp3); //Set volume value. From 0 to 30
last_vol = vol_mp3; last_vol = vol_mp3;
// myDFPlayer.sleep();
} }
} }
@ -106,33 +136,27 @@ void set_volume(void)
void check_button(void) void check_button(void)
{ {
if( MS_TO_WAIT_BEFORE_NEXT_PRESS < millis() - last_button_ms) static unsigned long last_play_button_ms = 0;
static unsigned long last_stop_button_ms = 0;
unsigned long current_ms = millis();
if( MS_TO_WAIT_BEFORE_NEXT_PRESS < current_ms - last_play_button_ms)
{ {
if(false == button_pressed && LOW == digitalRead(button_pin)) if(false == button_play_pressed && LOW == digitalRead(button_play_pin))
{ {
Serial.println("Button pressed ..."); Serial.println("Play button pressed ...");
last_button_ms = millis(); last_play_button_ms = current_ms;
button_pressed = true; button_play_pressed = true;
} }
} }
if( MS_TO_WAIT_BEFORE_NEXT_PRESS < millis() - last_upvol_button_ms) if( MS_TO_WAIT_BEFORE_NEXT_PRESS < current_ms - last_stop_button_ms)
{ {
if(false == button_upvol_pressed && LOW == digitalRead(button_vol_up)) if(false == button_stop_pressed && LOW == digitalRead(button_stop_pin))
{ {
Serial.println("Volume up button pressed ..."); Serial.println("Stop button pressed ...");
last_upvol_button_ms = millis(); last_stop_button_ms = current_ms;
button_upvol_pressed = true; button_stop_pressed = true;
}
}
if( MS_TO_WAIT_BEFORE_NEXT_PRESS < millis() - last_downvol_button_ms)
{
if(false == button_downvol_pressed && LOW == digitalRead(button_vol_down))
{
Serial.println("Volume down button pressed ...");
last_downvol_button_ms = millis();
button_downvol_pressed = true;
} }
} }
@ -141,14 +165,27 @@ void check_button(void)
void check_movement(void) void check_movement(void)
{ {
int act_movement = digitalRead(movesensor_pin); int act_movement = digitalRead(movesensor_pin);
if(act_movement == HIGH)
Serial.println("Movement.");
// Serial.println("No movement.");
//else
// if(MS_TO_WAIT_BEFORE_NEXT_MOVE < millis()-last_move_ms)
{
if(next_playing_possible == false)
{
Serial.printf("Playing the sounds after next movement is possible now.\n");
}
next_playing_possible = true;
}
else
{
next_playing_possible = false;
}
if(LOW == act_movement ) if(LOW == act_movement )
{ {
if(movement_detected == true)
{
Serial.printf("Changed to No movement at %dms.\n", millis());
}
movement_detected = false;
if( false == there_was_no_movement_in_between) if( false == there_was_no_movement_in_between)
{ {
if(MS_TO_WAIT_BEFORE_VALID_NO_MOVEMEVENT < millis()-last_no_move_ms) if(MS_TO_WAIT_BEFORE_VALID_NO_MOVEMEVENT < millis()-last_no_move_ms)
@ -160,26 +197,34 @@ void check_movement(void)
} }
else else
{ {
if(movement_detected == false)
{
Serial.printf("Changed to Movement at %dms.\n", millis());
}
movement_detected = true;
if( false == is_playing)
{
if(HIGH == act_movement && true == there_was_no_movement_in_between)
{
Serial.printf("Movement detected after a while of no movemwnt (last movement before %dms)...\n", millis()-last_move_ms);
there_was_no_movement_in_between = false;
if(true == next_playing_possible)
// if(MS_TO_WAIT_BEFORE_NEXT_MOVE < millis()-last_move_ms)
{
last_move_ms = millis();
movement = true;
Serial.printf("New movement detected ...\n");
}
}
}
else
{
last_move_ms = millis();
}
last_no_move_ms = millis(); last_no_move_ms = millis();
} }
if( true == is_playing)
{
last_move_ms = millis();
}
else
{
if(HIGH == act_movement && true == there_was_no_movement_in_between)
{
there_was_no_movement_in_between = false;
if(MS_TO_WAIT_BEFORE_NEXT_MOVE < millis()-last_move_ms)
{
last_move_ms = millis();
movement = true;
Serial.printf("New movement detected ...\n");
}
}
}
} }
void check_players_state(void) void check_players_state(void)
@ -190,45 +235,105 @@ void check_players_state(void)
//Serial.printf("Voltage at busy pins is: dig=%d , ana=%dmV\n", voltage_dig, voltage_mV); //Serial.printf("Voltage at busy pins is: dig=%d , ana=%dmV\n", voltage_dig, voltage_mV);
if(voltage_mV < MV_ONE_IS_PLAYING) if(voltage_mV < MV_ONE_IS_PLAYING)
{ {
if(false == is_playing) if(false == is_busy)
{ {
Serial.println("At least one player is playing now."); Serial.println("At least one player is in busy mode (Playing or SD card problem?).");
} }
is_playing = true; is_busy = true;
} }
else else
{ {
if(true == is_playing) if(true == is_busy)
{ {
Serial.println("All players off now."); Serial.println("All players off now.");
} }
is_busy = false;
is_playing = false; is_playing = false;
myDFPlayer.stop();
// myDFPlayer.sleep();
} }
} }
void play_sequence(void) void play_sequence(void)
{ {
if(false == is_playing) static unsigned long start_playing_ms = 0;
if(true == button_stop_pressed)
{ {
//Serial.println("All players still off ."); myDFPlayer.stop();
if(true == button_pressed || true == movement ) // 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)
{ {
Serial.println("All players starting now."); if(MS_MAX_PLAYING_LENGTH < millis()-start_playing_ms){
for(uint8_t ix= 0; ix < mp3player_nr; ix++) is_playing = false;
myDFPlayer.stop();
// myDFPlayer.sleep();
}
}
else
{
//Serial.println("All players still off .");
if(true == button_play_pressed || true == movement )
{ {
pinMode(mp3player_control_pin[ix],OUTPUT); Serial.println("All players starting now.");
digitalWrite(mp3player_control_pin[ix],LOW); myDFPlayer.play();
Serial.println("All players started.");
start_playing_ms = millis();
is_playing = true;
} }
Serial.println("All players started.");
delay(500);
for(uint8_t ix= 0; ix < mp3player_nr; ix++)
{
digitalWrite(mp3player_control_pin[ix],HIGH);
pinMode(mp3player_control_pin[ix],INPUT);
}
is_playing = true;
} }
} }
button_pressed = false;
button_play_pressed = false;
button_stop_pressed = false;
movement = 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);
}