Libraries.Robots.Lego.Sound Documentation
This class is an object representation of the LEGO EV3 Mindstorms sound controller. It is used to control volume, to play tones, and to play music.
Example Code
use Libraries.Robots.Lego.Sound
use Libraries.Robots.Lego.Motor
use Libraries.Robots.Lego.ColorSensor
use Libraries.Robots.Lego.Utility
Sound sound
Motor motor
ColorSensor colorSensor
Utility utility
colorSensor:SetPort(colorSensor:PORT_3)
motor:RotateForward(motor:MOTOR_B)
motor:RotateForward(motor:MOTOR_C) //two motors to move the robot, connected to ports B and C
text color = ""
repeat 20 times
color = colorSensor:GetColor()
if color = "red"
sound:PlayTone(200, 100)
elseif color = "blue"
sound:PlayTone(800, 100)
elseif color = "green"
sound:PlayTone(1600, 100)
elseif color = "yellow"
sound:PlayTone(3200, 100)
end
utility:DelayMilliseconds(100)
end
Inherits from: Libraries.Language.Object
Actions Documentation
Beep()
This action tells the robot to play a beep noise.
Example
use Libraries.Robots.Lego.Sound
use Libraries.Robots.Lego.Motor
use Libraries.Robots.Lego.ColorSensor
use Libraries.Robots.Lego.Utility
Sound sound
Motor motor
ColorSensor colorSensor
Utility utility
colorSensor:SetPort(3)
motor:RotateForward(motor:MOTOR_B)
motor:RotateForward(motor:MOTOR_C) //two motors used to move the robot, connected to ports B and C
repeat 5 times
if colorSensor:GetColor() = "red"
sound:BeepTwice()
else
sound:Buzz()
end
utility:DelayMilliseconds(1000)
end
BeepSequenceDown()
This action tells the robot to play a sequence of 4 beeps with each tone lower than the previous.
Example
use Libraries.Robots.Lego.Sound
use Libraries.Robots.Lego.Motor
Sound sound
Motor motor
motor:SetSpeed(motor:MOTOR_A, 180)
sound:BeepSequenceUp() //indicate that the motor is about to start moving
motor:RotateByDegrees(motor:MOTOR_A, 720) //rotate the motor connected to port A
sound:BeepSequenceDown() //indicate the motor has stopped moving
BeepSequenceUp()
This action tells the robot to play a sequence of 4 beeps with each tone higher than the previous.
Example
use Libraries.Robots.Lego.Sound
use Libraries.Robots.Lego.Motor
Sound sound
Motor motor
motor:SetSpeed(motor:MOTOR_A, 180)
sound:BeepSequenceUp() //indicate the motor is about to start moving
motor:RotateByDegrees(motor:MOTOR_A, 720) //rotate the motor connected to port A
sound:BeepSequenceDown() //indicate the motor has stopped moving
BeepTwice()
This action tells the robot to beep twice.
Example
use Libraries.Robots.Lego.Sound
use Libraries.Robots.Lego.Motor
use Libraries.Robots.Lego.ColorSensor
use Libraries.Robots.Lego.Utility
Sound sound
Motor motor
ColorSensor colorSensor
Utility utility
colorSensor:SetPort(3)
motor:RotateForward(motor:MOTOR_B)
motor:RotateForward(motor:MOTOR_C) //two motors used to move the robot, connected to ports B and C
repeat 5 times
if colorSensor:GetColor() = "blue"
sound:BeepTwice()
else
sound:Buzz()
end
utility:DelayMilliseconds(1000)
end
Buzz()
This action tells the robot to make a low pitch buzz noise.
Example
use Libraries.Robots.Lego.Sound
use Libraries.Robots.Lego.InfraredSensor
use Libraries.Robots.Lego.Motor
use Libraries.Robots.Lego.Utility
Sound sound
InfraredSensor infraredSensor
Motor motor
Utility utility
infraredSensor:SetPort(infraredSensor:PORT_4)
motor:RotateForward(motor:MOTOR_B)
motor:RotateForward(motor:MOTOR_C) //two motors used to move the robot, connected to ports B and C
repeat until infraredSensor:GetDistance() < 50
sound:Beep()
utility:DelayMilliseconds(200)
end
sound:Buzz() //the sensor found a nearby object
motor:Stop(motor:MOTOR_B)
motor:Stop(motor:MOTOR_C)
Compare(Libraries.Language.Object object)
This action compares two object hash codes and returns an integer. The result is larger if this hash code is larger than the object passed as a parameter, smaller, or equal. In this case, -1 means smaller, 0 means equal, and 1 means larger. This action was changed in Quorum 7 to return an integer, instead of a CompareResult object, because the previous implementation was causing efficiency issues.
Parameters
- Libraries.Language.Object: The object to compare to.
Return
integer: The Compare result, Smaller, Equal, or Larger.
Example
Object o
Object t
integer result = o:Compare(t) //1 (larger), 0 (equal), or -1 (smaller)
Equals(Libraries.Language.Object object)
This action determines if two objects are equal based on their hash code values.
Parameters
- Libraries.Language.Object: The to be compared.
Return
boolean: True if the hash codes are equal and false if they are not equal.
Example
use Libraries.Language.Object
use Libraries.Language.Types.Text
Object o
Text t
boolean result = o:Equals(t)
GetHashCode()
This action gets the hash code for an object.
Return
integer: The integer hash code of the object.
Example
Object o
integer hash = o:GetHashCode()
GetVolume()
This action gets the volume of the robot.
Return
integer: how loud the robot is set to be. This value will be between 0 (lowest) and 100 (highest).
Example
use Libraries.Robots.Lego.Sound
use Libraries.Robots.Lego.Screen
use Libraries.Robots.Lego.Utility
Sound sound
Screen screen
Utility utility
integer volume = sound:GetVolume()
screen:OutputCenter("Initial volume: " + volume, 2)
if volume > 30
sound:SetVolume(30) //limit the maximum volume
elseif volume < 10
sound:SetVolume(10) //limit the minimum volume
end
screen:OutputCenter("New volume: " + sound:GetVolume(), 4)
sound:BeepSequenceUp()
utility:DelayMilliseconds(250)
sound:BeepSequenceDown()
PlayAudio(Libraries.System.File audioFile)
This action plays an audio file stored on the robot. The audio file must be in .wav format and use a single channel (mono).
Parameters
- Libraries.System.File: should be a File whose path leads to a single channel .wav audio file.
Example
use Libraries.Robots.Lego.Sound
use Libraries.Robots.Lego.Motor
use Libraries.Robots.Lego.Utility
use Libraries.System.File
Sound sound
Motor motor
Utility utility
File audioFile
audioFile:SetPath("sound.wav") //requires a file named sound.wav already stored on the robot
motor:RotateForward(motor:MOTOR_B) //two motors used to move the robot, connected to ports B and C
motor:RotateBackward(motor:MOTOR_C) //both motors go in opposite directions, causing the robot to spin in place
sound:SetVolume(100)
sound:PlayAudio(audioFile)
repeat while sound:IsPlaying()
utility:DelayMilliseconds(100) //check every 100 milliseconds if the robot is still playing the audio
end
motor:Stop(motor:MOTOR_B)
motor:Stop(motor:MOTOR_C)
PlayAudio(text audioFile)
This action plays an audio file stored on the robot. The audio file must be in .wav format and use a single channel (mono).
Parameters
- text audioFile: should be a file path that leads to a single channel .wav audio file.
Example
use Libraries.Robots.Lego.Sound
use Libraries.Robots.Lego.Motor
use Libraries.Robots.Lego.Utility
use Libraries.System.File
Sound sound
Motor motor
Utility utility
text audioFile = "sound.wav" //assumes a file named sound.wav exists in the current directory
motor:RotateForward(motor:MOTOR_B) //two motors used to move the robot, connected to ports B and C
motor:RotateBackward(motor:MOTOR_C) //both motors go in opposite directions, causing the robot to spin in place
sound:SetVolume(100)
sound:PlayAudio(audioFile)
repeat while sound:IsPlaying()
utility:DelayMilliseconds(100) //check every 100 milliseconds if the robot is still playing the audio
end
motor:Stop(motor:MOTOR_B)
motor:Stop(motor:MOTOR_C)
PlayTone(integer frequency, integer duration)
This action plays a specified tone for a an amount of time.
Parameters
- integer frequency: indicates the frequency, in hertz (Hz), of the tone that will be played. Audible frequencies for humans generally range from around 20 to 20,000 Hz.
- integer duration: specifies how long the tone should play for, in milliseconds.
Example
use Libraries.Robots.Lego.Sound
use Libraries.Robots.Lego.Utility
Sound sound
Utility utility
integer frequency = 100
repeat 5 times
sound:PlayTone(frequency, 200)
utility:DelayMilliseconds(200)
frequency = frequency * 3
end
SetVolume(integer volume)
This action sets the volume of the robot.
Parameters
- integer volume: specifies how loud the robot should be. Valid volume levels range from 0 (lowest) to 100 (highest).
Example
use Libraries.Robots.Lego.Sound
use Libraries.Robots.Lego.Utility
Sound sound
Utility utility
sound:SetVolume(20)
sound:PlayTone(777, 200)
utility:DelayMilliseconds(400)
sound:SetVolume(40)
sound:PlayTone(777, 200)
utility:DelayMilliseconds(400)
sound:SetVolume(60)
sound:PlayTone(777, 200)
utility:DelayMilliseconds(400)
sound:SetVolume(80)
sound:PlayTone(777, 200)
utility:DelayMilliseconds(400)
sound:SetVolume(100)
sound:PlayTone(777, 200)
utility:DelayMilliseconds(400)