Added volume adjustment via software serial.

This commit is contained in:
Jens Noack 2023-05-05 20:20:08 +02:00
parent 021cc8e73c
commit 83582e6420
10 changed files with 368 additions and 1 deletions

3
vscode/.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,3 @@
{
"svn.ignoreMissingSvnWarning": true
}

View file

@ -1,7 +1,7 @@
#include <Arduino.h>
#define MV_ONE_IS_PLAYING 3100
#define MS_TO_WAIT_BEFORE_NEXT_MOVE 10000 //ms to wait before new movement is valid
#define MS_TO_WAIT_BEFORE_NEXT_MOVE 120000 //ms to wait before new movement is valid
#define MS_TO_WAIT_BEFORE_VALID_NO_MOVEMEVENT 5000 //ms a no movement phase must be
#define MS_TO_WAIT_BEFORE_NEXT_PRESS 200

View file

@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch

View file

@ -0,0 +1,10 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
],
"unwantedRecommendations": [
"ms-vscode.cpptools-extension-pack"
]
}

View file

@ -0,0 +1,3 @@
{
"svn.ignoreMissingSvnWarning": true
}

View file

@ -0,0 +1,39 @@
This directory is intended for project header files.
A header file is a file containing C declarations and macro definitions
to be shared between several project source files. You request the use of a
header file in your project source file (C, C++, etc) located in `src` folder
by including it, with the C preprocessing directive `#include'.
```src/main.c
#include "header.h"
int main (void)
{
...
}
```
Including a header file produces the same results as copying the header file
into each source file that needs it. Such copying would be time-consuming
and error-prone. With a header file, the related declarations appear
in only one place. If they need to be changed, they can be changed in one
place, and programs that include the header file will automatically use the
new version when next recompiled. The header file eliminates the labor of
finding and changing all the copies as well as the risk that a failure to
find one copy will result in inconsistencies within a program.
In C, the usual convention is to give header files names that end with `.h'.
It is most portable to use only letters, digits, dashes, and underscores in
header file names, and at most one dot.
Read more about using header files in official GCC documentation:
* Include Syntax
* Include Operation
* Once-Only Headers
* Computed Includes
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html

View file

@ -0,0 +1,46 @@
This directory is intended for project specific (private) libraries.
PlatformIO will compile them to static libraries and link into executable file.
The source code of each library should be placed in a an own separate directory
("lib/your_library_name/[here are source files]").
For example, see a structure of the following two libraries `Foo` and `Bar`:
|--lib
| |
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
| |
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |
| |- README --> THIS FILE
|
|- platformio.ini
|--src
|- main.c
and a contents of `src/main.c`:
```
#include <Foo.h>
#include <Bar.h>
int main (void)
{
...
}
```
PlatformIO Library Dependency Finder will find automatically dependent
libraries scanning project source files.
More information about PlatformIO Library Dependency Finder
- https://docs.platformio.org/page/librarymanager/ldf.html

View file

@ -0,0 +1,16 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:lolin32]
platform = espressif32
board = lolin32
framework = arduino
monitor_speed = 115200
board_build.f_cpu = 80000000L

View file

