Black Lives Matter - Action and Equality. ... Adafruit is part of the Stop Hate for Profit campaign. Adafruit is open and shipping.
0

Music Maker Affects Timing
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Re: Music Maker Affects Timing

by adafruit_support_bill on Sat Jan 14, 2017 11:04 am

You can call rtc.unixTime() at the start to get your T0. Then you can call the same function in your code at any time to get the current time. The current time minus T0 is the elapsed time in seconds.

adafruit_support_bill
 
Posts: 77895
Joined: Sat Feb 07, 2009 10:11 am

Re: Music Maker Affects Timing

by augydoggy on Sat Jan 14, 2017 11:33 am

1) Should the rtc.unixTime() go in setup or at the beginning of each "scene"?

2) Should it be written as rtc.unixTime() and then to get current time later in the scene write currentTime(rtc.unixTime() + 10); // current time plus 10 seconds.

3) If I do currentTime do I also have to define that in setup? Is there a better way to do this?

I think I'm beginning to understand but still need your help. Thanks mucho.

- Tom

augydoggy
 
Posts: 64
Joined: Mon Jun 06, 2016 6:36 am

Re: Music Maker Affects Timing

by adafruit_support_bill on Sat Jan 14, 2017 12:46 pm

1) Should the rtc.unixTime() go in setup or at the beginning of each "scene"?

Whatever is easiest to use as a start time. I suspect that you would probably want to make your timing relative to the start of the scene.

Code: Select all | TOGGLE FULL SIZE
2) Should it be written as rtc.unixTime() and then to get current time later in the scene write currentTime(rtc.unixTime() + 10); // current time plus 10 seconds.

To initialize your start time:
Code: Select all | TOGGLE FULL SIZE
uint32_t startTime = rtc.unixtime();

To calculate elapsed time in seconds
Code: Select all | TOGGLE FULL SIZE
uint32_t elapsedTime = rtc.unixtime() - startTime;

adafruit_support_bill
 
Posts: 77895
Joined: Sat Feb 07, 2009 10:11 am

Re: Music Maker Affects Timing

by augydoggy on Sat Jan 14, 2017 1:03 pm

This is great!

To create the actual delay, say for how long the motor should run, how would I write that?

- Tom

augydoggy
 
Posts: 64
Joined: Mon Jun 06, 2016 6:36 am

Re: Music Maker Affects Timing

by adafruit_support_bill on Sat Jan 14, 2017 1:37 pm

You can just write a 'while' loop to poll the timer.

To delay for 10 seconds:

Code: Select all | TOGGLE FULL SIZE
uint32_t startTime = rtc.unixtime();
while(rtc.unixtime() - startTime < 10);

adafruit_support_bill
 
Posts: 77895
Joined: Sat Feb 07, 2009 10:11 am

Re: Music Maker Affects Timing

by augydoggy on Sat Jan 14, 2017 4:10 pm

Bill - thank you very much. This has been very informative, very helpful. I'll get back to you with the results.

- Tom

augydoggy
 
Posts: 64
Joined: Mon Jun 06, 2016 6:36 am

Re: Music Maker Affects Timing

by augydoggy on Sat Jan 14, 2017 5:25 pm

I tried inserting the RTC code into my sketch and so far I'm only working on the section titled void waterspout down towards the end. In the beginning, towards the top I declared the following:

Code: Select all | TOGGLE FULL SIZE
RTC_DS3231 rtc;

// These are the pins used for the RTC

#define SDA 20       // RTC I2C clock

#define SCL 21       // RTC I2C data


Further down towards the end I modified void waterspout like this:

Code: Select all | TOGGLE FULL SIZE
void waterSpout() {
  // initialize RTC start time
  uint32_t startTime = rtc.unixtime();
  uint32_t elapsedTime = rtc.unixtime() - startTime;
  // play sound of coal being loaded into coal pit from gondola
  // coal falling sounds
  musicPlayer.startPlayingFile("track006.mp3");
  uint32_t elapsedTime = rtc.unixtime();
  while(rtc.unixtime() - startTime <6);
  // lower spout 6 seconds
  waterSpoutMotor ->run(FORWARD);
  uint32_t elapsedTime = rtc.unixtime();
  while(rtc.unixtime() - startTime <6);
  // stop motor
  waterSpoutMotor ->run(RELEASE);
  // water flows 110 seconds
  uint32_t elapsedTime = rtc.unixtime();
  while(rtc.unixtime() - startTime <110);
  // spout raises to stored position 6 seconds
  waterSpoutMotor ->run(BACKWARD);
  uint32_t elapsedTime = rtc.unixtime();
  while(rtc.unixtime() - startTime <6);
  // stop motor
  waterSpoutMotor ->run(RELEASE);
  uint32_t elapsedTime = rtc.unixtime();
  while(rtc.unixtime() - startTime <6);
  // stop playing sound file
  musicPlayer.stopPlaying();

  }   


When I compile I get the following errors:

Code: Select all | TOGGLE FULL SIZE
Arduino: 1.6.9 (Windows 8.1), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

WARNING: Spurious .github folder in 'RTClib' library
WARNING: Spurious .github folder in 'Adafruit Motor Shield V2 Library' library
WARNING: Spurious .github folder in 'Adafruit PWM Servo Driver Library' library
WARNING: Spurious .github folder in 'Adafruit VS1053 Library' library
C:\Users\Tom\Documents\Arduino\Adafruit_Motor_Shield_V2_Library_master\examples\Engine Service Terminal\Engine_Terminal_Master-2\Engine_Terminal_Master-2.ino: In function 'void waterSpout()':

Engine_Terminal_Master-2:359: error: 'class RTC_DS3231' has no member named 'unixtime'

   uint32_t startTime = rtc.unixtime();

                            ^

Engine_Terminal_Master-2:360: error: 'class RTC_DS3231' has no member named 'unixtime'

   uint32_t elapsedTime = rtc.unixtime() - startTime;

                              ^

Engine_Terminal_Master-2:364: error: redeclaration of 'uint32_t elapsedTime'

   uint32_t elapsedTime = rtc.unixtime();

            ^

Engine_Terminal_Master-2:360: error: 'uint32_t elapsedTime' previously declared here

   uint32_t elapsedTime = rtc.unixtime() - startTime;

            ^

Engine_Terminal_Master-2:364: error: 'class RTC_DS3231' has no member named 'unixtime'

   uint32_t elapsedTime = rtc.unixtime();

                              ^

Engine_Terminal_Master-2:365: error: 'class RTC_DS3231' has no member named 'unixtime'

   while(rtc.unixtime() - startTime <6);

             ^

Engine_Terminal_Master-2:368: error: redeclaration of 'uint32_t elapsedTime'

   uint32_t elapsedTime = rtc.unixtime();

            ^

Engine_Terminal_Master-2:360: error: 'uint32_t elapsedTime' previously declared here

   uint32_t elapsedTime = rtc.unixtime() - startTime;

            ^

Engine_Terminal_Master-2:368: error: 'class RTC_DS3231' has no member named 'unixtime'

   uint32_t elapsedTime = rtc.unixtime();

                              ^

Engine_Terminal_Master-2:369: error: 'class RTC_DS3231' has no member named 'unixtime'

   while(rtc.unixtime() - startTime <6);

             ^

Engine_Terminal_Master-2:373: error: redeclaration of 'uint32_t elapsedTime'

   uint32_t elapsedTime = rtc.unixtime();

            ^

Engine_Terminal_Master-2:360: error: 'uint32_t elapsedTime' previously declared here

   uint32_t elapsedTime = rtc.unixtime() - startTime;

            ^

Engine_Terminal_Master-2:373: error: 'class RTC_DS3231' has no member named 'unixtime'

   uint32_t elapsedTime = rtc.unixtime();

                              ^

Engine_Terminal_Master-2:374: error: 'class RTC_DS3231' has no member named 'unixtime'

   while(rtc.unixtime() - startTime <110);

             ^

Engine_Terminal_Master-2:377: error: redeclaration of 'uint32_t elapsedTime'

   uint32_t elapsedTime = rtc.unixtime();

            ^

