United Kingdom
Asked — Edited

Sound Activated Circuits (Leds, Lamps Etc)

One item that crops up often is sound activated LEDs or lights. And while the Sound servo control in ARC can handle this it is not perfect. So some clever scripting needs to be used to activate/deactivate/dim the lights or LEDs.

A quick little example of the outcome of the script initially done for Rex's project Questor.

So how is it done? It's very simple to be honest.

First add the Sound servo control and set it up as below.

User-inserted image

Notice how the port used is set to N/A. We don't want it to control any servo directly, what we want is the variable it gives.

Then add a new script control or new script to a script manager...


# Sound servo to PWM range Script
# Author: Rich
# Version: 1.0.0
# Date: 25th June 2013

# Variables (do not adjust)
# $soundvalue = auto
$pwmvalue = 0

# Main script
# Set the start point for the never ending loop
:loop
# Check the variable from the Sound servo control against a list of pre-determined levels (you may need to change these) and set the PWM value to suit.
IF ($soundvalue < 10)
  $pwmvalue = 0
ELSEIF ($soundvalue < 20)
  $pwmvalue = 10
ELSEIF ($soundvalue < 30)
  $pwmvalue = 20
ELSEIF ($soundvalue < 40)
  $pwmvalue = 30
ELSEIF ($soundvalue < 50)
  $pwmvalue = 40
ELSEIF ($soundvalue < 60)
  $pwmvalue = 50
ELSEIF ($soundvalue < 70)
  $pwmvalue = 60
ELSEIF ($soundvalue < 80)
  $pwmvalue = 70
ELSEIF ($soundvalue < 90)
  $pwmvalue = 80
ELSEIF ($soundvalue < 100)
  $pwmvalue = 90
ELSE 
  $pwmvalue = 100
ENDIF 

# Set the PWM with the value chosen above (you may need to change the port to suit)
PWM(D7, $pwmvalue)
# Add any other PWM ports above this line with PWM(PortNo, $pwmvalue)

# Sleep for 100ms to avoid saturation (you may need to adjust this to increase accuracy or increase stability. Reduce the delay for better accuracy, increase for better stability)
Sleep(100)

# Jump back to the start
Goto(loop)

Change the port on the PWM command line if needed, I used D7 but any port will work. Duplicate that line with a different port number if you need 2 (or more) ports to react.

You may also need to adjust the PWM values in the if/elseif part to suit your robot, LEDs, Lamps etc.

Lastly, connect up the LEDs, Lamps etc. to the EZ-B using the TIP Transistor circuit method.

Now, when any sound is detected by the Sound servo control it should adjust the PWM of the digital port in steps, as defined in the If/ElseIf/Else statement. If low level sound (in this case below 10 however that can be adjusted) the PWM is set to 0 and therefore the LED, Lamp etc. is off.

If you have any questions, queries, problems or even suggestions please feel free to ask. This is currently Version 1.0.0 of this script, there is always scope to improve:) I may, should time permit, add in the usual variables for ease of use (that'll be V1.0.1)


ARC Pro

Upgrade to ARC Pro

Join the ARC Pro community and gain access to a wealth of resources and support, ensuring your robot's success.

#1  

Thanks Rich... I am anxious to try this out.... as usual you are my hero sir.

#2  

This has been on my wish list for a long time.

ControlCommand("voice", ScriptStart) SayWait(" blah blah blah") ControlCommand("voice", ScriptStop) Have just tested this and works very well ...:)

How can I get this to work with other than "Say" or "SayWait"? For example to work with say a RSS Feed?

Rich, as always very grateful for your time & energy you are sure a GREAT asset to this community!

United Kingdom
#3  

You could just have it constantly running, so started with a ControlCommand in an init script run on connection.

ControlCommand("Script Manager", ScriptStart, "Sound Activated Lights")

It then picks up any sound. However this does include sound picked up by the microphone (so when you speak) and any system sounds.

Or you could just add the ControlCommand to the start and end of the scripts that cause speech. There is an RSS Feed script somewhere in the forum that I tinkered with, when I get home I'll look for it and show the adjustment needed.

#4  

My solution was not as high tech but has worked fantastic for me Mini-B Speaks I used an op-amp tied into the speaker output from my sound board to my LED array. I can adjust the brightness and responsiveness of the LEDs by adjusting the op-amp output. See my build here