@ -0,0 +1,234 @@
#include <Arduino.h>
#define MV_ONE_IS_PLAYING 3100
#define MS_TO_WAIT_BEFORE_NEXT_MOVE 120000 //ms to wait before new movement is valid
#define MS_TO_WAIT_BEFORE_VALID_NO_MOVEMEVENT 5000 //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 button_vol_down = 5;
const uint8_t button_vol_up = 18;
const uint8_t button_pin = 2;
const uint8_t movesensor_pin = 4;
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 red_movement_detected_led_pin = 19;
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"
SoftwareSerial mySoftwareSerial(19, 23); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void play_sequence(void);
void check_players_state(void);
void check_button(void);
void check_movement(void);
void set_volume(void);
void setup()
{
Serial.begin(115200);
Serial.println("Setting button,movement sensor pin to input.");
pinMode(vol_down_out, INPUT);
digitalWrite(vol_down_out,HIGH);
pinMode(button_vol_up, INPUT_PULLUP);
pinMode(button_pin, INPUT_PULLUP);
pinMode(movesensor_pin, INPUT_PULLDOWN);
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);
}
mySoftwareSerial.begin(9600);
myDFPlayer.begin(mySoftwareSerial);
if (!myDFPlayer.begin(mySoftwareSerial, false)) { //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){
delay(0); // Code to compatible with ESP8266 watch dog.
}
}
Serial.println(F("DFPlayer Mini online."));
set_volume();
}
bool movement = false;
bool there_was_no_movement_in_between = true;
bool is_playing = false;
bool button_pressed = false;
bool button_upvol_pressed = false;
bool button_downvol_pressed = false;
unsigned long last_move_ms = 0;
unsigned long last_no_move_ms = 0;
unsigned long last_button_ms = 0;
unsigned long last_upvol_button_ms = 0;
unsigned long last_downvol_button_ms = 0;
void loop()
{
check_players_state();
check_movement();
check_button();
set_volume();
play_sequence();
delay(100);
}
void set_volume(void)
{
static long last_vol = 0;
uint16_t vol_poti = analogRead(vol_poti_pin);
long vol_mp3 = map(vol_poti, 0, 4095, 2 , 30);
if(last_vol != vol_mp3)
{
myDFPlayer.volume(vol_mp3); //Set volume value. From 0 to 30
last_vol = vol_mp3;
}
}
void check_button(void)
{
if( MS_TO_WAIT_BEFORE_NEXT_PRESS < millis() - last_button_ms)
{
if(false == button_pressed && LOW == digitalRead(button_pin))
{
Serial.println("Button pressed ...");
last_button_ms = millis();
button_pressed = true;
}
}
if( MS_TO_WAIT_BEFORE_NEXT_PRESS < millis() - last_upvol_button_ms)
{
if(false == button_upvol_pressed && LOW == digitalRead(button_vol_up))
{
Serial.println("Volume up button pressed ...");
last_upvol_button_ms = millis();
button_upvol_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;
}
}
}
void check_movement(void)
{
int act_movement = digitalRead(movesensor_pin);
if(act_movement == HIGH)
Serial.println("Movement.");
// Serial.println("No movement.");
//else
//
if(LOW == act_movement )
{
if( false == there_was_no_movement_in_between)
{
if(MS_TO_WAIT_BEFORE_VALID_NO_MOVEMEVENT < millis()-last_no_move_ms)
{
there_was_no_movement_in_between = true;
Serial.printf("No movement anymore ...\n");
}
}
}
else
{
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)
{
// 0 ... 0V - 4095 ... 3.3V
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_playing)
{
Serial.println("At least one player is playing now.");
}
is_playing = true;
}
else
{
if(true == is_playing)
{
Serial.println("All players off now.");
}
is_playing = false;
}
}
void play_sequence(void)
{
if(false == is_playing)
{
//Serial.println("All players still off .");
if(true == button_pressed || true == movement )
{
Serial.println("All players starting now.");
for(uint8_t ix= 0; ix < mp3player_nr; ix++)
{
pinMode(mp3player_control_pin[ix],OUTPUT);
digitalWrite(mp3player_control_pin[ix],LOW);
}
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;
movement = false;
}

View file

@ -0,0 +1,11 @@
This directory is intended for PlatformIO Unit Testing and project tests.
Unit Testing is a software testing method by which individual units of
source code, sets of one or more MCU program modules together with associated
control data, usage procedures, and operating procedures, are tested to
determine whether they are fit for use. Unit testing finds problems early
in the development cycle.
More information about PlatformIO Unit Testing:
- https://docs.platformio.org/page/plus/unit-testing.html