Engine_Terminal_Master-2:360: error: 'uint32_t elapsedTime' previously declared here

   uint32_t elapsedTime = rtc.unixtime() - startTime;

            ^

Engine_Terminal_Master-2:377: error: 'class RTC_DS3231' has no member named 'unixtime'

   uint32_t elapsedTime = rtc.unixtime();

                              ^

Engine_Terminal_Master-2:378: error: 'class RTC_DS3231' has no member named 'unixtime'

   while(rtc.unixtime() - startTime <6);

             ^

Engine_Terminal_Master-2:381: error: redeclaration of 'uint32_t elapsedTime'

   uint32_t elapsedTime = rtc.unixtime();

            ^

Engine_Terminal_Master-2:360: error: 'uint32_t elapsedTime' previously declared here

   uint32_t elapsedTime = rtc.unixtime() - startTime;

            ^

Engine_Terminal_Master-2:381: error: 'class RTC_DS3231' has no member named 'unixtime'

   uint32_t elapsedTime = rtc.unixtime();

                              ^

Engine_Terminal_Master-2:382: error: 'class RTC_DS3231' has no member named 'unixtime'

   while(rtc.unixtime() - startTime <6);

             ^

exit status 1
'class RTC_DS3231' has no member named 'unixtime'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.


1) How should unixtime be declared and have I made the other declarations properly?

2) Did I insert the code properly in void waterspout?

3) Any other suggestions?

- Tom

augydoggy
 
Posts: 64
Joined: Mon Jun 06, 2016 6:36 am

Re: Music Maker Affects Timing

by adafruit_support_bill on Sun Jan 15, 2017 9:01 am

Looks like the RTC_DS3231 does not support unixtime directly. It returns a DateTime object and you get the unixtime from that:

Code: Select all | TOGGLE FULL SIZE
DateTime now = rtc.now();
uint32_t startTime = now.unixtime();


You could simplify that to:

Code: Select all | TOGGLE FULL SIZE
uint32_t startTime = rtc.now().unixtime();

adafruit_support_bill
 
Posts: 77895
Joined: Sat Feb 07, 2009 10:11 am

Re: Music Maker Affects Timing

by augydoggy on Sun Jan 15, 2017 10:29 am

Bill - I'm getting the same error about unixtime().

Code: Select all | TOGGLE FULL SIZE
Arduino: 1.6.9 (Windows 8.1), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

WARNING: Spurious .github folder in 'RTClib' library
WARNING: Spurious .github folder in 'Adafruit Motor Shield V2 Library' library
WARNING: Spurious .github folder in 'Adafruit PWM Servo Driver Library' library
WARNING: Spurious .github folder in 'Adafruit VS1053 Library' library
Engine_Terminal_Master-2:73: error: 'class RTC_DS3231' has no member named 'unixtime'

 uint32_t startTime = rtc.unixtime();

                          ^

C:\Users\Tom\Documents\Arduino\Adafruit_Motor_Shield_V2_Library_master\examples\Engine Service Terminal\Engine_Terminal_Master-2\Engine_Terminal_Master-2.ino: In function 'void waterSpout()':

Engine_Terminal_Master-2:362: error: 'class RTC_DS3231' has no member named 'unixtime'

   uint32_t startTime = rtc.unixtime();

                            ^

Engine_Terminal_Master-2:363: error: 'class RTC_DS3231' has no member named 'unixtime'

   uint32_t elapsedTime = rtc.unixtime() - startTime;

                              ^

Engine_Terminal_Master-2:367: error: redeclaration of 'uint32_t elapsedTime'

   uint32_t elapsedTime = rtc.unixtime();

            ^

Engine_Terminal_Master-2:363: error: 'uint32_t elapsedTime' previously declared here

   uint32_t elapsedTime = rtc.unixtime() - startTime;

            ^

Engine_Terminal_Master-2:367: error: 'class RTC_DS3231' has no member named 'unixtime'

   uint32_t elapsedTime = rtc.unixtime();

                              ^

Engine_Terminal_Master-2:368: error: 'class RTC_DS3231' has no member named 'unixtime'

   while(rtc.unixtime() - startTime <6);

             ^

