1

I am working on a project that requires a group of Scribbler 2s to dance at the start of playing a wav file and stop at the end of the file.

(This is not the full code, but rather me testing how to do it so I can apply it to the larger code.)

from Myro import *
from winsound import*
from time import *

def playSong():
    s=PlaySound('C:\Python34\cantHoldUs.wav',SND_FILENAME)
    sleep(30)
    s.PlaySound(None,SND_FILENAME)

while playSong()==True:
    motors(-1,1)   

The song plays and ends, but the robot does not move. Can anyone tell me how?

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
A. C.
  • 11
  • 3

1 Answers1

1

I'd recommend restructuring your code to something with a While Loop since it's cleaner and easier to control:

from time import *

# Play the song
s=PlaySound('C:\Python34\cantHoldUs.wav',SND_FILENAME)

# Start the timer so we can identify when to stop
starttime = time()

# Use a while loop with a True statement until we decide to break it
while True:
    # Make that robot dance!
    motors(-1,1)

    # Check the current time
    stop_time = ((time() - starttime))

    # Stop when 30 seconds is hit
    if stop_time > 30:
        s.PlaySound(None,SND_FILENAME)
        break

    sleep(1)
Adib
  • 1,282
  • 1
  • 16
  • 32
  • This does not work--the song plays but the scribbler still does not move. I received the error that time does not exist. I changed it by putting 'from time import time' but that did not work either. – A. C. Apr 22 '16 at 23:01
  • File "C:\Users\Aesia\Desktop\Elements\Calico-4.0.0-windows- all(1)\Calico\playmusicTest.py", line 8, in AttributeError: 'builtin_function_or_method' object has no attribute 'time' – A. C. Apr 22 '16 at 23:06
  • Have you tried simply using `import time` for time, which will import everything? – Adib Apr 22 '16 at 23:07
  • Yes that is what i had originally and it gave me the same error – A. C. Apr 22 '16 at 23:07
  • @A.C. That's weird. It works perfectly on mine. I edited the code to incorporate your original design `from time import *`, can you try it now – Adib Apr 22 '16 at 23:11
  • Still not working, I'm afraid. Still getting the same error – A. C. Apr 22 '16 at 23:21
  • @A.C. What did you change to make it work. And did the robot stop dancing after the song stopped based on my code? – Adib Apr 22 '16 at 23:31
  • I used import time, but what happened was that the song played, ended, and then the scribbler spun. – A. C. Apr 22 '16 at 23:33
  • @A.C. I'm not sure if this is ideal, but I placed sleep(1) after the if condition to give time for the motors function to operate. Let's see if that works – Adib Apr 22 '16 at 23:39
  • The same result, but now it is saying : `TypeError: 'stop_time' is an invalid keyword argument for this function` – A. C. Apr 22 '16 at 23:54
  • @A.C. Can you paste your full code on pastebin so I can examine it? My code is not part of a function, so stop_time cannot be part of an argument. Most likely there's something else going. If you can also try this: Create a loop that'll make the robot spin without music, see if that works. – Adib Apr 22 '16 at 23:56
  • I am unfamiliar with pastebin. I have my full code as well as the code I was using for a test below it in the past box. What else do I need to do? – A. C. Apr 23 '16 at 00:10
  • @A.C. Does the robot spin without the music? – Adib Apr 23 '16 at 00:11
  • Yes it spins without the music – A. C. Apr 23 '16 at 00:21
  • At least we know that works. It could be likely that there is a conflict in program between the windows player and the robot information. Can you paste the code on http://pastebin.com/ and share the URL to the code – Adib Apr 23 '16 at 00:29