Decreasing the size of my sketch - Mp3 player, RTC project

Hey guys,
I’m working on part of a project at the moment where a single pushbutton acts as a stop/start timer, and the mp3 reads out the interval time to you, stopping the timer.
In the code I’ve attached I’m using an arduino uno, DS3231 RTC, and the adafruit VS1053 MP3 player.

However, the sketch i’ve got below is far too large to add to the other code/functions I’ve already created for the rest of the project…
I’m a total beginner, this is my second project, so please forgive my sloppy code and logic.

Any suggestions on decreasing the size of my sketch/better efficiency in the code would be amazing,

cheers guys,

Jake

RTC.ino (30 KB)

Your giant printing if-construct could be replaced by

      Serial.print(lapTime / 60);
      Serial.println(F(" Minutes :"));
      Serial.print(lapTime % 60);
      Serial.println(F(" Seconds"));

After that change your code becomes postable

// include SPI, MP3 and SD libraries
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>

// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"

// These are the pins used for the Mp3 breakout
#define BREAKOUT_RESET  9      // VS1053 reset pin (output)
#define BREAKOUT_CS     10     // VS1053 chip select pin (output)
#define BREAKOUT_DCS    8      // VS1053 Data/command select pin (output)
// These are the pins used for the music maker shield
#define SHIELD_RESET  -1      // VS1053 reset pin (unused!)
#define SHIELD_CS     7      // VS1053 chip select pin (output)
#define SHIELD_DCS    6      // VS1053 Data/command select pin (output)

// These are common pins between breakout and shield
#define CARDCS 4     // Card chip select pin
// DREQ should be an Int pin, see http://arduino.cc/en/Reference/attachInterrupt
#define DREQ 3       // VS1053 Data request, ideally an Interrupt pin


RTC_DS3231 rtc;

Adafruit_VS1053_FilePlayer musicPlayer =
  // create breakout-example object!
  Adafruit_VS1053_FilePlayer(BREAKOUT_RESET, BREAKOUT_CS, BREAKOUT_DCS, DREQ, CARDCS);


char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

const int rtcButPin = 2;

int rtcTime = 0;
int lastRtcTime = 0;
int lapTime = 0;
int rtcState = 0;
int lastRtcState = 0;
int timerState = 0;
int lastTimerState = 0;
int miscState = 0;
int lastMiscState = 0;

void setup () {

  Serial.begin(9600);
  pinMode(rtcButPin, INPUT);
  Serial.println(F("Testing the device"));

  delay(300); // wait for console opening

  if (! musicPlayer.begin()) { // initialise the music player
    Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
    while (1);
  }
  Serial.println(F("Device found"));

  if (!SD.begin(CARDCS)) {
    Serial.println(F("SD failed, or not present"));
    while (1);  // don't do anything more
  }
  Serial.println(F("Found SD"));

  // Set volume for left, right channels. lower numbers == louder volume!
  musicPlayer.setVolume(20, 20);

  // If DREQ is on an interrupt pin (on uno, #2 or #3) we can do background
  // audio playing
  musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT);  // DREQ int

  Serial.println("Device on");
  Serial.println(F("Playing track 001"));
  musicPlayer.playFullFile("track001.mp3");


  if (! rtc.begin()) {
    Serial.println(F("Couldn't find RTC"));
    while (1);
  }

  if (rtc.lostPower()) {
    Serial.println(F("RTC lost power, lets set the time!"));
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }

  DateTime now = rtc.now();

  if (now.hour() >= 0 && now.hour() <= 11) {
    Serial.print(F("Good Morning"));
    Serial.print('\n');
  } else if (now.hour() >= 12 && now.hour() <= 16) {
    Serial.print(F("Good Afternoon"));
    Serial.print('\n');
  } else if (now.hour() >= 17 && now.hour() <= 23) {
    Serial.print(F("Good Evening"));
    Serial.print('\n');
  }
  Serial.println(F("Standby Mode"));
}

void loop () {

  DateTime now = rtc.now();


  // read the pushbutton input pin:
  rtcState = digitalRead(rtcButPin);

  // compare the buttonState to its previous state
  if (rtcState != lastRtcState) {
    // if the current state is HIGH then the button
    // wend from off to on:
    if (rtcState == HIGH) {
      // if the state has changed, increment the counter
      timerState++;
      Serial.print('\n');
      Serial.print(F("Number of button pushes:  "));
      Serial.println(timerState);
      ;
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }

  lastRtcState = rtcState;

  if (timerState != lastTimerState) {
    if (timerState % 2 == 0) {
      Serial.println(F("Timer Stop"));
      miscState = 2;
    } else {
      Serial.println(F("Timer Start"));
      miscState = 1;
    }
  }

  lastTimerState = timerState;

  if (miscState != lastMiscState) {
    if (miscState == 2) {   // code for stopwatch stop

      musicPlayer.playFullFile("track093.mp3");   // stop tone
      lastRtcTime = (now.unixtime());
      Serial.print(now.hour(), DEC);
      Serial.print(':');
      Serial.print(now.minute(), DEC);
      Serial.print(':');
      Serial.print(now.second(), DEC);
      Serial.print('\n');

      lapTime = (lastRtcTime - rtcTime);
      Serial.print(F("Time elapsed: "));
      Serial.print(lapTime);
      Serial.print(F(" Seconds"));
      Serial.print('\n');
      Serial.print('\n');
      Serial.print(F("Giving time:"));
      Serial.print('\n');
      musicPlayer.playFullFile("track099.mp3");    // "time"

      Serial.print(lapTime / 60);
      Serial.println(F(" Minutes :"));
      Serial.print(lapTime % 60);
      Serial.println(F(" Seconds"));
    }

  } else if (miscState == 1) {    // code for stopwatch start

    musicPlayer.playFullFile("track094.mp3");     // start tone
    rtcTime = (now.unixtime());
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.print('\n');
    Serial.print('\n');
  }

  lastMiscState = miscState;
}