Engine_Terminal_Master-2:371: error: redeclaration of 'uint32_t elapsedTime'

   uint32_t elapsedTime = rtc.unixtime();

            ^

Engine_Terminal_Master-2:363: error: 'uint32_t elapsedTime' previously declared here

   uint32_t elapsedTime = rtc.unixtime() - startTime;

            ^

Engine_Terminal_Master-2:371: error: 'class RTC_DS3231' has no member named 'unixtime'

   uint32_t elapsedTime = rtc.unixtime();

                              ^

Engine_Terminal_Master-2:372: error: 'class RTC_DS3231' has no member named 'unixtime'

   while(rtc.unixtime() - startTime <6);

             ^

Engine_Terminal_Master-2:376: error: redeclaration of 'uint32_t elapsedTime'

   uint32_t elapsedTime = rtc.unixtime();

            ^

Engine_Terminal_Master-2:363: error: 'uint32_t elapsedTime' previously declared here

   uint32_t elapsedTime = rtc.unixtime() - startTime;

            ^

Engine_Terminal_Master-2:376: error: 'class RTC_DS3231' has no member named 'unixtime'

   uint32_t elapsedTime = rtc.unixtime();

                              ^

Engine_Terminal_Master-2:377: error: 'class RTC_DS3231' has no member named 'unixtime'

   while(rtc.unixtime() - startTime <110);

             ^

Engine_Terminal_Master-2:380: error: redeclaration of 'uint32_t elapsedTime'

   uint32_t elapsedTime = rtc.unixtime();

            ^

Engine_Terminal_Master-2:363: error: 'uint32_t elapsedTime' previously declared here

   uint32_t elapsedTime = rtc.unixtime() - startTime;

            ^

Engine_Terminal_Master-2:380: error: 'class RTC_DS3231' has no member named 'unixtime'

   uint32_t elapsedTime = rtc.unixtime();

                              ^

Engine_Terminal_Master-2:381: error: 'class RTC_DS3231' has no member named 'unixtime'

   while(rtc.unixtime() - startTime <6);

             ^

Engine_Terminal_Master-2:384: error: redeclaration of 'uint32_t elapsedTime'

   uint32_t elapsedTime = rtc.unixtime();

            ^

Engine_Terminal_Master-2:363: error: 'uint32_t elapsedTime' previously declared here

   uint32_t elapsedTime = rtc.unixtime() - startTime;

            ^

Engine_Terminal_Master-2:384: error: 'class RTC_DS3231' has no member named 'unixtime'

   uint32_t elapsedTime = rtc.unixtime();

                              ^

Engine_Terminal_Master-2:385: error: 'class RTC_DS3231' has no member named 'unixtime'

   while(rtc.unixtime() - startTime <6);

             ^

exit status 1
'class RTC_DS3231' has no member named 'unixtime'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.


I looked in the library sketch ds3231 and it also has unixtime() and when compiled it runs fine. I tried copying they way it's written there but it still fails. I'm obviously doing something wrong here. could you take another look? Here's my most recent sketch:

Code: Select all | TOGGLE FULL SIZE
// Model railroad animations sketch




// include SPI, MP3 and SD libraries

#include <SPI.h>

#include <Adafruit_VS1053.h>

#include <SD.h>

#include <Wire.h>

#include <Adafruit_MotorShield.h>

#include "utility/Adafruit_MS_PWMServoDriver.h"

#include "RTClib.h"




// define the pins used

//#define CLK 13       // SPI Clock, shared with SD card

//#define MISO 12      // Input data, from VS1053/SD card

//#define MOSI 11      // Output data, to VS1053/SD card

// Connect CLK, MISO and MOSI to hardware SPI pins.

// See http://arduino.cc/en/Reference/SPI "Connections"




// 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




Adafruit_VS1053_FilePlayer musicPlayer =

  // create shield-example object!

  Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);




RTC_DS3231 rtc;

uint32_t startTime = rtc.unixtime();

// These are the pins used for the RTC

#define SDA 20       // RTC I2C clock

#define SCL 21       // RTC I2C data



