#include <EEPROM.h> #include <avr/sleep.h> #include <SoftwareSerial.h> SoftwareSerial mySoftwareSerial(10,11); #include "DFRobotDFPlayerMini.h" DFRobotDFPlayerMini myDFPlayer; unsigned long SEREN_time=0; uint8_t ZonaSeren=0, TrekIR=0, randNumber=0 ; uint8_t Step=4, fiasco = 0; bool Seren_activity=1; volatile bool FLG_Sleep =1; boolean flagStart=1; uint8_t address = 56; uint8_t addrs_test = 68; #define DEBUG_TO_COMPORT uint8_t Regim=3; #define SEREN 2 //#define RECVR 3 #define BUSY 9 #define Power 4 #define sbros_time 500 #define max_regim 5 #define volum 30 #define ALARM7 3 #define papkaN 1 #define trek_start 1 void myISR () {FLG_Sleep = 1;} void setup() { #ifdef DEBUG_TO_COMPORT Serial.begin(115200); mySoftwareSerial.begin (9600); Serial.println("start ?"); myDFPlayer.setTimeOut(500); #endif pinMode(BUSY, INPUT); pinMode(SEREN, INPUT); pinMode(Power, OUTPUT); digitalWrite(Power, HIGH); mySoftwareSerial.begin (9600); payza(1000); ADCSRA &= ~(1 << ADEN); if (!myDFPlayer.begin(mySoftwareSerial)) { Serial.println("Крах ?"); while(true); } #ifdef DEBUG_TO_COMPORT Serial.println("start OK"); myDFPlayer.setTimeOut(500); #endif myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD); myDFPlayer.volume(volum); myDFPlayer.EQ(DFPLAYER_EQ_POP); myDFPlayer.playFolder(papkaN, trek_start); payza(200); } void loop () { if (FLG_Sleep) { digitalWrite(Power, HIGH); if (!Seren_activity && digitalRead(SEREN)) { ZonaSeren++; Seren_activity=1; SEREN_time = millis(); } else if (Seren_activity && !digitalRead(SEREN)) Seren_activity=0; if (millis()-SEREN_time > sbros_time && ZonaSeren) { if(Regim) randNumber = Regim; else randNumber = random(0,max_regim); SEREN_time = millis(); if (!Seren_activity) myDFPlayer.playFolder(randNumber, ZonaSeren); else myDFPlayer.playFolder(randNumber, ALARM7); while( digitalRead(BUSY)&& millis()-SEREN_time < sbros_time) {} if (!digitalRead(BUSY)) ZonaSeren=0; } else if (digitalRead(BUSY)&& millis()-SEREN_time>sbros_time && !ZonaSeren && !digitalRead(SEREN)) OF_Power (); } if (!FLG_Sleep ) { attachInterrupt(digitalPinToInterrupt(SEREN),myISR, HIGH); set_sleep_mode(SLEEP_MODE_PWR_DOWN); sleep_mode(); detachInterrupt(digitalPinToInterrupt(SEREN)); } } void OF_Power (){ TrekIR=0; ZonaSeren=0; digitalWrite(Power, LOW); delay(50); FLG_Sleep = 0; } void payza(unsigned long _mls){ unsigned long _timeout = millis() + _mls; while ( millis() < _timeout) {}; }