Im trying to make an annoying sound player using an attiny85 and df player. Ideally I want the attiny to sleep for a random time, wake and ask the df player to play a file.
I have some code assembled but I am not getting the file to play. Can anyone give me some guidance please? I'm very new to arduino and have no coding experience.
#include "DFRobotDFPlayerMini.h"
#include "SoftwareSerial.h"
#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/power.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
#include <util/delay.h>
#ifndef ARDUINO
#include <stdint.h>
#include "millionUtil.h" //not needed if compiled with Arduino & Arduino-Tiny
#endif
#define BODS 7 //BOD Sleep bit in MCUCR
#define BODSE 2 //BOD Sleep enable bit in MCUCR
#define RX 1 // *** Pin 1
#define TX 2 // *** Pin 2
SoftwareSerial softSerial(RX, TX); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
int PIN = 0;
int REGULAR_HI_MS = 800;
int WAKE_INDICATOR_HI_MS = 0; //200;
int INITIAL_BEEP_COUNT = 3; // number of "test" beeps before we go into the real loop
int RANDOM_SLEEP_MIN = 7; // 1 min (1 * 60 / 8)
int RANDOM_SLEEP_MAX = 225; // 30 mins
uint8_t mcucr1, mcucr2;
bool keepSleeping; //flag to keep sleeping or not
unsigned long msNow; //the current time from millis()
unsigned long msWakeUp; //the time we woke up
long wdtCount; //how many 8-sec WDT periods we've slept for
void setup()
{myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);
myDFPlayer.enableDAC();
delay(200);
myDFPlayer.volume(30); //Set volume value. From 0 to 30
for (int i=0; i < INITIAL_BEEP_COUNT - 1; i++) {
myDFPlayer.play(1);;
myDFPlayer.disableDAC();
myDFPlayer.sleep();
goToSleep(1);
}
}
void loop() {
myDFPlayer.play(1);
myDFPlayer.disableDAC();
myDFPlayer.sleep();
goToSleep(random(RANDOM_SLEEP_MIN, RANDOM_SLEEP_MAX + 1));
}
// wdtLimit = number of WDT periods to wake after
void goToSleep(long wdtLimit)
{
msNow = millis();
wdtCount = 0;
do {
ACSR |= _BV(ACD); //disable the analog comparator
ADCSRA &= ~_BV(ADEN); //disable ADC
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
wdtEnable(); //start the WDT
//turn off the brown-out detector.
//must have an ATtiny45 or ATtiny85 rev C or later for software to be able to disable the BOD.
//current while sleeping will be <0.5uA if BOD is disabled, <25uA if not.
cli();
mcucr1 = MCUCR | _BV(BODS) | _BV(BODSE); //turn off the brown-out detector
mcucr2 = mcucr1 & ~_BV(BODSE);
MCUCR = mcucr1;
MCUCR = mcucr2;
sei(); //ensure interrupts enabled so we can wake up again
sleep_cpu(); //go to sleep
//----zzzz----zzzz----zzzz----zzzz
cli(); //wake up here, disable interrupts
sleep_disable();
wdtDisable(); //don't need the watchdog while we're awake
sei(); //enable interrupts again (but INT0 is disabled above)
if (++wdtCount < wdtLimit) {
keepSleeping = true;
if (WAKE_INDICATOR_HI_MS > 0) {
(WAKE_INDICATOR_HI_MS); //briefly blink an LED so we can see the wdt wake-ups
}
}
else {
keepSleeping = false;
}
} while (keepSleeping);
msWakeUp = millis();
}
ISR(WDT_vect) {} //don't need to do anything here when the WDT wakes the MCU
//enable the WDT for 8sec interrupt
void wdtEnable(void)
{
wdt_reset();
cli();
MCUSR = 0x00;
WDTCR |= _BV(WDCE) | _BV(WDE);
WDTCR = _BV(WDIE) | _BV(WDP3) | _BV(WDP0); //8192ms
sei();
}
//disable the WDT
void wdtDisable(void)
{
wdt_reset();
cli();
MCUSR = 0x00;
WDTCR |= _BV(WDCE) | _BV(WDE);
WDTCR = 0x00;
sei();
}