Working version with PIR
This commit is contained in:
parent
a926f77fe8
commit
021cc8e73c
10 changed files with 652 additions and 49 deletions
3
vscode/singing_picture/.vscode/settings.json
vendored
Normal file
3
vscode/singing_picture/.vscode/settings.json
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"svn.ignoreMissingSvnWarning": true
|
||||
}
|
|
@ -13,7 +13,4 @@ platform = espressif32
|
|||
board = lolin32
|
||||
framework = arduino
|
||||
monitor_speed = 115200
|
||||
lib_deps =
|
||||
dfrobot/DFRobotDFPlayerMini@^1.0.5
|
||||
plerup/EspSoftwareSerial@^6.15.2
|
||||
board_build.f_cpu = 80000000L
|
||||
|
|
|
@ -1,54 +1,258 @@
|
|||
|
||||
#include "Arduino.h"
|
||||
#include "DFRobotDFPlayerMini.h"
|
||||
#include "SoftwareSerial.h"
|
||||
#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_VALID_NO_MOVEMEVENT 5000 //ms a no movement phase must be
|
||||
#define MS_TO_WAIT_BEFORE_NEXT_PRESS 200
|
||||
|
||||
#define BUTTON D1
|
||||
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]);
|
||||
|
||||
void play_sequence(void);
|
||||
void check_players_state(void);
|
||||
void check_button(void);
|
||||
void check_movement(void);
|
||||
void set_leds(void);
|
||||
void set_volume(void);
|
||||
|
||||
SoftwareSerial mySoftSer_1(13, 12); // RX, TX
|
||||
SoftwareSerial mySoftSer_2(14, 27);
|
||||
|
||||
DFRobotDFPlayerMini child_1;
|
||||
DFRobotDFPlayerMini child_2;
|
||||
|
||||
void setup() {
|
||||
|
||||
Serial.begin(115200); // start serial interface
|
||||
|
||||
if (!child_1.begin(mySoftSer_1, false)) { //Use softwareSerial to communicate with mp3.
|
||||
Serial.println(F("Unable to begin Child 1:"));
|
||||
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.
|
||||
}
|
||||
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);
|
||||
pinMode(blue_isplaying_led_pin, OUTPUT);
|
||||
pinMode(red_button_pressed_led_pin, OUTPUT);
|
||||
pinMode(red_movement_detected_led_pin, OUTPUT);
|
||||
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);
|
||||
}
|
||||
Serial.println(F("Child 1 online."));
|
||||
child_1.volume(10); //Set volume value. From 0 to 30
|
||||
|
||||
if (!child_2.begin(mySoftSer_2, false)) { //Use softwareSerial to communicate with mp3.
|
||||
Serial.println(F("Unable to begin Child 2:"));
|
||||
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("Child 2 online."));
|
||||
child_2.volume(10); //Set volume value. From 0 to 30
|
||||
|
||||
//WiFi.mode(WIFI_OFF);
|
||||
//WiFi.forceSleepBegin();
|
||||
delay(10);
|
||||
|
||||
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
child_1.play(1); //Play the first mp3
|
||||
child_1.readState()
|
||||
delay(1000);
|
||||
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_leds();
|
||||
set_volume();
|
||||
play_sequence();
|
||||
delay(100);
|
||||
}
|
||||
|
||||
void set_volume(void)
|
||||
{
|
||||
//if(is_playing == false)
|
||||
if(0 == 0)
|
||||
{
|
||||
if(button_downvol_pressed)
|
||||
{
|
||||
Serial.println("Decreasing volume ...");
|
||||
pinMode(vol_down_out, OUTPUT);
|
||||
digitalWrite(vol_down_out,LOW);
|
||||
delay(10000);
|
||||
digitalWrite(vol_down_out,HIGH);
|
||||
pinMode(vol_down_out, INPUT);
|
||||
Serial.println("Done.");
|
||||
}
|
||||
|
||||
if(button_upvol_pressed)
|
||||
{
|
||||
Serial.println("Increasing volume.");
|
||||
for(uint8_t ix= 0; ix < mp3player_nr; ix++)
|
||||
{
|
||||
pinMode(mp3player_control_pin[ix],OUTPUT);
|
||||
digitalWrite(mp3player_control_pin[ix],LOW);
|
||||
}
|
||||
delay(10000);
|
||||
for(uint8_t ix= 0; ix < mp3player_nr; ix++)
|
||||
{
|
||||
digitalWrite(mp3player_control_pin[ix],HIGH);
|
||||
pinMode(mp3player_control_pin[ix],INPUT);
|
||||
}
|
||||
Serial.println("Done.");
|
||||
}
|
||||
}
|
||||
button_downvol_pressed = false;
|
||||
button_upvol_pressed = false;
|
||||
}
|
||||
|
||||
|
||||
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 set_leds(void)
|
||||
{
|
||||
/*
|
||||
if( true == button_pressed)
|
||||
digitalWrite(red_button_pressed_led_pin,HIGH);
|
||||
else
|
||||
digitalWrite(red_button_pressed_led_pin,LOW);
|
||||
*/
|
||||
if( true == is_playing)
|
||||
digitalWrite(blue_isplaying_led_pin,HIGH);
|
||||
else
|
||||
digitalWrite(blue_isplaying_led_pin,LOW);
|
||||
|
||||
if( true == movement)
|
||||
digitalWrite(red_movement_detected_led_pin,HIGH);
|
||||
else
|
||||
{
|
||||
if(there_was_no_movement_in_between == true)
|
||||
digitalWrite(red_movement_detected_led_pin,LOW);
|
||||
}
|
||||
}
|
||||
|
||||
void check_movement(void)
|
||||
{
|
||||
int act_movement = digitalRead(movesensor_pin);
|
||||
/*if(act_movement == LOW)
|
||||
Serial.println("No movement.");
|
||||
else
|
||||
Serial.println("Movement.");
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue