(LEARN) #2 Using the Sound Command

What is the Sound Command?

The sound command will get your robot to play a variety of different sounds

 

Make sure these are added to the from. . . import. . . lines of your script:

from ev3dev2.sound import Sound

*IMPORTANT NOTE the word Sound after import is capitalized.

This is how you will use the command:

The Sound command will allow you to do many different things. Let's look at some of them here. To see the full list of things the Sound command can do for you, click here Links to an external site..

  • Get your robot to talk:
    • This is the structure of the command: Sound().speak(ThingToSpeak)
    • Don't forget the empty parenthesis () after Sound().
    • Replace ThingToSpeak from the structure with a message or a variable. If you place a custom message there, the message must be in quotation marks '' (See example 1 below). If you get the robot to say whatever is inside of a variable, do not use quotation marks (See example 2 below).
    • Example 1: Robot speaks a custom message
      from ev3dev2.sound import Sound
      Sound().speak("Hello, world!")
    • Example 2:  Robot choose a random number between 1 and 100. Robot remembers that number with a variable. Robot speaks the number inside of the variable.
      from ev3dev2.sound import Sound
      import random
      number=random.randint(1,100)
      Sound().speak(number)
  • Get your robot to play a tone
    • This is the structure of the command: Sound().play_tone(frequency, duration)
    • You will need to replace "frequency" and "duration" with numbers.
    • Frequency is the "pitch" of the note the robot will play. Frequency is measured in a unit called Hertz - basically vibrations per second. A low note is about 100 Hertz. The concert A tuning note on the piano is 440 Hertz (this is what people who play instruments usually tune their instrument to). A VERY high pitched note is 10,000 Hertz.
    • Duration is how long, in seconds, the note should be played.
    • Example 1: Robot plays a 440 Hertz note for three seconds:
      from ev3dev2.sound import Sound
      Sound().play_tone(440, 3)

 

 

Something to remember:

*Your robot can do many more things with the Sound command - including playing a .WAV recording. Remember to click here Links to an external site. to see what else it can do.