added some dummy resistors to keep battery running
This commit is contained in:
parent
1eb5545465
commit
2dee384bf5
1 changed files with 79 additions and 17 deletions
|
@ -5,23 +5,35 @@
|
||||||
#define RX D2
|
#define RX D2
|
||||||
#define BUTTON D1
|
#define BUTTON D1
|
||||||
|
|
||||||
|
#define R1 D8
|
||||||
|
#define R2 D5
|
||||||
|
|
||||||
|
#define RED_LED D7
|
||||||
|
#define GREEN_LED D6
|
||||||
|
|
||||||
|
#define LDR_HIGH A0 //yellow
|
||||||
|
|
||||||
#define STARTED_STRING "Status: playing"
|
#define STARTED_STRING "Status: playing"
|
||||||
#define STOPPED_STRING "Status: stopped"
|
#define STOPPED_STRING "Status: stopped"
|
||||||
#define MAX_PLAYTIME 60000
|
#define MAX_PLAYTIME 25000
|
||||||
|
|
||||||
unsigned long lastpressed = 0;
|
unsigned long lastpressed = 0;
|
||||||
unsigned long pressed_cycles = 0;
|
unsigned long pressed_cycles = 0;
|
||||||
unsigned long lastreleased = 0;
|
unsigned long lastreleased = 0;
|
||||||
const unsigned long wait_until_next_pressed = 100;
|
const unsigned long wait_until_next_pressed = 100;
|
||||||
|
|
||||||
|
unsigned long last_res = millis();
|
||||||
|
bool is_res = false;
|
||||||
|
|
||||||
SerialMP3Player mp3(RX,TX);
|
SerialMP3Player mp3(RX,TX);
|
||||||
|
|
||||||
bool button_pressed(unsigned int button_pin){
|
bool button_pressed(unsigned int button_pin){
|
||||||
static bool buttons_state = false;
|
static bool buttons_state = false;
|
||||||
if(millis() - lastpressed > wait_until_next_pressed)
|
if(millis() - lastpressed > wait_until_next_pressed)
|
||||||
{
|
{
|
||||||
if(digitalRead(button_pin) == LOW)
|
if(digitalRead(button_pin) == HIGH)
|
||||||
{
|
{
|
||||||
|
//Serial.printf("PRESSED\n");
|
||||||
pressed_cycles++;
|
pressed_cycles++;
|
||||||
lastpressed = millis();
|
lastpressed = millis();
|
||||||
buttons_state = true;
|
buttons_state = true;
|
||||||
|
@ -39,36 +51,88 @@ void setup() {
|
||||||
|
|
||||||
Serial.begin(115200); // start serial interface
|
Serial.begin(115200); // start serial interface
|
||||||
pinMode(BUTTON,INPUT_PULLUP);
|
pinMode(BUTTON,INPUT_PULLUP);
|
||||||
|
pinMode(RED_LED,OUTPUT);
|
||||||
|
pinMode(GREEN_LED,OUTPUT);
|
||||||
|
pinMode(R1,OUTPUT);
|
||||||
|
pinMode(R2,OUTPUT);
|
||||||
|
|
||||||
|
digitalWrite(R1, LOW);
|
||||||
|
digitalWrite(R2, LOW);
|
||||||
|
|
||||||
|
digitalWrite(RED_LED, LOW);
|
||||||
|
digitalWrite(GREEN_LED, HIGH);
|
||||||
|
|
||||||
WiFi.mode(WIFI_OFF);
|
WiFi.mode(WIFI_OFF);
|
||||||
WiFi.forceSleepBegin();
|
WiFi.forceSleepBegin();
|
||||||
delay(10);
|
delay(10);
|
||||||
|
|
||||||
mp3.begin(9600); // start mp3-communication
|
mp3.begin(9600); // start mp3-communication
|
||||||
delay(500); // wait for init
|
|
||||||
|
|
||||||
|
|
||||||
mp3.setVol(25);
|
mp3.setVol(25);
|
||||||
delay(500);
|
|
||||||
|
|
||||||
mp3.sendCommand(CMD_SEL_DEV, 0, 2); //select sd-card
|
mp3.sendCommand(CMD_SEL_DEV, 0, 2); //select sd-card
|
||||||
delay(500); // wait for init
|
delay(50); // wait for init
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool was_high = false;
|
||||||
|
void check_battery()
|
||||||
|
{
|
||||||
|
|
||||||
|
int is_high = analogRead(LDR_HIGH);
|
||||||
|
//Serial.printf("IS HIGH: %d \n", is_high);
|
||||||
|
|
||||||
|
if(is_high>50)
|
||||||
|
{
|
||||||
|
if(false == was_high)
|
||||||
|
{
|
||||||
|
digitalWrite(RED_LED, LOW);
|
||||||
|
digitalWrite(GREEN_LED, HIGH);
|
||||||
|
was_high = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if(true == was_high)
|
||||||
|
{
|
||||||
|
digitalWrite(RED_LED, HIGH);
|
||||||
|
digitalWrite(GREEN_LED, LOW);
|
||||||
|
was_high = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_dummy_res()
|
||||||
|
{
|
||||||
|
if(millis() > last_res + 60000)
|
||||||
|
{
|
||||||
|
last_res = millis();
|
||||||
|
is_res = true;
|
||||||
|
digitalWrite(R1, HIGH);
|
||||||
|
digitalWrite(R2, HIGH);
|
||||||
|
}
|
||||||
|
if(is_res == true && millis() > last_res + 5000)
|
||||||
|
{
|
||||||
|
is_res = false;
|
||||||
|
digitalWrite(R1, LOW);
|
||||||
|
digitalWrite(R2, LOW);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// the loop function runs over and over again forever
|
// the loop function runs over and over again forever
|
||||||
unsigned long started_plying = 0;
|
unsigned long started_plying = 0;
|
||||||
bool playing = false;
|
bool playing = false;
|
||||||
void loop() {
|
void loop() {
|
||||||
|
|
||||||
|
set_dummy_res();
|
||||||
|
|
||||||
|
check_battery();
|
||||||
|
|
||||||
if(true == button_pressed(BUTTON))
|
if(true == button_pressed(BUTTON))
|
||||||
{
|
{
|
||||||
playing = false;
|
playing = false;
|
||||||
mp3.play(1);
|
mp3.play(1);
|
||||||
started_plying = millis();
|
started_plying = millis();
|
||||||
Serial.println("");
|
//Serial.println("");
|
||||||
Serial.println("Song playing started!");
|
//Serial.println("Song playing started!");
|
||||||
while((playing == false) && (millis()-started_plying < MAX_PLAYTIME))
|
while((playing == false) && (millis()-started_plying < MAX_PLAYTIME))
|
||||||
{
|
{
|
||||||
delay(100);
|
delay(100);
|
||||||
|
@ -78,11 +142,10 @@ void loop() {
|
||||||
if(answer.indexOf(STARTED_STRING) > -1)
|
if(answer.indexOf(STARTED_STRING) > -1)
|
||||||
{
|
{
|
||||||
playing = true;
|
playing = true;
|
||||||
Serial.println("Song started!");
|
//Serial.println("Song started!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
while((playing == true) && (millis()-started_plying < MAX_PLAYTIME))
|
while((playing == true) && (millis()-started_plying < MAX_PLAYTIME))
|
||||||
{
|
{
|
||||||
delay(100);
|
delay(100);
|
||||||
|
@ -92,15 +155,14 @@ void loop() {
|
||||||
if(answer.indexOf(STOPPED_STRING) > -1)
|
if(answer.indexOf(STOPPED_STRING) > -1)
|
||||||
{
|
{
|
||||||
playing = false;
|
playing = false;
|
||||||
Serial.println("Song finished!");
|
//Serial.println("Song finished!");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(playing == true)
|
if(playing == true)
|
||||||
mp3.stop();
|
mp3.stop();
|
||||||
Serial.println("Song playing done!");
|
//Serial.println("Song playing done!");
|
||||||
|
|
||||||
while(true == button_pressed(BUTTON))
|
while(true == button_pressed(BUTTON))
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue