The MP3 ans SD use SPI the RTC uses I2C.
I can use microseconds and run the MP3 and the analog clock with no issues. It only crashes when I use the RTC.
I read the RTC on every loop in this example and only update the serial port every second. There are no delays in my loop
Here is most of the code involved.
MP3 Sheild
#include <memorysaver.h> // Trim unneeded definitions from UTFT.
#include <dtostrf.h>
// ##### UTFT ---------------------------------------------------------------------------
#include <UTFT.h> //Display
UTFT myGLCD(CPLD,25,26,27,28); // For DUE with 7.0 CLPD and CTE Sheild
#include <UTFT_Geometry.h>
UTFT_Geometry geo(&myGLCD); // For Drawing triangles and arrows
// ##### TOUCH --------------------------------------------------------------------------
#include <URTouch.h> // Touch Screen
URTouch myTouch(6,5,32,3,2);// Define Touch Screen
// ##### TIME ---------------------------------------------------------------------------
#include <DS3231.h> // I2C RTC
DS3231 rtc(SDA, SCL); // Init the DS3231 using the hardware interface
Time t; // Init a Time-data structure
// ##### FONTS --------------------------------------------------------------------------
extern uint8_t Ubuntu[]; //Font size: 24x32 pixels
extern uint8_t SixteenSegment16x24[];
extern uint8_t SixteenSegment24x36[];
extern uint8_t DotMatrix_M_Slash[];
extern uint8_t SixteenSegment40x60[];
extern uint8_t BigFont[];
// ##### UTFT_BUTTONS -------------------------------------------------------------------
#include <UTFT_Buttons.h> // UTFT Buttons Add-On
UTFT_Buttons myButtons(&myGLCD, &myTouch);
// ##### SD ----------------------------------------------------------------------------
#include <avr/pgmspace.h>
#include <SPI.h>
#include <SdFat.h> // SD library with long filenames
#define CARDCS 8 // SD Card chip select pin
SdFat SD;
boolean hasSD=false;
// ##### MP3 ----------------------------------------------------------------------------
#include <VS1053SdFat.h>
/* MP3 Header Row
VS1053_FilePlayer(VS1053_RESET, VS1053_CS, VS1053_DCS, VS1053_DREQ, CARDCS);
D8 --- Use For SD Card CS
D9 --- Used for TFT Backlight
D10 --- Used for TFT Flash
D11 --- Use for MP3 DREQ
D12 --- Use for MP3 DCS
D13 --- Use for MP3 RESET
*/
// ##### Define Pins for MP3 ------------------------------------------------------------
#define VS1053_CS 14 // VS1053 chip select pin. ??? Does it have to be 52
#define VS1053_DCS 11 // VS1053 DCS pin.
#define VS1053_DREQ 12 // VS1053 DREQ pin.
#define VS1053_RESET 13 // VS1053 RESET pin.
VS1053_FilePlayer musicPlayer = VS1053_FilePlayer(VS1053_RESET, VS1053_CS, VS1053_DCS, VS1053_DREQ, CARDCS);
// ##### SD File Variables
#define MAX_NO_TRACKS 20 //50 tracks maximum per album
#define MAX_NAME_LEN 30 //50 tracks maximum per album
char filebuffer[MAX_NAME_LEN +MAX_NAME_LEN +2];
char currentSong[MAX_NAME_LEN +1]; //79 char max length for filename (including .mp3)
char currentAlbum[MAX_NAME_LEN +1];//79 char max length for directory (album) name
FatPos_t tracks[MAX_NO_TRACKS];
byte serialPos=0; byte numberAlbums=0; byte numberTracks=0; byte totalTracks=0;
//PERSISTANT VARIABLES
bool randomPlay=false; byte albumNumber = 0; byte trackNumber = 1;
File dir;
byte lastkeypress=0; unsigned long press_ms=0; unsigned long spress_ms=0;
// ##### SENSORS -----------------------------------------------------------------------
int FIn = A11 ; //47ohm Resistor to +5vdc Black Wire
int BIn = A10 ; //4.7K to Ground and 100K to 12V
int WIn = A9 ; //47ohm Resistor to +5vdc Red Wire
int OIn = A8 ; //100ohm Resistor to +5vdc Green Wire
int FVal = 0; float FL = 0; float FLSnap = 0 ; float PFL = 0; //FUEL
int FSV; int FSH; int FEV; int FEH;
int BVal = 0; float BV = 0; float BVSnap = 0; float PBV = 0; //BATTERY
int BSV; int BSH; int BEV; int BEH;
int WVal = 0; float WT = 0; float WTSnap = 0; float PWT = 0; //WATER
int WSV; int WSH; int WEV; int WEH;
int OVal = 0; float OP = 0; float OPSnap = 0 ; float POP = 0; //OIL
int OSV; int OSH; int OEV; int OEH;
// ##### Clock --------------------------------------------------------------------------
int Width = 34; int Length = 250; // Set Gauge Size
int clockCenterX=600;
int clockCenterY=208;
int oldsec=0;
// ##### Setup --------------------------------------------------------------------------
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
//while (!Serial) {; // wait for serial port to connect. Needed for native USB port only
//}
initSD();
rtc.begin(); // Initialize the rtc object... // If this is missing from the setup System will not initialize.
//t = rtc.getTime();// Get data from the DS3231
myTouch.InitTouch();
myTouch.setPrecision(PREC_HI);
myGLCD.InitLCD(LANDSCAPE);
myGLCD.fillScr (VGA_BLACK); //Blank out display page 0
myGLCD.setFont(SixteenSegment16x24);
drawGaugePage();
//dispClock();
initSD(); //MP3 Player
initMusicPlayer(); //MP3 Player
playMP3();
t = rtc.getTime();
oldsec=t.sec;
}
// ##### LOOP ---------------------------------------------------------------------------
void loop() {
gaugeRead(50);
//turnSignalLeft();
//turnSignalRight();
//dispClock();
t = rtc.getTime();
if (oldsec >= 59) oldsec=0;
if (t.sec >= oldsec+1) {
Serial.print(F(" Old Second is "));
Serial.print(oldsec);
Serial.print(F(" Current Time "));
Serial.print(t.hour);
Serial.print(F(":"));
Serial.print(t.min);
Serial.print(F(":"));
Serial.println(t.sec);
oldsec=t.sec;
if (oldsec == 59) oldsec=0;
}
}
void initMusicPlayer(){
Serial.println(F(">>> Intializing VS1053 MP3 Player chip"));
if (! musicPlayer.begin()) { // initialise the music player
Serial.println(F(">>> Could not Find VS1053, Do you have the correct pins defined"));
while (1); //This stops the code until the mp3 initializes.
}
//if (! musicPlayer.useInterrupt(VS1053_FILEPLAYER_TIMER0_INT)) // Timer Interrupt
if (! musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT)) // Hardware DREQ Interrupt
delay(100); // Give VS1053 some time to initialize.
Serial.println(F(">>> MP3 OK"));
musicPlayer.setVolume(0,0); // Set volume for left, right channels. lower numbers == louder volume!
}
// ##### SD -----------------------------------------------------------------------------
void initSD(){
numberAlbums=0;
if (!SD.begin(CARDCS)) {
Serial.println(F(">>> SD Not Found!")); //SD failed, or not present
hasSD=false; // don't do anything more
}
else {
hasSD=true;
Serial.println(F(">>> SD OK"));
listSDContents();
}
}
void playMP3(){
getSong(trackNumber);
sprintf(filebuffer,"%s/%s",currentAlbum,currentSong); // Sets Up the variable to play the track.
Serial.println(F("CURRENT TRACK"));
Serial.println(filebuffer);
if (musicPlayer.paused()) musicPlayer.pausePlaying(false); // Start the music player back up it it is paused.
if (! musicPlayer.startPlayingFile(filebuffer)) { // Play the track.
Serial.print("Could not open file ");
Serial.println(filebuffer);
delay(1000);
}
else{
Serial.println(F("Started playing "));
Serial.println(filebuffer);
}
}