// Create the motor shield object with the default I2C address

Adafruit_MotorShield AFMS = Adafruit_MotorShield();




// Assign some motors.

Adafruit_DCMotor *sandChuteMotor = AFMS.getMotor(1);

Adafruit_DCMotor *coalChuteMotor = AFMS.getMotor(2);

Adafruit_DCMotor *coalBucketMotor = AFMS.getMotor(3);

Adafruit_DCMotor *waterSpoutMotor = AFMS.getMotor(4);




int runXTimes = 0;




const int BUTTON_A = 21; // pin 21

const int BUTTON_B = 22; // pin 22

const int BUTTON_C = 23; // pin 23

const int BUTTON_D = 24; // pin 24

const int BUTTON_E = 25; // pin 25

const int BUTTON_F = 26; // pin 26

const int BUTTON_G = 27; // pin 27  //not connected


void setup() {
 
const int BUTTON_A = 21;  // ashpit

const int BUTTON_B = 22;  // sandhouse chute

const int BUTTON_C = 23;  // sandhouse compressor

const int BUTTON_D = 24;  // coaling tower chute

const int BUTTON_E = 25;  // coaling tower bucket

const int BUTTON_F = 26;  // water tank spout

const int BUTTON_G = 27;  // water tank spout, not connected







 // initialize seven inputs for use with pushbuttons. Normally HIGH, if pushed, goes to LOW.

  pinMode(BUTTON_A, INPUT_PULLUP);

  pinMode(BUTTON_B, INPUT_PULLUP);

  pinMode(BUTTON_C, INPUT_PULLUP);

  pinMode(BUTTON_D, INPUT_PULLUP);

  pinMode(BUTTON_E, INPUT_PULLUP);

  pinMode(BUTTON_F, INPUT_PULLUP);

  pinMode(BUTTON_G, INPUT_PULLUP);

  Serial.begin(9600);

  if (! musicPlayer.begin()) {
   
    // initialise the music player

     Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));

     while (1);   // Note that this loops forever if it can't find the musicPlayer.

  }
 
  SD.begin(CARDCS); 
 
  // initialise the SD card

  // Set volume for left, right channels. lower numbers == louder volume!

  musicPlayer.setVolume(30,30);




  // 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





  AFMS.begin();  // create with the default frequency 1.6KHz

  sandChuteMotor->setSpeed(100);

  coalChuteMotor->setSpeed(100);

  coalBucketMotor->setSpeed(100);

  waterSpoutMotor->setSpeed(100);

 
}

void loop() {
  if (digitalRead(BUTTON_A) == LOW) {

    ashPit();   // button A was pressed

  }

  if (digitalRead(BUTTON_B) == LOW) {

    sandChute();    // button B was pushed

  }

  if (digitalRead(BUTTON_C) == LOW) {

    sandCompressor();    // button C was pressed

  }

  if (digitalRead(BUTTON_D) == LOW) {

    coalChute();    // button D was pressed

  }

  if (digitalRead(BUTTON_E) == LOW) {

    coalBucket();    // button E was pressed

  }

  if (digitalRead(BUTTON_F) == LOW) {

    waterSpout();    // button F was pressed

  }

  if (digitalRead(BUTTON_G) == LOW) {

    coalPit();    // button G was pressed

  }




}




void ashPit() {
  // play sound of fire and coals
  // sound file to drive LEDs, simulate fire and coals
  musicPlayer.startPlayingFile("track001.wav");
  // stop playing sound file
  musicPlayer.stopPlaying();

  }




void sandChute() {
  // play sand blowing sounds to go with chute operation
  musicPlayer.startPlayingFile("track002.wav");
  delay(15000);
  // turn on motor, chute lowers to sand dome 10 seconds 
  sandChuteMotor ->run(FORWARD);
  delay(2500);
  // stop motor
  sandChuteMotor ->run(RELEASE);
  // sand flows into dome 15 seconds
  delay(2500);
  // chute raises to stored position 10 seconds
  sandChuteMotor ->run(BACKWARD);
  delay(2500);
  // stop motor
  sandChuteMotor ->run(RELEASE);
  delay(2500);
  // stop playing sound file since the animation is done
  musicPlayer.stopPlaying();

}