Mini-B Build

#5  

Rich did you get a chance to look at the script I did in this thread post #112 and view the video I posted?

I had come up with what @Herr Ball mentioned above. The nice thing about doing it this way is the LEDs are not triggered by any sounds, the loop script flickers them only while the text is being spoken. The SayWait lets the flicker loop run until the text finishes then the script is stopped by the ControlCommand("Voice Lights", ScriptStop) that I made. The only thing is I am sure that you could help me clean up the code some to streamline it.

I still want to test your latest version using the SoundServo. I have other uses for that.

Thanks

United Kingdom
#6  

Yes I found that yesterday (seems I missed a whole page of the topic out).

With Herr Balls method one thing I thought about was if the LEDs are on when the ControlCommand stops the script, they would stay on. But an easy solution. I'll post on that something shortly:)

#8  

What I did was to place a PWM(D15,0) right after the ControlCommand("Voice Lights", ScriptStop) to make sure it set it back to off when the SayWait was finished.

United Kingdom
#9  

As Herr Ball's method...

I'll use the RSS News Reading script I made a few weeks back. The original looks like this;

# RSS News Reading Script
# Author: Rich Pyke
# Version: 1.0.0
#
# RSS Script for reading news from RSS posts. Adapted from cafasarus
# script at https://synthiam.com/Community/Questions/3650

# Define variables
$feedurl = "http://feeds.reuters.com/Reuters/domesticNews"

# Do not change these variables
$storyLn = 0

# Start the code
:begin
# Increment the story line by 1
$storyLn++
# Print the story number
Print($storyLn)
# Speak the story
SpeakRSSDescription($feedurl,$storyLn)
SayWait("Would you like me to continue?")
# Ask if it should continue to the next story
$response = WaitForSpeech(10,"YES","NO")
# If yes
IF ($response = "YES")
  # Go back to the start
  Goto(begin)
  # Otherwise
ELSE 
  # Say OK
  SayWait("Ok")
ENDIF 
  # End of script
Halt()

We also want the Sound servo control from post #1

User-inserted image

and the script from post #1

# Sound servo to PWM range Script
# Author: Rich
# Version: 1.0.0
# Date: 25th June 2013

# Variables (do not adjust)
# $soundvalue = auto
$pwmvalue = 0

# Main script
# Set the start point for the never ending loop
:loop
# Check the variable from the Sound servo control against a list of pre-determined levels (you may need to change these) and set the PWM value to suit.
IF ($soundvalue < 10)
  $pwmvalue = 0
ELSEIF ($soundvalue < 20)
  $pwmvalue = 10
ELSEIF ($soundvalue < 30)
  $pwmvalue = 20
ELSEIF ($soundvalue < 40)
  $pwmvalue = 30
ELSEIF ($soundvalue < 50)
  $pwmvalue = 40
ELSEIF ($soundvalue < 60)
  $pwmvalue = 50
ELSEIF ($soundvalue < 70)
  $pwmvalue = 60
ELSEIF ($soundvalue < 80)
  $pwmvalue = 70
ELSEIF ($soundvalue < 90)
  $pwmvalue = 80
ELSEIF ($soundvalue < 100)
  $pwmvalue = 90
ELSE 
  $pwmvalue = 100
ENDIF 

# Set the PWM with the value chosen above (you may need to change the port to suit)
PWM(D7, $pwmvalue)
# Add any other PWM ports above this line with PWM(PortNo, $pwmvalue)

# Sleep for 100ms to avoid saturation (you may need to adjust this to increase accuracy or increase stability. Reduce the delay for better accuracy, increase for better stability)
Sleep(100)

# Jump back to the start
Goto(loop)

Assuming the scripts are EZ-Scripts rather than in the script manager. The Sound servo script we will assume is named "SoundScript"

So we need to modify the RSS News Script to include starting and stopping the SoundScript. We do this with

ControlCommand("SoundScript", ScriptStart)

and

ControlCommand("SoundScript", ScriptStop)

We put the ScriptStart before any SayWait or SpeakRSSDescription and the ScriptStop after. So the code should now look like;

