Black Lives Matter - Action and Equality. ... Adafruit joins the Stop Hate for Profit campaign.
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 augydoggy on Mon Jan 16, 2017 2:34 pm

Bill - I'm sorry but I really don't understand what I'm doing here. At the beginning of void waterspout I have the following code:

Code: Select all | TOGGLE FULL SIZE
void waterSpout() {
  // initialize RTC start time
  DateTime now = rtc.now();  //defines "rtc.now"
  uint32_t startTime = now.unixtime();  //defines startTime as "now.unixtime()"
  uint32_t elapsedTime = now.unixtime() - startTime;  //defines elapsedTime


I've added "//"statements after each line of code. Are these statements correct? Do I understand what's going on here?

Your last post stated that I wasn't updating the time in the while loops and that I only read "now" once at the beginning. You said it should be "while(rtc.now().unixtime() - startTime <6)" (Note: If I add the rtc in this statement I get an error when compiling). So here I'm confused (even more). I thought "while(rtc.now().unixtime() - startTime <6)" sets the time delay for how long the motor runs, how long it would be off, etc. Your comment makes me think it should go at the beginning of void waterspout and only be read once. If I do that how should I set the delay further down in the script?

I know I'm driving you nuts with this and I apologize. I think it's very close to working now and I just need some better understanding.

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 2:42 pm

The compiler error was this:
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);

Note the difference between that and what I posted:
while(rtc.now().unixtime() - startTime < 6);

"now()" is a function that returns a DateTime object. If you omit the parenthesis, the compiler thinks you are referring to the address of the function which is something entirely different.

If you add the parenthesis as posted, that line will compile correctly.

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

Re: Music Maker Affects Timing

by augydoggy on Mon Jan 16, 2017 3:44 pm

Thank you. I added the () and it now compiles. I still cant get the motors to run though. In my previous post I tried to explain that I don't have a clue what I'm doing here. I'm sure you picked up on that a long time ago. Would you please take a look at void watertank and tell me what I need to change to get the motors motoring? I think I must not understand how to properly set up the time delay function.

Code: Select all | TOGGLE FULL SIZE
void waterSpout() {
  // initialize RTC start time
  DateTime now = rtc.now();  //defines "rtc.now()"
  uint32_t startTime = rtc.now().unixtime();  //defines startTime as "now.unixtime()"
  uint32_t elapsedTime = rtc.now().unixtime() - startTime;  //defines elapsedTime
  //play sounds of filling water tank in tender
  musicPlayer.startPlayingFile("track006.mp3");
  while(rtc.now().unixtime() - startTime < 6);
  // lower spout 6 seconds
  waterSpoutMotor ->run(FORWARD);
  while(rtc.now().unixtime() - startTime < 6);
  // stop motor
  waterSpoutMotor ->run(RELEASE);
  // water flows 110 seconds
  while(rtc.now().unixtime() - startTime < 110);
  // spout raises to stored position 6 seconds
  waterSpoutMotor ->run(BACKWARD);
  while(rtc.now().unixtime() - startTime < 6);
  // stop motor
  waterSpoutMotor ->run(RELEASE);
  while(rtc.now().unixtime() - startTime < 6);
  // stop playing sound file
  musicPlayer.stopPlaying();

  } 

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 4:06 pm

Code: Select all | TOGGLE FULL SIZE
void waterSpout() {
  // initialize RTC start time
  DateTime now = rtc.now();  //defines "rtc.now()"
  uint32_t startTime = rtc.now().unixtime();  //defines startTime as "now.unixtime()"  <<------- startTime is the time you enter the function.
  uint32_t elapsedTime = rtc.now().unixtime() - startTime;  //defines elapsedTime   <<------ this variable is never used.
  //play sounds of filling water tank in tender
  musicPlayer.startPlayingFile("track006.mp3");
  while(rtc.now().unixtime() - startTime < 6);    // <<------------- starts the motor 6 seconds after entering the function.
  // lower spout 6 seconds
  waterSpoutMotor ->run(FORWARD);
  while(rtc.now().unixtime() - startTime < 6);   // <<------------- Stops the motor 6 seconds **after entering the function** (i.e. immediately).  If you want the motor to run for 6 seconds, then you should wait until 6+6=12 seconds have passed since "startTime"
  // stop motor
  waterSpoutMotor ->run(RELEASE);
  // water flows 110 seconds
  while(rtc.now().unixtime() - startTime < 110);  // <<---------- start the motor 110 seconds after entry to the function.  Should be 110 + 12 = 122
  // spout raises to stored position 6 seconds
  waterSpoutMotor ->run(BACKWARD);
  while(rtc.now().unixtime() - startTime < 6);   //<<-------- stops the motor if it has been more than 6 seconds after entry to the function.   Should be 122 + 6 = 128
  // stop motor
  waterSpoutMotor ->run(RELEASE);
  while(rtc.now().unixtime() - startTime < 6);  //<<------- should be 128 + 6 = 134
  // stop playing sound file
  musicPlayer.stopPlaying();

  }

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

Re: Music Maker Affects Timing

by augydoggy on Mon Jan 16, 2017 5:03 pm

Bill - thank you very much. Your notes are great and really explain what's going on. I appreciate the extra effort you put into this. I'm putting you in for a 20% pay increase.

- Tom

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 5:22 pm

Glad to hear it is working for you. If you have photos of your layout, we'd love to see them.

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

Re: Music Maker Affects Timing

by augydoggy on Tue Jan 17, 2017 6:11 am

Bill - I have another question. In the scene called void coalbucket there's a loop that repeats eight times. How do I do the rtc timing in this loop? In the example below I started updating the sketch but stopped when I got to the loop so you'll still see the delay() command there.

Code: Select all | TOGGLE FULL SIZE
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
  // initialize RTC start time
  DateTime now = rtc.now();  // defines "rtc.now()"
  uint32_t startTime = rtc.now().unixtime();  // defines startTime as "now.unixtime()", the time you enter the function
  musicPlayer.startPlayingFile("track005.wav");  // begin soundtrack
  while(rtc.now().unixtime() - startTime < 5);  // starts the bucket filling loop 5 seconds after startTime
  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();

  }


I'll add some pictures in a separate post.
- Tom

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

Re: Music Maker Affects Timing

by adafruit_support_bill on Tue Jan 17, 2017 7:14 am

The simplest way would be to add a "loopStartTime" and initialize it to rtc.now() at the beginning of each loop iteration. Within the loop, calculate your times relative to loopStartTime.

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

Re: Music Maker Affects Timing

by augydoggy on Tue Jan 17, 2017 1:05 pm

In the photo below the water tank is far right, then the coaling tower and sand house to the left. The ash pit is in the lower front left.

Beneath the trestle of the ash pit will be a large pile of cinders with hidden LEDs simulating the glowing coals. The LEDs will be driven by the soundtrack in void ashPit and the varying voltage from the soundtrack will give a varying intensity of light.

The sand house has two scenes. One for the tower (void sandChute) when it provides sand to the locomotive. The sand chute is operated by a DC motor located in the sand house. The soundtrack for this scene has sounds of the locomotive's sand some being opened and closed, the chute being lowered and raised and sand being loaded into the dome. The second scene (void sandCompressor) is sound only and simulates the sounds of shoveling and drying the sand and blowing it up to the top of the tower.

The coaling tower has three scenes. One for the coal chute in the front of the tower (void coalChute) which simulates the filling of the locomotive's tender with coal. The chute is operated by a DC motor hidden inside the tower. The soundtrack for this scene has sounds of the chute being lowered and raised and coal dumping into the tender. The second scene (void coalBucket) is for the coal bucket at the back of the tower. The bucket runs on a track from beneath ground level to the top of the tower. It's hoisted by a DC motor located inside the tower shack and the purpose is to keep the tower filled with coal. The soundtrack for this scene has sounds of the hoist motor running, coal being loaded and dumped, and the bucket being hoisted and lowered. This scene has a loop that repeats eight times. The third scene (void coalPit) is sound only and simulates the sounds of a hopper car dumping coal into the coal bin located behind the coal tower.

The water tank scene (void waterSpout) simulates filling the locomotive's tender with water. The spout is operated by a DC motor hidden beneath the water tank. The soundtrack has sounds of the tenders water hatch being opened and closed, water flowing and the spout cables running through the pulleys as it's lowered and raised.
Attachments
image.jpeg
Front View
image.jpeg (842.67 KiB) Viewed 211 times
image.jpeg
Back View
image.jpeg (871.5 KiB) Viewed 211 times

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

Re: Music Maker Affects Timing

by adafruit_support_bill on Tue Jan 17, 2017 1:17 pm

Very impressive modeling! What scale is that?
Thanks for the photos.

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

Re: Music Maker Affects Timing

by augydoggy on Tue Jan 17, 2017 3:04 pm

Bill - this is O scale, 1/4":1'

I'm doing narrow gauge so all spouts and chutes are lowered 4' compared to standard gauge.