void sandCompressor() {
  // sounds associated with sand drying process.
  // furnace and compressor sounds to go with door operation
  musicPlayer.startPlayingFile("track003.wav");
  // stop playing sound file since the animation is done
  musicPlayer.stopPlaying();

}







void coalChute() {
  // play sounds of coaling tower chute operation
  // run motor to animate coal chute. Use coalChuteMotor.
  // coal chute sound to go with chute operation
  musicPlayer.startPlayingFile("track004.wav");
  delay(5000);
  // turn on motor, chute lowers 10 seconds
  coalChuteMotor ->run(FORWARD);
  delay(10000);
  // stop motor
  coalChuteMotor ->run(RELEASE);
  // coal drops into tender 45 seconds
  delay(45000);
  // chute raises to stored position 10 seconds
  coalChuteMotor ->run(BACKWARD);
  delay(10000);
  // stop motor
  coalChuteMotor ->run(RELEASE);
  // stop playing sound file since the animation is done
  musicPlayer.stopPlaying();

  }




void coalBucket() {
  // play sounds of coaling tower bucket operation
  // run motor to animate coal bucket. Use coalBucketMotor.
  // coal bucket sounds to go with bucket operation
  musicPlayer.startPlayingFile("track005.wav");
  delay(5000);
  for (; runXTimes < 8; runXTimes++) {
  delay(4000);
  coalBucketMotor ->run(FORWARD);
  // turn on motor, bucket raises to top of tower
  delay(10000);
  coalBucketMotor ->run(RELEASE);
  delay(4000);
  // coal is dumped into tower
  coalBucketMotor ->run(BACKWARD);
  delay(10000);
  // bucket is lowered into pit
  coalBucketMotor ->run(RELEASE);}
  // stop playing sound file since the animation is done
  musicPlayer.stopPlaying();

  }




void waterSpout() {
  // initialize RTC start time
  uint32_t startTime = rtc.unixtime();
  uint32_t elapsedTime = rtc.unixtime() - startTime;
  // play sound of coal being loaded into coal pit from gondola
  // coal falling sounds
  musicPlayer.startPlayingFile("track006.mp3");
  uint32_t elapsedTime = rtc.unixtime();
  while(rtc.unixtime() - startTime <6);
  // lower spout 6 seconds
  waterSpoutMotor ->run(FORWARD);
  uint32_t elapsedTime = rtc.unixtime();
  while(rtc.unixtime() - startTime <6);
  // stop motor
  waterSpoutMotor ->run(RELEASE);
  // water flows 110 seconds
  uint32_t elapsedTime = rtc.unixtime();
  while(rtc.unixtime() - startTime <110);
  // spout raises to stored position 6 seconds
  waterSpoutMotor ->run(BACKWARD);
  uint32_t elapsedTime = rtc.unixtime();
  while(rtc.unixtime() - startTime <6);
  // stop motor
  waterSpoutMotor ->run(RELEASE);
  uint32_t elapsedTime = rtc.unixtime();
  while(rtc.unixtime() - startTime <6);
  // stop playing sound file
  musicPlayer.stopPlaying();

  }   




void coalPit() {
  // play sounds of coal pit operation
  musicPlayer.startPlayingFile("track007.wav");
  delay(15000);
  // stop playing sound file since the animation is done
  musicPlayer.stopPlaying();

}

augydoggy
 
Posts: 64
Joined: Mon Jun 06, 2016 6:36 am

Re: Music Maker Affects Timing

by adafruit_support_bill on Sun Jan 15, 2017 10:58 am

You are still calling unixtime on the RTC_DS3231 rtc object.
Code: Select all | TOGGLE FULL SIZE
RTC_DS3231 rtc;

uint32_t startTime = rtc.unixtime();


You need to call it on the DateTime object as in my previous post.
Code: Select all | TOGGLE FULL SIZE
DateTime now = rtc.now();
uint32_t startTime = now.unixtime();


or

Code: Select all | TOGGLE FULL SIZE
uint32_t startTime = rtc.now().unixtime();