# RSS News Reading Script
# Author: Rich Pyke
# Version: 1.0.0
#
# RSS Script for reading news from RSS posts. Adapted from cafasarus
# script at https://synthiam.com/Community/Questions/3650

# Define variables
$feedurl = "http://feeds.reuters.com/Reuters/domesticNews"

# Do not change these variables
$storyLn = 0

# Start the code
:begin
# Increment the story line by 1
$storyLn++
# Print the story number
Print($storyLn)
# Speak the story
ControlCommand("SoundScript", ScriptStart)
SpeakRSSDescription($feedurl,$storyLn)
SayWait("Would you like me to continue?")
ControlCommand("SoundScript", ScriptStop)
# Ask if it should continue to the next story
$response = WaitForSpeech(10,"YES","NO")
# If yes
IF ($response = "YES")
  # Go back to the start
  Goto(begin)
  # Otherwise
ELSE 
  # Say OK
  ControlCommand("SoundScript", ScriptStart)
  SayWait("Ok")
  ControlCommand("SoundScript", ScriptStop)
ENDIF 
  # End of script
Halt()

Now, there may be a problem of the LEDs/Lamps staying on after the speech. It shouldn't happen but it could do. In which case, rather than using a ControlCommand("SoundScript", ScriptStop) command we can make another script to sort this out for us. A script that will be started, in the script it will stop the SoundScript and also set the PWM to 0.

ControlCommand("SoundScript", ScriptStop)
PWM(D7, 0)

And change any command line above that was ControlCommand("SoundScript", ScriptStop) to ControlCommand("SoundScriptStop", ScriptStart).

# RSS News Reading Script
# Author: Rich Pyke
# Version: 1.0.0
#
# RSS Script for reading news from RSS posts. Adapted from cafasarus
# script at https://synthiam.com/Community/Questions/3650

# Define variables
$feedurl = "http://feeds.reuters.com/Reuters/domesticNews"

# Do not change these variables
$storyLn = 0

# Start the code
:begin
# Increment the story line by 1
$storyLn++
# Print the story number
Print($storyLn)
# Speak the story
ControlCommand("SoundScript", ScriptStart)
SpeakRSSDescription($feedurl,$storyLn)
SayWait("Would you like me to continue?")
ControlCommand("SoundScriptStop", ScriptStart)
# Ask if it should continue to the next story
$response = WaitForSpeech(10,"YES","NO")
# If yes
IF ($response = "YES")
  # Go back to the start
  Goto(begin)
  # Otherwise
ELSE 
  # Say OK
  ControlCommand("SoundScript", ScriptStart)
  SayWait("Ok")
  ControlCommand("SoundScriptStop", ScriptStart)
ENDIF 
  # End of script
Halt()

This will always ensure that the LEDs/Lamps are off after speech has finished.

United Kingdom
#10  

@Rex, I guess that's similar, I just used another script to take care of the PWM(D#, 0). If you use the command a lot then it saves that second line however many times. Although either way works perfectly.

I'll post an updated script for just on and off next. It's much simpler so should only take a few minutes to throw together and test.

United Kingdom
#11  

If you don't want to dim the LED or lamp to the ranges with the PWM command for whatever reason, it is a simpler script to activate/deactivate the port based on the sound value.

# Sound servo to On or Off Script
# Author: Rich
# Version: 1.0.0
# Date: 26th June 2013

# Variables (do not adjust)
# $soundvalue = auto

# Main script
# Set the start point for the never ending loop
:loop
# Check the variable from the Sound servo control against a pre-determined level (you may need to change this) and set the port on or off to suit.
IF ($soundvalue < 10)
  # If low sound set the LED or lamps off
  Set(D7, off)
ELSE 
  # Otherwise set the LED or lamps to on
  Set(D7, on)
ENDIF 

# Sleep for 100ms to avoid saturation (you may need to adjust this to increase accuracy or increase stability. Reduce the delay for better accuracy, increase for better stability)
Sleep(100)

# Jump back to the start
Goto(loop)

Basically, what it does is it checks for low sound value, if it is low it will set D7 to off, the lamp will turn off. Otherwise it will set D7 to on, the lamp will turn on. Again, adjust the sound value level to suit your needs and the port where necessary.

In this case the SoundScriptStop script will need to be

