I have been working on the code for a mp3 player prank. When finished it should play an mp3 file and then sleep for a random time, wake, play the file and then sleep a random time.
I have hit a brick wall, going round in circles and spending hours trying to get code to compile. At the moment on start up, I achieve sound... lots of times and then nothing. The random play doesn't seem to work.
I'm driving myself crazy, can anyone help?
#include <avr/sleep.h> //Needed for sleep_mode
#include <avr/wdt.h> //Needed to enable/disable watch dog timer
#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
int count = 0;
int randTime = 0;
#define RX 3 // *** D3, Pin 2
#define TX 4 // *** D4, Pin 3
SoftwareSerial softSerial(RX, TX); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
ISR(WDT_vect) {
}
void setup()
{
softSerial.begin(9600);
if (!myDFPlayer.begin(softSerial)) { //Use softwareSerial to communicate with mp3.
}
myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);
myDFPlayer.enableDAC();
delay(200);
myDFPlayer.volume(30); //Set volume value. From 0 to 30
myDFPlayer.play(1); //Play the first mp3
//}
ADCSRA &= ~(1<<ADEN); //Disable ADC
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
}
void loop()
{
setup_watchdog(9); //8sec sleep
sleep_mode();
if(count >= randTime){
randTime = random(255, 300);
count = 0;
myDFPlayer.enableDAC();
delay(200);
myDFPlayer.volume(30); //Set volume value. From 0 to 30
myDFPlayer.play(1);
if (myDFPlayer.available()) {
uint8_t type = myDFPlayer.readType();
if (type == DFPlayerPlayFinished)
{
myDFPlayer.disableDAC();
myDFPlayer.sleep();
}
}
}else{
count++;
}
}
//Sets the watchdog timer to wake us up, but not reset
//0=16ms, 1=32ms, 2=64ms, 3=128ms, 4=250ms, 5=500ms
//6=1sec, 7=2sec, 8=4sec, 9=8sec
//From: http://interface.khm.de/index.php/lab/experiments/sleep_watchdog_battery/
void setup_watchdog(int timerPrescaler) {
if (timerPrescaler > 9 ) timerPrescaler = 9; //Limit incoming amount to legal settings
byte bb = timerPrescaler & 7;
if (timerPrescaler > 7) bb |= (1<<5); //Set the special 5th bit if necessary
//This order of commands is important and cannot be combined
MCUSR &= ~(1<<WDRF); //Clear the watch dog reset
WDTCR |= (1<<WDCE) | (1<<WDE); //Set WD_change enable, set WD enable
WDTCR = bb; //Set new watchdog timeout value
WDTCR |= _BV(WDIE); //Set the interrupt enable, this will keep unit from resetting after each int
}