adafruit_support_bill
 
Posts: 77895
Joined: Sat Feb 07, 2009 10:11 am

Re: Music Maker Affects Timing

by augydoggy on Sun Jan 15, 2017 11:17 am

OK Bill. Thanks. I overlooked that.

augydoggy
 
Posts: 64
Joined: Mon Jun 06, 2016 6:36 am

Re: Music Maker Affects Timing

by augydoggy on Mon Jan 16, 2017 12:42 pm

Bill - I was able to get the sketch to compile but I don't think I have the code correct for the time delays. The sound track plays (only MP3) but the motor doesn't run. I've attached void waterspout below.

Code: Select all | TOGGLE FULL SIZE
void waterSpout() {
  // initialize RTC start time
  DateTime now = rtc.now();
  uint32_t startTime = now.unixtime();
  uint32_t elapsedTime = now.unixtime() - startTime;
  // play sound of coal being loaded into coal pit from gondola
  // coal falling sounds
  musicPlayer.startPlayingFile("track006.mp3");
  while(now.unixtime() - startTime < 6);
  // lower spout 6 seconds
  waterSpoutMotor ->run(FORWARD);
  while(now.unixtime() - startTime < 12);
  // stop motor
  waterSpoutMotor ->run(RELEASE);
  // water flows 110 seconds
  while(now.unixtime() - startTime < 122);
  // spout raises to stored position 6 seconds
  waterSpoutMotor ->run(BACKWARD);
  while(now.unixtime() - startTime < 128);
  // stop motor
  waterSpoutMotor ->run(RELEASE);
  while(now.unixtime() - startTime < 134);
  // stop playing sound file
  musicPlayer.stopPlaying();

  }   


Below is the code for the RTC from the beginning of the sketch:

Code: Select all | TOGGLE FULL SIZE
RTC_DS3231 rtc;

// These are the pins used for the RTC

#define SDA 20       // RTC I2C clock

#define SCL 21       // RTC I2C data

augydoggy
 
Posts: 64
Joined: Mon Jun 06, 2016 6:36 am

Re: Music Maker Affects Timing

by adafruit_support_bill on Mon Jan 16, 2017 1:03 pm

You are not updating the time in your while loops. You only read "now" once at the beginning.

it should be:
Code: Select all | TOGGLE FULL SIZE
while(rtc.now().unixtime() - startTime < 6);

adafruit_support_bill
 
Posts: 77895
Joined: Sat Feb 07, 2009 10:11 am

Re: Music Maker Affects Timing

by augydoggy on Mon Jan 16, 2017 1:40 pm

In the while(rtc.now().unixtime() command if I leave off the rtc it will compile. When I write it as rtc.now() I get the following error:

Code: Select all | TOGGLE FULL SIZE
Arduino: 1.6.9 (Windows 8.1), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

WARNING: Spurious .github folder in 'RTClib' library
WARNING: Spurious .github folder in 'Adafruit Motor Shield V2 Library' library
WARNING: Spurious .github folder in 'Adafruit PWM Servo Driver Library' library
WARNING: Spurious .github folder in 'Adafruit VS1053 Library' library
C:\Users\Tom\Documents\Arduino\Adafruit_Motor_Shield_V2_Library_master\examples\Engine Service Terminal\Engine_Terminal_Master-2\Engine_Terminal_Master-2.ino: In function 'void waterSpout()':

Engine_Terminal_Master-2:366: error: request for member 'unixtime' in 'rtc.RTC_DS3231::now', which is of non-class type 'DateTime()'

   while(rtc.now.unixtime() - startTime < 6);

                 ^

exit status 1
request for member 'unixtime' in 'rtc.RTC_DS3231::now', which is of non-class type 'DateTime()'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

augydoggy
 
Posts: 64
Joined: Mon Jun 06, 2016 6:36 am

Re: Music Maker Affects Timing

by adafruit_support_bill on Mon Jan 16, 2017 1:58 pm

while(rtc.now.unixtime() - startTime < 6);

"now()" is a function call. You are missing the parenthesis.

adafruit_support_bill
 
Posts: 77895
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.