ControlCommand("SoundScript", ScriptStop)
Set(D7, off)
United Kingdom
#12  

It can control as many or as few LEDs as you require, it uses the TIP transistor circuit therefore the current is restricted by the transistor and the EZ-B regulators. The Vcc can easily be modified to come from a different source and current restrictions are only that of the transistor. If that still poses a problem the transistor can be changed for one to suit your requirements better.

United Kingdom
#13  

How it works is like this;

The volume level picked up by the sound servo control The sound servo control stores this as the variable as defined in the control settings We use that variable in the script to check which range the volume level is in and set the PWM (or on/off status) as required The PWM command will simulate a lower voltage to the device thereby creating a dimming effect The Set command will enable or disable the device

To increase the accuracy of the script decrease the Sleep time at the end. This will require more processing and more bandwidth therefore may cause negative effects if set too low.

To increase performance and allow for other functions to communicate with the EZ-B increase the Sleep time at the end. However this will cause the lamp, LED etc. to react slower.

#14  

Thanks so much Rich. All of this is helping me understand the things I want to accomplish. Your time and effort are of tremendous benefit to all here. I can't wait to try this. Lot of drama going on at home right now so having trouble getting time to make progress.

#15  

Hi! I found this thread and was wondering if there were any recommended changes since the EZ B v4 came out.

Basically, I want LED (eye) lights to pulse with speech files. I am also going to try to wire a larger speaker to where the tiny one would be. I'll probably just use a speaker that has a headphone jack input +/- an amplifier.

Do I need to make anything like the TIP Transistor circuit to connect the LEDs?
It looks a bit complex for my capabilities. Is there a simpler method to achieve what I'm after?

Separately, I need to cause 12 different LEDs to randomly light up (not associated with sound). Can the EZ B 4 do this without tying up 12 ports/ plugs? I do have a way of doing this with some circuits that will blink the LEDs sequentially (I'll just put them in a random order on the robot), but it'd be nice if the EZ B could do everything.

thanks in advance,

Frank

PRO
Canada
#16  

Hi Frank,

You could do some Charliplexing if you'd like, you would only need 4 Digital lines to control 12 LEDs.

The equation to find how may LEDs you can control is n(n-1) where n is the number of Digital lines you have.

If you are unfamiliar with the concept Google it up if you'd like to know more:)

#17  

Thanks Jeremie. That's a more elegant solution, but defitiely beyond what I can figure out. I'll probably just activate a 2 sets of LED runner boards for the control panel lights. Is there an easy way to do that directly from the EZ B v4 or will I need something like this relay switch?

http://www.dimensionengineering.com/products/picoswitch

Frank

PRO
Canada
#18  

You couldn't light up a runner board of LEDs with a digital pin as there would be too much current draw (they are limited to 10mA), the simplest way would be to use a transistor or mosfet to pass the battery voltage directly to the runner board with a digital pin controlling it.

That Dimension engineering product looks like it would work if you sent it a servo command to it but it seems to be fairly expensive for what a 50 cent transistor can do.

There are a number of great tutorials on these forums such as this one that can help you put your circuit together.

Relays can work too but you likely won't need them unless your LEDs are very high in current draw (like 10Amps).

#19  

So what would I need to do to be able to set a desired servo position based on the sound level? I'm trying to make my servo respond rhythmically to exterior sounds (ie music, voices etc.)

#21  

@RichardR Ive been trying to use the Sound servo control to no avail. Maybe Im not setting it up properly but I set the servo ports and the range of min and max. Still no dice. This may sound dumb but is there a mic on the robot that is picking up the audio or is it capturing via my laptop mic? Have been also playing with microphone control and don't seem to be creating any waveforms. When I record, I only hear hiss during playback. Please help!

#22  

There is no mic on the ezb or Revolution robots... The only mic would be the one on your PC... Are you using sound servo PC or sound servo EZB? Sound servo PC uses the mic on your PC to move the servo (which works quite well)... Sound servo EZB does it without sound, it uses software to compute the sound levels and then apply them accordingly to control servo movement...

PRO
Canada
#23  

Hi @CWIST

You can look up your audio settings in windows at check if your microphone is working. There you can also increase the microphone gain if you need to in order to make sure your mic is picking up your voice.