Home Arduino Code Code for Arduino/Versalino Ultrasonic distance triggered Art Gallery audio
Code for Arduino/Versalino Ultrasonic distance triggered Art Gallery audio

Code for Arduino/Versalino Ultrasonic distance triggered Art Gallery audio

0

I was recently approached by a team that needed to trigger audio for their art (specifically photography) gallery at a a specific range and frequency (interval to be clear). I have been asked about similar projects by a lot of folks so far, so I decided I would help with the project, and draft up some plans and code to help them, and anyone else with similar projects.

This project is designed around the Virtuabotix Ultrasonic sensor, the Versalino Nano or Versalino Uno and the Versalino Sense & Move board. In the case of this particular gallery they are using the Sparkfun MP3 trigger board to play the audio itself.

/*####################################################################
FILE: Ultrasonic_trigger.cpp
VERSION: 1S0A
PURPOSE: Trigger MP3 audio or any other event with your Arduino
or with your Versalino system
LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html)

Designed for these products:
Virtuabotix Ultrasonic
https://www.virtuabotix.com/productpage/0609224531682/
Versalino Sense & Move
https://www.virtuabotix.com/productpage/609224531927/
Versalino Uno
https://www.virtuabotix.com/productpage/0609224531828/
Or Versalino Nano
https://www.virtuabotix.com/productpage/0609224532030/
and the Sparkfun MP3 Audio trigger board or anything else
that takes a digital trigger
GET UPDATES: https://www.virtuabotix.com/

HISTORY:
Joseph Dattilo (Virtuabotix LLC) - Version 1S0A (09/29/13)
#######################################################################*/

Okay so that is our coding header, be sure to include it if you choose to use or distribute anything based on this code, and thank you in advance for supporting open source coding and hardware. 🙂

The way we are going to set this up, is we are going to put the Sense & Move on BUSA and run our Virtuabotix Ultrasonic on the loadboard attached to our Versalino Nano (you can use a Versalino Uno too, but the Nano has a much smaller profile which makes it ideal for putting into a case or display).

The Versalino Audio trigger project before assembly.
The Versalino Audio trigger project before assembly.

We then attach a jumper wire between BUSB pin AN0 and the trigger of our choice on the MP3 trigger board (making sure to connect our grounds between the Versalino and the MP3 Trigger board to allow an electrical circuit between the two).

In this code we use the library for the Virtuabotix Ultrasonic https://www.virtuabotix.com/productpage/0609224531682/#tabs3-tab and the Versalino Library https://www.virtuabotix.com/productpage/0609224532030/#tabs3-tab so you should download both if you haven’t already and install them to your Arduino IDE.

#include
#include
//this assumes that the Sense & Move is on BUSA
Ultrasonic ultrasonic(BUSA.D2,BUSA.D2);

byte myTrigger = BUSB.AN0;//this is the trigger pin

int cmMsec; //stores centimeters
long microsec;//stores measurement

In the code above we setup our Ultrasonic object on BUSA.D2, the reason we call BUSA.D2 twice is because the Virtuabotix Ultrasonic Library and device is setup, and test with single wire interfacing. This is to say that you can tie both trigger and echo pins to each-other with our library. Other Ultrasonic libraries for the Arduino IDE do not support this feature so make sure you are using the Virtuabotix library as linked to above.

Additionally we declare the trigger pin, and variables in which to store out Ultrasonic measurements. If you need to change the pin the trigger is connected to, just change the pin that is assigned to the myTrigger variable above. Additionally if you need to have the Ultrasonic on BUSB instead of BUSA then all you need to do is change the assignments above before physically switching them.

void setup()
{
pinMode(myTrigger, OUTPUT);//set our trigger as an output
digitalWrite(myTrigger, HIGH);//set our trigger high which means off
}

In this Arduino Sketch our setup is actually pretty simple since we did most of our preparation outside of the setup call. Basically all we are doing in the code above is making our trigger pin for our MP3 board an output, and then making sure it is held HIGH(which means at VDD/5V) by calling the digitalWrite() after we called pinMode().

void loop()
{
//below we take our measurement, and convert it to centimeters
cmMsec = (int) ultrasonic.convert(ultrasonic.timing(), Ultrasonic::CM);
while(cmMsec <= 40 || cmMsec >= 543)//loop while outside of operational range
{
delay(75);//this is the minimum time between measurements
//you can adjust it, but this delay has been quite stable for me
cmMsec = (int) ultrasonic.convert(ultrasonic.timing(), Ultrasonic::CM);
// ^ above we take another measurement, because we are still not in range ^
}

if(cmMsec <= 182)//less than = 6 feet or 182 cm then trigger { digitalWrite(myTrigger, LOW); delay(400);//adjust this delay if the trigger does not work digitalWrite(myTrigger, HIGH); delay(180000);//wait 3 minutes before allowing a second trigger //adjust the above delay to make the event/sound duration for your trigger } //once out of the if statement we start the loop over. }

Now we finally get to the meet and potatoes of the project in the loop() section of our code. If you aren't familiar with Ultrasonic sensors, then you may be interested to know that the way they work is to measure the number of microseconds it takes for a high pitch frequency to bounce off of something and return to the sensor. This is the same way a bat or submarine sonar works, and can be really handy, but it is possible to get returns from previous attempts at measurements if you go to fast or have too many in one place so keep that in mind.

Our first line of code takes the measurement of microseconds, and then converts that to centimeters by dividing the time to return by 2 (the sound goes there and back so it is twice the distance you are measuring) and then multiply by the speed of sound which is 34.3 centimeters per millisecond(cM/mS). Of course we are operating in microseconds, so that means we divide our microseconds by 2, and then multiply by 0.0343 centimeters per microsecond (cM/uS).

We then check to make sure we are between 40 and 543 centimeters (which I found to be a great way to get rid of false readings in some previous outdoor Virtuabotix Ultrasonic projects). Once we are in that range we will check that we are inside our trigger range which for this particular Art Gallery was 6 feet or approximately 182 centimeters. Thus if(cmMsec <= 182).

If we are in range we want to trigger our MP3 board by setting our trigger pin high, and then we wait 0.4 seconds to make sure it triggers the audio playback successfully. You can tweak the time stays high by adjusting the delay. After the delay we take the trigger back to low, and wait the time it takes for the audio to complete, and enough time to keep it sounding natural before the next trigger is allowed (in the case of this particular frame that is 3 minutes or 180000 milliseconds). After that we start all over and wait until we are triggered again.

So that is everything you need to do on the code side of things to get a Virtuabotix Ultrasonic to trigger an MP3 Audio board with the Versalino Uno or Nano! I hope this helps you with your next project, and I can't wait to see videos or hear stories about your own Ultrasonic triggers if you use this in your Art Gallery or show.

Joseph Dattilo Writer, Electrical Engineer, CEO and founder of Virtuabotix LLC, and completely crazy in every way.

LEAVE YOUR COMMENT

Your email address will not be published. Required fields are marked *