N00b - Theremin sensor HC-SR04 project, need to add 2nd or more sensor

General project discussion / help
Did you make a robotic coffee pot which implements HTCPCP and decafs unauthorized users? Show it off here!

Moderator: phalanx

Post Reply
lorraine
Posts: 1
Joined: Tue Dec 11, 2018 11:45 am

N00b - Theremin sensor HC-SR04 project, need to add 2nd or more sensor

Post by lorraine » Tue Dec 11, 2018 3:31 pm

Dear project people,I am an artist who is working on a project for an upcoming show. First, I would like to say I am a super n00b and would like to apologize for my ignorance The project I am working on makes use of HC-SR04 ultrasonic sensors and people moving. At the moment the sensors trigger a keyboard I have set-up in REAPER. When the viewer activates the sensor it triggers a sound. I have a code that works great for one sensor but I will eventually need to wire up 6-10 (sensors). I am unclear on how to make that happen. This is the code I am using at the moment.


#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();

int pingPin = 13;
int inPin = 12;
long duration;
int cm_new = 20;
int cm_old = 20;
int sens = 5; // sensivity range when to launch a new note

void setup() {

Serial.begin(9600); // MIDI Begin
pinMode(pingPin, OUTPUT); // setup the Pins
pinMode(inPin, INPUT); // setup the Pins

}

void loop()
{


// The PING is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:

digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin, LOW);
duration = pulseIn(inPin, HIGH); // the singal from the Sensor is measured. The length of the pulse describes the distance form an object to the sensor.

cm_new = map(constrain(duration, 20, 3000), 50, 2000, 96, 48); // contrain --> set a threshold to cut values of the sensor that are to far away of to short (20 .. 3000) MAP --> Map the sensor range to a note values vom 96 (A high C) to 48 (a low C)

if (cm_new != cm_old) // only if a new sensor value is detected, a new note will be send. IF the object stays stable infront of the sensor, no note will be send.
{
MIDI.sendNoteOff(cm_old,0,1); // Stop the old note
MIDI.sendNoteOn(cm_new,127,1); // Send a new Note (pitch , velocity 127, on channel 1)
cm_old = cm_new;
}
delay(30); // for staiblity
}




I have also attached a pic to see what I'm up to… https://www.instagram.com/p/BrNvEtmn3Va ... hare_sheet

Not sure what changes are needed in the code. Help would be very appreciated appreciated!
I can even show you a video of the show once it's up!
Thanks for your time and help!

jremington
Support Volunteer
Posts: 2342
Joined: Fri Jun 15, 2007 9:41 pm
Location: Eugene, Or

Re: N00b - Theremin sensor HC-SR04 project, need to add 2nd or more sensor

Post by jremington » Tue Dec 11, 2018 8:04 pm

Cool project! Please use code tags ("</>" button) to post code, so that it looks like this:

Code: Select all

digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin, LOW);
duration = pulseIn(inPin, HIGH); // the singal from the Sensor is measured. The length of the pulse describes the distance form an object to the sensor.
For each sonar sensor, you have to repeat the above, with each sensor ping and input connected to different pins, obviously. The sensors must be fired sequentially, with a few milliseconds delay between each sensor action. Otherwise the sensors (along with possible multiple sound reflections) will interfere with each other. With several sensors, you will need to adjust that delay time to minimize sensor cross talk, and may find that the overall ranging cycle is so slow as to be impractical.

You might consider instead using optical time of flight distance sensors, like the VL53L0X, which are much faster, quite directional and usually do not interfere with each other. See https://www.pololu.com/product/2490

Post Reply