Thanks again for all your help. I haven't had a chance to try out the code for the loop timing yet but think I understand what needs to be done.

- Tom

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

Re: Music Maker Affects Timing

by augydoggy on Tue Jan 17, 2017 5:17 pm

I attempted to get the loopcode going but am not having any luck with even initializing it. Would you take a look and tell me how to properly get it started?

Code: Select all | TOGGLE FULL SIZE
void coalBucket() {
  // play sounds of coaling tower bucket operation
  // run motor to animate coal bucket.
  // initialize RTC start time
  DateTime now = rtc.now();  // defines "rtc.now()"
  uint32_t startTime = rtc.now().unixtime();  // defines startTime as "now.unixtime()", the time you enter the function
  musicPlayer.startPlayingFile("track005.wav");  // begin soundtrack
  while(rtc.now().unixtime() - startTime < 5);  // pause 5 seconds after startTime for sound of engine starting
  for (; runXTimes < 8; runXTimes++) {  // begin bucket filling loop, repeat 8 times
  loopStartTime = rtc.now();
  delay(4000);  // pause while bucket fills, 4 seconds
  coalBucketMotor ->run(FORWARD);  // start motor in forward
  delay(10000);  // bucket raises to top of tower, 10 seconds
  coalBucketMotor ->run(RELEASE);  // stop motor
  delay(4000);  // pause while bucket dumps, 4 seconds
  coalBucketMotor ->run(BACKWARD);  // start motor in backward
  delay(10000);  // bucket is lowered into pit
  coalBucketMotor ->run(RELEASE);}  // stop motor, end of filling loop after 8 repetitions
  while(rtc.now().unixtime() - startTime < 6);  // allow 6 seconds for sound of engine stopping
  musicPlayer.stopPlaying();  // stop playing sound file since the animation is done

  }

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

Re: Music Maker Affects Timing

by adafruit_support_bill on Tue Jan 17, 2017 5:39 pm

You have 28 seconds of delay() between loopStartTime and your while loop. And your while loop is still comparing against startTime.

If you want to measure time relative to the start of the loop, you need to use loopStartTime. And you also need to account for all the delays before that.

Code: Select all | TOGGLE FULL SIZE
  while(rtc.now().unixtime() - loopStartTime < 28 + 6);

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

Re: Music Maker Affects Timing

by augydoggy on Tue Jan 17, 2017 6:27 pm

I didn't explain myself well in my last post. I hadn't replaced the delay() yet. Let me try again.

I tried initializing loopStartTime in the beginning of the loop but I'm not doing it correctly. I'm getting an error that it "was not declared in this scope."

Code: Select all | TOGGLE FULL SIZE
void coalBucket() {
  // play sounds of coaling tower bucket operation
  // run motor to animate coal bucket.
  // initialize RTC start time
  DateTime now = rtc.now();  // defines "rtc.now()"
  uint32_t startTime = rtc.now().unixtime();  // defines startTime as "now.unixtime()", the time you enter the function
  musicPlayer.startPlayingFile("track005.wav");  // begin soundtrack
  while(rtc.now().unixtime() - startTime < 5);  // pause 5 seconds after startTime for sound of engine starting
  for (; runXTimes < 8; runXTimes++) {  // begin bucket filling loop, repeat 8 times
  loopStartTime = rtc.now();
  while(rtc.now().unixtime() - loopStartTime < 4 + 5);  // pause while bucket fills, 4 seconds
  coalBucketMotor ->run(FORWARD);  // start motor in forward
  while(rtc.now().unixtime() - loopStartTime < 14 + 5);  // bucket raises to top of tower, 10 seconds
  coalBucketMotor ->run(RELEASE);  // stop motor
  while(rtc.now().unixtime() - loopStartTime < 18 + 5);  // pause while bucket dumps, 4 seconds
  coalBucketMotor ->run(BACKWARD);  // start motor in backward
  while(rtc.now().unixtime() - loopStartTime < 28 + 5);  // bucket is lowered into pit, 10 seconds
  coalBucketMotor ->run(RELEASE);}  // stop motor, end of filling loop after 8 repetitions
  while(rtc.now().unixtime() - startTime < 235);  // allow 6 seconds for sound of engine stopping
  musicPlayer.stopPlaying();  // stop playing sound file since the animation is done

  }

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

Re: Music Maker Affects Timing

by adafruit_support_bill on Tue Jan 17, 2017 6:33 pm

You need to either declare it as a DateTime instance globally - just as you did with startTime. Or declare a local instance at the start of your loop:

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

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

Please be positive and constructive with your questions and comments.