Puzzler with Snooze and wav player

Status
Not open for further replies.

snusmumriken

Active member
I'm using a Teensy 3.2 with audio board to play long WAV files (>1 min). The playing part works fine. Between plays, I want the processor to sleep for a pre-determined interval which in the final version is likely to be in the order of 15-30 minutes. I'm using the timer in the Snooze library to wake the device at intervals of 5000ms (can it be longer?) and check against the RTC whether it is now time to play, else go back to sleep. I thought I had written the code so it would play the file to the end before going back to sleep.

What actually seems to happen is that the processor wakes at intervals and plays only a fragment of the WAV file before going back to sleep. It doesn't seem to go back to the beginning of the WAV file.

As others have noted, when the processor sleeps, the device becomes invisible to Windows 10 and/or the Arduino IDE, so it's difficult to check that the code is working properly, or even to upload an improved version. However, when running on a battery supply, the problem persists, so the code isn't working how I expected.

Can anyone help, please?

Code:
/*************************************************************************************************
 Call libraries
*************************************************************************************************/
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
#include <Snooze.h>
#include <TimeLib.h>
#include <DS1307RTC.h>  // basic DS1307 library that returns time as a time_t variable

/*********************************************************************************************************************************
 Define global variables
*********************************************************************************************************************************/

time_t timeNow = 1357041600; // Initialised to Jan 1 2013 as recognisable nonsense value;

unsigned long startTime = 0;   // time_t variable for time on entering activation window.
unsigned long endTime = 0;     // time_t variable for time on entering pause.
unsigned long prevTime = 0;    // time_t variable for time at start of previous activation window.
unsigned long diffSec = 0;     // time_t variable for difference between startTime and prevTimetime left before waking.
unsigned long remainSec = 0;   // time_t variable for time left before waking.

int silentMin = 1;             // Determines how long in whole minutes the unit should sit inactive between play events
unsigned long silentSec = silentMin*60;  // Defines silent interval in seconds

byte event = 9; // Stores type of event to log. 0=Device started; 1=start of play event; 2=end of play event. Initialised with nonsense value. 
File logFile;   // File for storing event data.
const char* wavFile[] = {"","SDTEST1","SDTEST1","SDTEST1","SDTEST1"); //  Sound files on SD card, to be populated later.  Indices start at 0, which is left empty.

/*************************************************************************************************
 Make Snooze timer available and load it to a Snoozeblock
*************************************************************************************************/
SnoozeTimer timer;
SnoozeBlock config(timer);

/*************************************************************************************************
 Set up audio system
*************************************************************************************************/
AudioPlaySdWav           playWav1;
AudioOutputI2S           audioOutput;
AudioConnection          patchCord1(playWav1, 0, audioOutput, 0); // Not sure whether I need these 2 lines?
AudioConnection          patchCord2(playWav1, 1, audioOutput, 1); // Not sure whether I need these 2 lines?
AudioControlSGTL5000     sgtl5000_1;

// Connections for the Teensy Audio Shield
#define SDCARD_CS_PIN    10
#define SDCARD_MOSI_PIN  7
#define SDCARD_SCK_PIN   14
#define SPKR_CNTRL  2

/*************************************************************************************************
 Set-up loop
*************************************************************************************************/
void setup() {
  
  setSyncProvider(getTeensy3Time);  // sets the Time library to use the RTC on board the Teensyto keep time

  Serial.begin(9600);

  pinMode(LED_BUILTIN, OUTPUT);
  
  timer.setTimer(5000);// Set Low Power Timer wake up interval in milliseconds

  AudioMemory(8);  // Audio connections require memory to work.
  sgtl5000_1.enable();     // specific to the Teensy audio board
  sgtl5000_1.volume(0.4);  // specific to the Teensy audio board

  SPI.setMOSI(SDCARD_MOSI_PIN);
  SPI.setSCK(SDCARD_SCK_PIN);
  
  prevTime = 1357041600; // Initialised to Jan 1 2013 as recognisable nonsense value
  
/********************************************************
  If SD card not present, flash error signal indefinitely.
********************************************************/
  if (!(SD.begin(SDCARD_CS_PIN))) {
    while (1) {
      error(); 
    }
  }
/********************************************************
  Card present, so signal ready to go.
********************************************************/
  flash();
  
} // End of set-up

/**************************************************************************************************
  Functions
/*************************************************************************************************/
time_t getTeensy3Time()
{
  return Teensy3Clock.get();
}

void flash()
{
  for (int k=0; k<6; k++) // Event signal is 6 quick flashes on green LED.
  {
    digitalWrite(LED_BUILTIN, HIGH);  // LED on.
    delay(10);           
    digitalWrite(LED_BUILTIN, LOW);   // LED off.
    delay(150);                       
  }
} 

void error()
  {
     digitalWrite(LED_BUILTIN, HIGH);  // LED on.
     delay(500);                       
     digitalWrite(LED_BUILTIN, LOW);   // LED off.
     delay(500);                       
  }

/*************************************************************************************************
  Main loop
**************************************************************************************************/
void loop() {
/********************************************************
  On wake, start here.
********************************************************/
  flash(); // Signal via LED to show have woken up
  /********************************************************
    Check whether it's time to play again.
  ********************************************************/
  timeNow = now();
  Serial.print(timeNow);
  Serial.print(" ");
  Serial.print(prevTime);
  Serial.print(" ");
  diffSec = timeNow-prevTime;
  Serial.print("Seconds gone: ");
  Serial.print(diffSec);
  Serial.print(" ");
  Serial.print("Seconds left: ");
  remainSec = (prevTime+silentSec-timeNow);
  Serial.println(remainSec);
  
  if(timeNow > (prevTime + silentSec)) {
  /********************************************************
    Now play the sound file and let it play to end.
  ********************************************************/
  Serial.print("Playing ");
  Serial.println(wavFile[4]);
  playWav1.play(wavFile[4]);
  delay(5);   // Brief delay to read WAV info.
  while (playWav1.isPlaying()) {
    }    // Wait for the file to finish playing.
  } // End of IF loop

  /********************************************************
    When done, flash briefly and go back to sleep
  ********************************************************/
  flash(); // Signal to indicate about to sleep.
  Snooze.deepSleep(config);  // or could use hibernate in place of deepSleep

} // End of void loop.
 
Status
Not open for further replies.
Back
Top