Need to modify code to support 8 HC SR04 sensors rather than 1.

EDIT: skip to my post on page 2 for an update of where I am and all questions i’ve been asked answered (rather than reading through my foolishness on the first page)

Hey folks.

After having a very friendly person help me figure out how to code for 1 sensor for a midi controller over in the audio section, I have come here for help in using multiple sensors to complete the current project I’m working on. (my initial problems here for those curious http://forum.arduino.cc/index.php?topic=192034.0)

This is my first project and trawling through google has given me a rough understanding that what I want to do is hard.

I have found http://forum.arduino.cc/index.php/topic,17898.0.html which has a suggestion for a different sensor, but in terms of actually analysing their code and why it works-well it’s all gibberish to me and as such i’ve no idea how to make it applicable to what I’m doing.

Anyway-What I am looking to be able to do is have each sensor feed their data into a different instance of hairless where using loopmidi i will be able to assign them as controllers to a modx knob in FL studio’s sytrus. (they are all going to a different sytrus instance and as such a different modx knob)

The code i have for a single one to work (and it works the full way) is

#include <MIDI.h>

const int trigPin = 2;
const int echoPin = 4;
 
void setup() {
  MIDI.begin();
  Serial.begin(115200);
}
 
void loop()
{
  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  long duration, inches, cm;
 
  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
 
  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
 
  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  
  //Serial.print(inches);
  //Serial.print("in, ");
  //Serial.print(cm);
  //Serial.print("cm");
  //Serial.println();
  
  cm = map(cm, 0, 250, 0, 127);
  MIDI.sendControlChange(7
 , cm, 1);
  
 
}
 
long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}
 
long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

I have used the midi library for this.

Any help is greatly appreciated.

Thanks

Never used any other sensor than ribbons and pots. But by looking at the code... The easier would be to just duplicate. Instead of wiring it to pin 2 and 4, use 5 and 6 for the 2nd sensor.

const int trigPin = 2;
const int echoPin = 4;
const int trigPin2 = 5;
const int echoPin2 = 6;

then also declare more variables for the 2nd sensor

void loop(){
long duration, inches, cm;
 long duration2, cm2;
  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(trigPin, OUTPUT);
 pinMode(trigPin2, OUTPUT);
  digitalWrite(trigPin, LOW);
digitalWrite(trigPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
 digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
digitalWrite(trigPin2, LOW);

  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(echoPin, INPUT);
 pinMode(echoPin2, INPUT);
duration2 = pulseIn(echoPin2, HIGH);
  duration = pulseIn(echoPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
cm2 = microsecondsToCentimeters2(duration2);
// i suppose you could use the same function, no need for other microsecondsToCentimeters but not sure

}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

long microsecondsToCentimeters2(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

you can delete the inches part if you're not gonna use them

Disregard following: I’m a ruddy fool who likes to type then check for a way around-needed to open a new file, not new tab. /facepalm

Going to test this extensively.

Hmmm-i’ve tried that (without yet deleting inches as i want to clearly see which stage of changing code is problematic).
Im getting an error message of
~~ ~~Double_Sensor:3: error: redefinition of 'const int trigPin' Single_Sensor:3: error: 'const int trigPin' previously defined here Double_Sensor:4: error: redefinition of 'const int echoPin' Single_Sensor:4: error: 'const int echoPin' previously defined here Double_Sensor.ino: In function 'void setup()': Double_Sensor:8: error: redefinition of 'void setup()' Single_Sensor:6: error: 'void setup()' previously defined here Double_Sensor.ino: In function 'void loop()': Double_Sensor:13: error: redefinition of 'void loop()' Single_Sensor:11: error: 'void loop()' previously defined here Double_Sensor:18: error: redeclaration of 'long int inches' Double_Sensor:17: error: 'long int inches' previously declared here Double_Sensor:23: error: 'trigPin2' was not declared in this scope Double_Sensor.ino: In function 'long int microsecondsToInches(long int)': Double_Sensor:56: error: redefinition of 'long int microsecondsToInches(long int)' Single_Sensor:49: error: 'long int microsecondsToInches(long int)' previously defined here Double_Sensor.ino: In function 'long int microsecondsToCentimeters(long int)': Double_Sensor:66: error: redefinition of 'long int microsecondsToCentimeters(long int)' Single_Sensor:59: error: 'long int microsecondsToCentimeters(long int)' previously defined here~~ ~~
The full code I have at the moment is
```
~~#include <MIDI.h>

const int trigPin = 2;
const int echoPin = 4;
const int trigpin2 =5;
const int echopin2 =6;

void setup() {
 MIDI.begin();
 Serial.begin(115200);
}

void loop()
{
 // establish variables for duration of the ping,
 // and the distance result in inches and centimeters:
 long duration, inches, cm;
 long duration2, inches, cm2;

// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
 // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(trigPin, OUTPUT);
pinMode(trigPin2, OUTPUT);
 digitalWrite(trigPin, LOW);
digitalWrite(trigPin2, LOW);
 delayMicroseconds(2);
 digitalWrite(trigPin, HIGH);
digitalWrite(trigPin2, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPin, LOW);
digitalWrite(trigPin2, LOW);

// Read the signal from the sensor: a HIGH pulse whose
 // duration is the time (in microseconds) from the sending
 // of the ping to the reception of its echo off of an object.
 pinMode(echoPin, INPUT);
 duration = pulseIn(echoPin, HIGH);

// convert the time into a distance
 inches = microsecondsToInches(duration);
 cm = microsecondsToCentimeters(duration);
 
 //Serial.print(inches);
 //Serial.print("in, ");
 //Serial.print(cm);
 //Serial.print(“cm”);
 //Serial.println();
 
 cm = map(cm, 0, 250, 0, 127);
 MIDI.sendControlChange(7, cm, 1);

}

long microsecondsToInches(long microseconds)
{
 // According to Parallax’s datasheet for the PING))), there are
 // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
 // second).  This gives the distance travelled by the ping, outbound
 // and return, so we divide by 2 to get the distance of the obstacle.
 // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
 return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
 // The speed of sound is 340 m/s or 29 microseconds per centimeter.
 // The ping travels out and back, so to find the distance of the
 // object we take half of the distance travelled.
 return microseconds / 29 / 2;
}~~
```
Honestly I have no idea what’s happening in terms of errors-looking at your code I get how that would work in an intuitive system. I’ve had a thought in terms of simplifying the process past the Arduino, but it would probably need programming on the Arduino itself. Is it possible to specify that sensor 1 would go to MIDI.sendControlChange(7, cm, 1); but have sensor 2 go to MIDI.sendControlChange(7, cm, 2);?
In my mind that would allow me to only open 1 instance of hairless and simply selecting the channel of the controller in FL rather than running each as a separate midi control? If that makes it harder on this end-it probably wont be worth it, but it seems like it might get easier here with a change like that.

In the last code you're not reading the 2nd sensor yet. With this you're sending the same value to 2 different channels.

 MIDI.sendControlChange(7, cm, 1);
MIDI.sendControlChange(7, cm, 2);

i still don't know what you want to do with 8 sensors? but if you want to control different parameters...

MIDI.sendControlChange(7, cm, 1); // sensor 1 controls volume
MIDI.sendControlChange(10, cm2, 1); //sensor 2 controls pan

In my mind that would allow me to only open 1 instance of hairless and simply selecting the channel of the controller in FL rather than running each as a separate midi control? If that makes it harder on this end-it probably wont be worth it, but it seems like it might get easier here with a change like that.[/s]

I do not think you can open several instances of hairless and have them all talk to the same serial port. Define better what you want to acheave.

Grumpy_Mike:

In my mind that would allow me to only open 1 instance of hairless and simply selecting the channel of the controller in FL rather than running each as a separate midi control? If that makes it harder on this end-it probably wont be worth it, but it seems like it might get easier here with a change like that.[/s]

I do not think you can open several instances of hairless and have them all talk to the same serial port. Define better what you want to acheave.

Ok-what i want to do is send the data from the 8 sensors though hairless in such a way that from my music editing software (FL studio) I will be able to use them as separate controllers for the volumes of 8 different tracks.

Hopefully that was clearer.

So to do that you need to know what MIDI commands FL studio expects for volume control for each track.

FL is a black box... Does it have MIDI learn? I suppose it expects 7 CC as volume. You can have each track in a different MIDI channel so maybe something like this

MIDI.sendControlChange(7, sensor1, 1);
MIDI.sendControlChange(7, sensor2, 2);
MIDI.sendControlChange(7, sensor3, 3);
MIDI.sendControlChange(7, sensor4, 4);
MIDI.sendControlChange(7, sensor5, 5);
MIDI.sendControlChange(7, sensor6, 6);
MIDI.sendControlChange(7, sensor7, 7);
MIDI.sendControlChange(7, sensor8, 8);

You could do that with a for() to make it "better"...

EDIT:found mistake in my code-will fix post when fixed

capicoso:
In the last code you’re not reading the 2nd sensor yet.
With this you’re sending the same value to 2 different channels.

 MIDI.sendControlChange(7, cm, 1);

MIDI.sendControlChange(7, cm, 2);



i still don't know what you want to do with 8 sensors?
but if you want to control different parameters...


MIDI.sendControlChange(7, cm, 1); // sensor 1 controls volume
MIDI.sendControlChange(10, cm2, 1); //sensor 2 controls pan

I’m creating an installation to do with the harmonic series. The sensors with pick up proximity with passing humans, and create various tone colors depending on their positions. The pitch will be constant-but the harmonic change should a) bring more awareness to how cool the harmonic series is and b) be something to play with.

At the moment the data is getting to the program I believe as the green light for unhandled data is flashing. Unfortunately the harmonics i have assigned to the sensors are not working properly with it. It does work when I use the code for a single sensor as done in the topic in the audio forum, so either the info is getting confused, or there is a problem on FL’s side i need to fix.

You help has been great guys.

#include <MIDI.h>

const int trigPin = 2;
const int echoPin = 4;
const int trigPin2 =36;
const int echoPin2 =37;
 
void setup() {
  MIDI.begin();
  Serial.begin(115200);
}
 
void loop()
{
  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  long duration, cm;
  long duration2, cm2;
 
  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
 pinMode(trigPin, OUTPUT);
 pinMode(trigPin2, OUTPUT);
  digitalWrite(trigPin, LOW);
digitalWrite(trigPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
 digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
digitalWrite(trigPin2, LOW);
 
  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
 
  // convert the time into a distance
  cm = microsecondsToCentimeters(duration);
  
  //Serial.print(cm);
  //Serial.print("cm");
  //Serial.println();
  
  cm = map(cm, 0, 250, 0, 127);
  MIDI.sendControlChange(7, cm, 1);
  MIDI.sendControlChange(7, cm, 2);
  
 
}
  
long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

I am unsure about midi learn on FL-haven’t had to use it-will find out for you.

Minrat:
EDIT:found mistake in my code-will fix post when fixed

capicoso:
In the last code you’re not reading the 2nd sensor yet.
With this you’re sending the same value to 2 different channels.

 MIDI.sendControlChange(7, cm, 1);

MIDI.sendControlChange(7, cm, 2);



i still don't know what you want to do with 8 sensors?
but if you want to control different parameters...


MIDI.sendControlChange(7, cm, 1); // sensor 1 controls volume
MIDI.sendControlChange(10, cm2, 1); //sensor 2 controls pan

I’m creating an installation to do with the harmonic series. The sensors with pick up proximity with passing humans, and create various tone colors depending on their positions. The pitch will be constant-but the harmonic change should a) bring more awareness to how cool the harmonic series is and b) be something to play with.

At the moment the data is getting to the program I believe as the green light for unhandled data is flashing. Unfortunately the harmonics i have assigned to the sensors are not working properly with it. It does work when I use the code for a single sensor as done in the topic in the audio forum, so either the info is getting confused, or there is a problem on FL’s side i need to fix.

You help has been great guys.

#include <MIDI.h>

const int trigPin = 2;
const int echoPin = 4;
const int trigPin2 =36;
const int echoPin2 =37;

void setup() {
 MIDI.begin();
 Serial.begin(115200);
}

void loop()
{
 // establish variables for duration of the ping,
 // and the distance result in inches and centimeters:
 long duration, cm;
 long duration2, cm2;

// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
 // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(trigPin, OUTPUT);
pinMode(trigPin2, OUTPUT);
 digitalWrite(trigPin, LOW);
digitalWrite(trigPin2, LOW);
 delayMicroseconds(2);
 digitalWrite(trigPin, HIGH);
digitalWrite(trigPin2, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPin, LOW);
digitalWrite(trigPin2, LOW);

// Read the signal from the sensor: a HIGH pulse whose
 // duration is the time (in microseconds) from the sending
 // of the ping to the reception of its echo off of an object.
 pinMode(echoPin, INPUT);
 duration = pulseIn(echoPin, HIGH);

// convert the time into a distance
 cm = microsecondsToCentimeters(duration);
 
 //Serial.print(cm);
 //Serial.print(“cm”);
 //Serial.println();
 
 cm = map(cm, 0, 250, 0, 127);
 MIDI.sendControlChange(7, cm, 1);
 MIDI.sendControlChange(7, cm, 2);

}
 
long microsecondsToCentimeters(long microseconds)
{
 // The speed of sound is 340 m/s or 29 microseconds per centimeter.
 // The ping travels out and back, so to find the distance of the
 // object we take half of the distance travelled.
 return microseconds / 29 / 2;
}




I am unsure about midi learn on FL-haven't had to use it-will find out for you.

I’ve never used that hairless program, but does it have a monitor? I’m not sure on windows/mac but you can use MIDI-oX to monitor what the pc is getting… On linux i just use Puredata…
With that last code you’re using only one sensor and sending the same value to the same parameter but different channel, check the configurations on fruity loops

It does have a monitor-but it ceases displaying when more than one signal is sent.

Yeah-i just fixed that code-it is now

#include <MIDI.h>

const int trigPin = 2;
const int echoPin = 4;
const int trigPin2 =36;
const int echoPin2 =37;
 
void setup() {
  MIDI.begin();
  Serial.begin(115200);
}
 
void loop()
{
  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  long duration, cm;
  long duration2, cm2;
 
  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
 pinMode(trigPin, OUTPUT);
 pinMode(trigPin2, OUTPUT);
  digitalWrite(trigPin, LOW);
digitalWrite(trigPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
 digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
digitalWrite(trigPin2, LOW);
 
  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
 
  // convert the time into a distance
  cm = microsecondsToCentimeters(duration);
  
  //Serial.print(cm);
  //Serial.print("cm");
  //Serial.println();
  
  cm = map(cm, 0, 100, 0, 127);
  cm2 = map(cm, 0, 100, 0, 127);
  
  MIDI.sendControlChange(7, cm, 1);
  MIDI.sendControlChange(7, cm2, 2);
  
 
}
  
long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

So far I can make 1 at a time work in FL which is progress from before. Slowly i’m getting an understanding of the interactions and soon I should have this running. As I am connecting to a modx rather than straight volume, am going to see if changing the first value of “MIDI.sendControlChange(7, cm, 2);” instead of the second is clearer to my software.

EDIT: My ‘solution’ just made things break-sticking with initial plan of changing last digit. I think I may need to test my second sensor independently. It moves slightly on the grid, but does not take account of small movements like the first is they are close. In fact-the ends of the wires are getting a bit stuffy-brb soldering

Minrat:
It does have a monitor-but it ceases displaying when more than one signal is sent.

Yeah-i just fixed that code-it is now

#include <MIDI.h>

const int trigPin = 2;
const int echoPin = 4;
const int trigPin2 =36;
const int echoPin2 =37;

void setup() {
 MIDI.begin();
 Serial.begin(115200);
}

void loop()
{
 // establish variables for duration of the ping,
 // and the distance result in inches and centimeters:
 long duration, cm;
 long duration2, cm2;

// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
 // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(trigPin, OUTPUT);
pinMode(trigPin2, OUTPUT);
 digitalWrite(trigPin, LOW);
digitalWrite(trigPin2, LOW);
 delayMicroseconds(2);
 digitalWrite(trigPin, HIGH);
digitalWrite(trigPin2, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPin, LOW);
digitalWrite(trigPin2, LOW);

// Read the signal from the sensor: a HIGH pulse whose
 // duration is the time (in microseconds) from the sending
 // of the ping to the reception of its echo off of an object.
 pinMode(echoPin, INPUT);
 duration = pulseIn(echoPin, HIGH);

// convert the time into a distance
 cm = microsecondsToCentimeters(duration);
 
 //Serial.print(cm);
 //Serial.print(“cm”);
 //Serial.println();
 
 cm = map(cm, 0, 100, 0, 127);
 cm2 = map(cm, 0, 100, 0, 127);
 
 MIDI.sendControlChange(7, cm, 1);
 MIDI.sendControlChange(7, cm2, 2);

}
 
long microsecondsToCentimeters(long microseconds)
{
 // The speed of sound is 340 m/s or 29 microseconds per centimeter.
 // The ping travels out and back, so to find the distance of the
 // object we take half of the distance travelled.
 return microseconds / 29 / 2;
}




So far I can make 1 at a time work in FL which is progress from before. Slowly i'm getting an understanding of the interactions and soon I should have this running. As I am connecting to a modx rather than straight volume, am going to see if changing the first value of "MIDI.sendControlChange(7, cm, 2);" instead of the second is clearer to my software.

There are missing a few things i think…

 pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);

you have to do that to echoPin2 as well, with another variable than duration, maybe, duration2.
and cm2 is missing something too

 cm = microsecondsToCentimeters(duration);

you should add

 cm2 = microsecondsToCentimeters(duration2);

You could try to test if the both sensor part is working ok, ignoring the MIDI part, just go back to the sketch with the serial 9600 and the serial.print lines, but change them to read cm2 too

Just saw the rest of you first reply when i looked back after that last post-completely missed the section I would have had to scroll down for. Thanks for your help and presumably your patience as i wafted past things you had already written.

Fixing my code now so anything to do with distance has a 2 to do with it as well.

EDIT:

Would i need to add a 2nd version of

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

any of that? (if i get no quick response will probs end up just testing it-but i figure you might know offhand if you are still online)

Ok-something has happened that is annoying. The sensors are now only sensing less than once per second.

Also, while this was a problem before I assumed it would be fixed with coding better stuff (which I may or may not have done). While in FL I have 1 knob for port 0 channel 2 control 7, and the other for port 0 channel 1 control 7, they are both only being affected by the sensor plugged into arduino ports 2 and 4. I had assumed this to do with me mixing up ‘cm’ and ‘cm2’ but i can’t see where i’ve done that.

#include <MIDI.h>

const int trigPin = 2;
const int echoPin = 4;
const int trigPin2 =36;
const int echoPin2 =37;
 
void setup() {
  MIDI.begin();
  Serial.begin(115200);
}
 
void loop()
{
  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  long duration, cm;
  long duration2, cm2;
 
  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
 pinMode(trigPin, OUTPUT);
 pinMode(trigPin2, OUTPUT);
 digitalWrite(trigPin, LOW);
 digitalWrite(trigPin2, LOW);
  delayMicroseconds(2);
 digitalWrite(trigPin, HIGH);
 digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10);
 digitalWrite(trigPin, LOW);
 digitalWrite(trigPin2, LOW);
 
  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(echoPin, INPUT);
  pinMode(echoPin2, INPUT);
  duration = pulseIn(echoPin, HIGH);
  duration2 = pulseIn(echoPin2, HIGH);
 
  // convert the time into a distance
  cm = microsecondsToCentimeters(duration);
  cm2 = microsecondsToCentimeters2(duration2);
  
  //Serial.print(cm);
  //Serial.print("cm");
  //Serial.println();
  
  cm = map(cm, 0, 100, 0, 127);
  cm2 = map(cm, 0, 100, 0, 127);
  
  MIDI.sendControlChange(7, cm, 1);
  MIDI.sendControlChange(7, cm2, 2);
  
 
}
  
long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

long microsecondsToCentimeters2(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

going to start taking out the comments as this gets bigger

EDIT: additionally i went to test both the sensors with the single sensor code

#include <MIDI.h>

const int trigPin = 2;
const int echoPin = 4;
 
void setup() {
  MIDI.begin();
  Serial.begin(115200);
}
 
void loop()
{
  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  long duration, inches, cm;
 
  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
 
  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
 
  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  
  //Serial.print(inches);
  //Serial.print("in, ");
  //Serial.print(cm);
  //Serial.print("cm");
  //Serial.println();
  
  cm = map(cm, 0, 250, 0, 127);
  MIDI.sendControlChange(7, cm, 1);
  
 
}
 
long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}
 
long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

there is only delay when no object is within sensor range-otherwise readings continue extremely quickly for the first sensor I have. The second sensor is always slow. Additionally, all readings I get from within Hairless carry the error message ‘+2.996 - Error: got unexpected data byte 0x*.’ where * is a number. It will not link to my loopmidi.


EDIT2:Kind of related to above, i ran the 2 sensor code above but before starting hairless opened and closed the serial monitor.

That resulted in the following in the hairless display:

Starting MIDI<->Serial Bridge...
Opening serial port 'COM3'...
FTDI drivers don't seem to be installed. Not attempting any FTDI latency tricks.
Opening MIDI Out port #9
+3.415 - Serial In: Ch 1: Controller 7 value 0
+3.415 - Serial In: Ch 2: Controller 7 value 0
+5.93 - Serial In: Ch 1: Controller 7 value 0
+5.93 - Serial In: Ch 2: Controller 7 value 0
+8.445 - Serial In: Ch 1: Controller 7 value 0
+8.445 - Serial In: Ch 2: Controller 7 value 0
+10.959 - Serial In: Ch 1: Controller 7 value 0
+10.963 - Serial In: Ch 2: Controller 7 value 0
+13.478 - Serial In: Ch 1: Controller 7 value 0
+13.478 - Serial In: Ch 2: Controller 7 value 0
+14.674 - Serial In: Ch 1: Controller 7 value 6
+14.674 - Serial In: Ch 2: Controller 7 value 7
+15.871 - Serial In: Ch 1: Controller 7 value 3
+15.871 - Serial In: Ch 2: Controller 7 value 3
+17.067 - Serial In: Ch 1: Controller 7 value 2
+17.067 - Serial In: Ch 2: Controller 7 value 2
etc

etc

+23.596 - Closing MIDI<->Serial bridge...

Both values changed when I waved my hand over the sensor occupying 2 and 4, with no change with it over the sensor occupying 36 and 37.

EDIT no. something or other tested both sensors with default non midi thing-both function correctly as a single with

const int trigPin = 36;
const int echoPin = 37;
 
void setup() {
  // initialize serial communication:
  Serial.begin(9600);
}
 
void loop()
{
  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  long duration, inches, cm;
 
  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
 
  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
 
  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  
  delay(100);
}
 
long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}
 
long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

http://code.google.com/p/arduino-new-ping/wiki/15_Sensors_Example

@trembo

Thankyou for the link. I am quite unsure of the best way for me to appropriate it. This is my first project and I have extremely limited knowledge of the programming code.

In a bit i will see what happens if I plug it straight in with little change and advise of what I have done so that if this is a good path to take, yourself and other could possibly help me with touching it up.

In terms of the coding I was trying before, I went back to my 9600 daub version and tried to make it work for 2 but was unsuccessful. The sensor that used 'trigPin' and 'echoPin' was fine (swapped around the numbers-both sensors worked when assigned to this terminology), but the other would always come up as 0. This would probably explain the problems I am having with my midi version currently.

const int trigPin = 36;
const int trigPin2 = 2;
const int echoPin = 37;
const int echoPin2 = 4;

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
}

void loop()
{
  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  long duration, cm;
  long duration2, cm2;

  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(trigPin, OUTPUT);
  pinMode(trigPin2, OUTPUT);
  digitalWrite(trigPin, LOW);
  digitalWrite(trigPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  digitalWrite(trigPin2, LOW);

  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(echoPin, INPUT);
  pinMode(echoPin2, INPUT);
  duration = pulseIn(echoPin, HIGH);
  duration2 = pulseIn(echoPin2, HIGH);

  // convert the time into a distance
  cm = microsecondsToCentimeters(duration);
  cm2 = microsecondsToCentimeters2(duration2);
  
  Serial.print(cm);
  Serial.print("cm");
  Serial.print(cm2);
  Serial.print("cm");
  Serial.println();
  
  delay(100);
}


long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}
long microsecondsToCentimeters2(long microseconds)
{
  return microseconds / 29 / 2;
}

and heres the data from the serial monitor if that helps anyone

54cm0cm
53cm0cm
53cm0cm
54cm0cm
54cm0cm
54cm0cm
54cm0cm
54cm0cm
54cm0cm
54cm0cm
54cm0cm
54cm0cm
54cm0cm
54cm0cm
54cm0cm
54cm0cm
54cm0cm
54cm0cm
54cm0cm
54cm0cm
54cm0cm
54cm0cm
54cm0cm
54cm0cm
54cm0cm
54cm0cm
53cm0cm
54cm0cm
54cm0cm
54cm0cm
54cm0cm
54cm0cm
53cm0cm
54cm0cm
54cm0cm
54cm0cm
54cm0cm
54cm0cm
54cm0cm
54cm0cm
53cm0cm
54cm0cm
54cm0cm
54cm0cm
54cm0cm
53cm0cm
54cm0cm
53cm0cm
54cm0cm
53cm0cm
53cm0cm
53cm0cm
53cm0cm
53cm0cm
53cm0cm
54cm0cm
54cm0cm
54cm0cm
54cm0cm
54cm0cm
53cm0cm
54cm0cm
54cm0cm
54cm0cm
54cm0cm
54cm0cm
53cm0cm
54cm0cm
53cm0cm
55cm0cm
58cm0cm
58cm0cm
58cm0cm
62cm0cm
9cm0cm
7cm0cm
4cm0cm
3cm0cm
3cm0cm
3cm0cm
3cm0cm
3cm0cm
3cm0cm
3cm0cm
3cm0cm
3cm0cm
3cm0cm
3cm0cm
3cm0cm
3cm0cm
3cm0cm
3cm0cm
3cm0cm
3cm0cm
3cm0cm
3cm0cm
3cm0cm
3cm0cm
3cm0cm
3cm0cm
3cm0cm
3cm0cm
3cm0cm
3cm0cm
3cm0cm
3cm0cm
3cm0cm
3cm0cm
3cm0cm
3cm0cm
3cm0cm
3cm0cm

Putting a where I am currently post in here-to long to fit in one and since I was the last to respond, starting it in this space.


Hey guys-some of my responses may have been convoluted/hard to follow so I am just going to respond to any questions i may have missed about what I'm doing here, and well as post the possible code I have going that are not working, and what happens when I try them.

Sorry about prior ramblings-this post should include all I have done to solve my problem and show obstacles to my solutions that I have not yet overcome.

Define better what you want to acheave.

I wish to have 8 sensors each sending a volume signal to a voice in FL studio. I currently intend to do this by sending each sensor's data through a different channel of the volume MIDI parameter for FL (which is 7).

You could do that with a for() to make it "better"...

Is for() coding essential? Will it make the actual process quicker-or is it just for better visibility?

Does it have MIDI learn?

Yes. Sorry-my brain farted yesterday. It does have that.

I've never used that hairless program, but does it have a monitor?

It does have a monitor. Something interesting that happens that is related to this question: If i don't first open and close the Arduino programs serial monitor, I will receive error messages or nothing at all on the hairless monitor. That is why it had not been working previously. I do not know if that quirk is unique to my project or just a fault of the program itself.

(continued from latter half of above post)

You could try to test if the both sensor part is working ok, ignoring the MIDI part, just go back to the sketch with the serial 9600 and the serial.print lines, but change them to read cm2 too

I have just tried this recently. Here is my current code for it:

const int trigPin = 36;
const int trigPin2 = 2;
const int echoPin = 37;
const int echoPin2 = 4;
 
void setup() {
  // initialize serial communication:
  Serial.begin(9600);
}
 
void loop()
{
  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  long duration, cm;
  long duration2, cm2;
 
  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(trigPin, OUTPUT);
  pinMode(trigPin2, OUTPUT);
  digitalWrite(trigPin, LOW);
  digitalWrite(trigPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  digitalWrite(trigPin2, LOW);
 
  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(echoPin, INPUT);
  pinMode(echoPin2, INPUT);
  duration = pulseIn(echoPin, HIGH);
  duration2 = pulseIn(echoPin2, HIGH);
 
  // convert the time into a distance
  cm = microsecondsToCentimeters(duration);
  cm2 = microsecondsToCentimeters2(duration2);
  
  Serial.print(cm);
  Serial.print("cm");
  Serial.print(cm2);
  Serial.print("cm");
  Serial.println();
  
  delay(100);
}
 
 
long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}
long microsecondsToCentimeters2(long microseconds)
{
  return microseconds / 29 / 2;
}

What happens is that the first reading is always valid and correct, but the second is always 0. If I swap the sensor allocation on the arduino code to

const int trigPin = 2;
const int trigPin2 = 36;
const int echoPin = 4;
const int echoPin2 = 37;

The results is the same. Meaning that the first reading now coming from a different sensor is now accurate while the other is always 0.

http://code.google.com/p/arduino-new-ping/wiki/15_Sensors_Example

I have tried using this but am unfamiliar with much of the variables and how to code them. This is largely due to my ineptitude, and I’m trying to learn but it is slow.

What I have got so far is

// ---------------------------------------------------------------------------

#include <NewPing.h>

#define SONAR_NUM     2 // Number of sensors.
#define MAX_DISTANCE 100 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 33 // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo).

unsigned long pingTimer[SONAR_NUM]; // Holds the times when the next ping should happen for each sensor.
unsigned int cm[SONAR_NUM];         // Where the ping distances are stored.
uint8_t currentSensor = 0;          // Keeps track of which sensor is active.

NewPing sonar[SONAR_NUM] = {     // Sensor object array.
  NewPing(2, 4, 100), // Each sensor's trigger pin, echo pin, and max distance to ping.
  NewPing(36, 37, 100)
};

void setup() {
  Serial.begin(115200);
  pingTimer[0] = millis() + 75;           // First ping starts at 75ms, gives time for the Arduino to chill before starting.
  for (uint8_t i = 1; i < SONAR_NUM; i++) // Set the starting time for each sensor.
    pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;
}

void loop() {
  for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through all the sensors.
    if (millis() >= pingTimer[i]) {         // Is it this sensor's time to ping?
      pingTimer[i] += PING_INTERVAL * SONAR_NUM;  // Set next time this sensor will be pinged.
      if (i == 0 && currentSensor == SONAR_NUM - 1) oneSensorCycle(); // Sensor ping cycle complete, do something with the results.
      sonar[currentSensor].timer_stop();          // Make sure previous timer is canceled before starting a new ping (insurance).
      currentSensor = i;                          // Sensor being accessed.
      cm[currentSensor] = 0;                      // Make distance zero in case there's no ping echo for this sensor.
      sonar[currentSensor].ping_timer(echoCheck); // Do the ping (processing continues, interrupt will call echoCheck to look for echo).
    }
  }
  // Other code that *DOESN'T* analyze ping results can go here.
}

void echoCheck() { // If ping received, set the sensor distance to array.
  if (sonar[currentSensor].check_timer())
    cm[currentSensor] = sonar[currentSensor].ping_result / US_ROUNDTRIP_CM;
}

void oneSensorCycle() { // Sensor ping cycle complete, do something with the results.
  // The following code would be replaced with your code that does something with the ping results.
  for (uint8_t i = 0; i < SONAR_NUM; i++) {
    Serial.print(i);
    Serial.print("=");
    Serial.print(cm[i]);
    Serial.print("cm ");
  }
  Serial.println();
}

with the following error messages when I try to verify

new_stuff_ard:22: error: 'NewPing' does not name a type
new_stuff_ard.ino: In function 'void loop()':
new_stuff_ard:39: error: 'sonar' was not declared in this scope
new_stuff_ard.ino: In function 'void echoCheck()':
new_stuff_ard:49: error: 'sonar' was not declared in this scope
new_stuff_ard:50: error: 'US_ROUNDTRIP_CM' was not declared in this scope

At the moment I am attempting to understand how the first error works. A lot of google stuff is going over my head as this is my first time using the arduino. If anyone has a link to a laman worded tutorial to this kind of stuff I would love it.

My current code from the first approach to making this work is

#include <MIDI.h>

const int trigPin = 2;
const int echoPin = 4;
const int trigPin2 =36;
const int echoPin2 =37;
 
void setup() {
  MIDI.begin();
  Serial.begin(115200);
}
 
void loop()
{
  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  long duration, cm;
  long duration2, cm2;
 
  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
 pinMode(trigPin, OUTPUT);
 pinMode(trigPin2, OUTPUT);
 digitalWrite(trigPin, LOW);
 digitalWrite(trigPin2, LOW);
  delayMicroseconds(2);
 digitalWrite(trigPin, HIGH);
 digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10);
 digitalWrite(trigPin, LOW);
 digitalWrite(trigPin2, LOW);
 
  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(echoPin, INPUT);
  pinMode(echoPin2, INPUT);
  duration = pulseIn(echoPin, HIGH);
  duration2 = pulseIn(echoPin2, HIGH);
 
  // convert the time into a distance
  cm = microsecondsToCentimeters(duration);
  cm2 = microsecondsToCentimeters2(duration2);
  
  //Serial.print(cm);
  //Serial.print("cm");
  //Serial.println();
  
  cm = map(cm, 0, 100, 0, 127);
  cm2 = map(cm, 0, 100, 0, 127);
  
  MIDI.sendControlChange(7, cm, 1);
  MIDI.sendControlChange(7, cm2, 2);
  
 
}
  
long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

long microsecondsToCentimeters2(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

It has the same problem as its serial equivalent in that one will come through, but not the other.

Thanks-and sorry again if any of my replies may have been convoluted or not having supplied enough information. If there is anything you need to know missing from this post, please feel free to ask.

Minrat:
(continued from latter half of above post)

You could try to test if the both sensor part is working ok, ignoring the MIDI part, just go back to the sketch with the serial 9600 and the serial.print lines, but change them to read cm2 too

I have just tried this recently. Here is my current code for it:

const int trigPin = 36;

const int trigPin2 = 2;
const int echoPin = 37;
const int echoPin2 = 4;

void setup() {
 // initialize serial communication:
 Serial.begin(9600);
}

void loop()
{
 // establish variables for duration of the ping,
 // and the distance result in inches and centimeters:
 long duration, cm;
 long duration2, cm2;

// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
 // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
 pinMode(trigPin, OUTPUT);
 pinMode(trigPin2, OUTPUT);
 digitalWrite(trigPin, LOW);
 digitalWrite(trigPin2, LOW);
 delayMicroseconds(2);
 digitalWrite(trigPin, HIGH);
 digitalWrite(trigPin2, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPin, LOW);
 digitalWrite(trigPin2, LOW);

// Read the signal from the sensor: a HIGH pulse whose
 // duration is the time (in microseconds) from the sending
 // of the ping to the reception of its echo off of an object.
 pinMode(echoPin, INPUT);
 pinMode(echoPin2, INPUT);
 duration = pulseIn(echoPin, HIGH);
 duration2 = pulseIn(echoPin2, HIGH);

// convert the time into a distance
 cm = microsecondsToCentimeters(duration);
 cm2 = microsecondsToCentimeters2(duration2);
 
 Serial.print(cm);
 Serial.print(“cm”);
 Serial.print(cm2);
 Serial.print(“cm”);
 Serial.println();
 
 delay(100);
}

long microsecondsToCentimeters(long microseconds)
{
 // The speed of sound is 340 m/s or 29 microseconds per centimeter.
 // The ping travels out and back, so to find the distance of the
 // object we take half of the distance travelled.
 return microseconds / 29 / 2;
}
long microsecondsToCentimeters2(long microseconds)
{
 return microseconds / 29 / 2;
}




What happens is that the first reading is always valid and correct, but the second is always 0. If I swap the sensor allocation on the arduino code to



const int trigPin = 2;
const int trigPin2 = 36;
const int echoPin = 4;
const int echoPin2 = 37;




The results is the same. Meaning that the first reading now coming from a different sensor is now accurate while the other is always 0.



> http://code.google.com/p/arduino-new-ping/wiki/15_Sensors_Example



I have tried using this but am unfamiliar with much of the variables and how to code them. This is largely due to my ineptitude, and I'm trying to learn but it is slow.

What I have got so far is



// ---------------------------------------------------------------------------

#include <NewPing.h>

#define SONAR_NUM     2 // Number of sensors.
#define MAX_DISTANCE 100 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 33 // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo).

unsigned long pingTimer[SONAR_NUM]; // Holds the times when the next ping should happen for each sensor.
unsigned int cm[SONAR_NUM];         // Where the ping distances are stored.
uint8_t currentSensor = 0;          // Keeps track of which sensor is active.

NewPing sonar[SONAR_NUM] = {     // Sensor object array.
 NewPing(2, 4, 100), // Each sensor’s trigger pin, echo pin, and max distance to ping.
 NewPing(36, 37, 100)
};

void setup() {
 Serial.begin(115200);
 pingTimer[0] = millis() + 75;           // First ping starts at 75ms, gives time for the Arduino to chill before starting.
 for (uint8_t i = 1; i < SONAR_NUM; i++) // Set the starting time for each sensor.
   pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;
}

void loop() {
 for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through all the sensors.
   if (millis() >= pingTimer[i]) {         // Is it this sensor’s time to ping?
     pingTimer[i] += PING_INTERVAL * SONAR_NUM;  // Set next time this sensor will be pinged.
     if (i == 0 && currentSensor == SONAR_NUM - 1) oneSensorCycle(); // Sensor ping cycle complete, do something with the results.
     sonar[currentSensor].timer_stop();          // Make sure previous timer is canceled before starting a new ping (insurance).
     currentSensor = i;                          // Sensor being accessed.
     cm[currentSensor] = 0;                      // Make distance zero in case there’s no ping echo for this sensor.
     sonar[currentSensor].ping_timer(echoCheck); // Do the ping (processing continues, interrupt will call echoCheck to look for echo).
   }
 }
 // Other code that DOESN’T analyze ping results can go here.
}

void echoCheck() { // If ping received, set the sensor distance to array.
 if (sonar[currentSensor].check_timer())
   cm[currentSensor] = sonar[currentSensor].ping_result / US_ROUNDTRIP_CM;
}

void oneSensorCycle() { // Sensor ping cycle complete, do something with the results.
 // The following code would be replaced with your code that does something with the ping results.
 for (uint8_t i = 0; i < SONAR_NUM; i++) {
   Serial.print(i);
   Serial.print("=");
   Serial.print(cm[i]);
   Serial.print("cm ");
 }
 Serial.println();
}




with the following error messages when I try to verify



new_stuff_ard:22: error: ‘NewPing’ does not name a type
new_stuff_ard.ino: In function ‘void loop()’:
new_stuff_ard:39: error: ‘sonar’ was not declared in this scope
new_stuff_ard.ino: In function ‘void echoCheck()’:
new_stuff_ard:49: error: ‘sonar’ was not declared in this scope
new_stuff_ard:50: error: ‘US_ROUNDTRIP_CM’ was not declared in this scope



At the moment I am attempting to understand how the first error works. A lot of google stuff is going over my head as this is my first time using the arduino. If anyone has a link to a laman worded tutorial to this kind of stuff I would love it.
----------
My current code from the first approach to making this work is 



#include <MIDI.h>

const int trigPin = 2;
const int echoPin = 4;
const int trigPin2 =36;
const int echoPin2 =37;

void setup() {
 MIDI.begin();
 Serial.begin(115200);
}

void loop()
{
 // establish variables for duration of the ping,
 // and the distance result in inches and centimeters:
 long duration, cm;
 long duration2, cm2;

// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
 // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(trigPin, OUTPUT);
pinMode(trigPin2, OUTPUT);
digitalWrite(trigPin, LOW);
digitalWrite(trigPin2, LOW);
 delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
digitalWrite(trigPin2, HIGH);
 delayMicroseconds(10);
digitalWrite(trigPin, LOW);
digitalWrite(trigPin2, LOW);

// Read the signal from the sensor: a HIGH pulse whose
 // duration is the time (in microseconds) from the sending
 // of the ping to the reception of its echo off of an object.
 pinMode(echoPin, INPUT);
 pinMode(echoPin2, INPUT);
 duration = pulseIn(echoPin, HIGH);
 duration2 = pulseIn(echoPin2, HIGH);

// convert the time into a distance
 cm = microsecondsToCentimeters(duration);
 cm2 = microsecondsToCentimeters2(duration2);
 
 //Serial.print(cm);
 //Serial.print(“cm”);
 //Serial.println();
 
 cm = map(cm, 0, 100, 0, 127);
 cm2 = map(cm, 0, 100, 0, 127);
 
 MIDI.sendControlChange(7, cm, 1);
 MIDI.sendControlChange(7, cm2, 2);

}
 
long microsecondsToCentimeters(long microseconds)
{
 // The speed of sound is 340 m/s or 29 microseconds per centimeter.
 // The ping travels out and back, so to find the distance of the
 // object we take half of the distance travelled.
 return microseconds / 29 / 2;
}

long microsecondsToCentimeters2(long microseconds)
{
 // The speed of sound is 340 m/s or 29 microseconds per centimeter.
 // The ping travels out and back, so to find the distance of the
 // object we take half of the distance travelled.
 return microseconds / 29 / 2;
}




It has the same problem as its serial equivalent in that one will come through, but not the other.


Thanks-and sorry again if any of my replies may have been convoluted or not having supplied enough information. If there is anything you need to know missing from this post, please feel free to ask.

I don’t know how the sensor actually works, i wasn’t sure if what I told you would work, just trying.
About the NewPing.h error, you need the library. You’re including it but you did not download it and put it in the libraries folder
http://playground.arduino.cc/Code/NewPing

  duration = pulseIn(echoPin, HIGH);
  duration2 = pulseIn(echoPin2, HIGH);

This will not work. You launch two pulses and then measure the duration of the first, by which time the second has finished and so always returns zero. Therefore you can not use pulseIn to measure more than one sensor. You need to use the technique in the blink without delay example to measure multiple pulses. This project might be just too hard for you at the moment. I recomend you learn more about programming first.

capicoso:
I don’t know how the sensor actually works, i wasn’t sure if what I told you would work, just trying.
About the NewPing.h error, you need the library. You’re including it but you did not download it and put it in the libraries folder
http://playground.arduino.cc/Code/NewPing

That worked-It now posts serial values for the 2 sensors I have hooked up. The only challenge left to get these 2 finished is to convert to MIDI.

Once I’ve done that I will build the frame for it to be used as an interactive music medium followed by trying it with 8 sensors.

For midi conversion I have included the MIDI library and copied and pasted what I believe was the MIDI output code from the first way we were trying it. Can you check that it is in the correct position? (I put it where the serial.print things were which makes sense to me)

Do I need the mapping code? I believe I have already specified the maximum for the sensors earlier but would like to know if the program has been designed in such a way as to mean I would not need to respecify.

Also, I don’t know what to replace cm and cm2 with in terms of the MIDI output code. Looking over what’s there but I can’t see any clearly defined = stuff. That said, the output appears as

0=18cm 1=27cm
0=18cm 1=27cm
0=17cm 1=27cm
0=17cm 1=27cm
0=18cm 1=27cm
0=17cm 1=27cm
0=18cm 1=27cm
0=18cm 1=27cm
0=18cm 1=27cm
0=18cm 1=27cm

So I imagine that i would use

MIDI.sendControlChange(7, 0, 1);
  MIDI.sendControlChange(7, 1, 2);

(just tested that and it worked)

With the code like this

#include <MIDI.h>

#include <NewPing.h>


// ---------------------------------------------------------------------------
// This example code was used to successfully communicate with 15 ultrasonic sensors. You can adjust
// the number of sensors in your project by changing SONAR_NUM and the number of NewPing objects in the
// "sonar" array. You also need to change the pins for each sensor for the NewPing objects. Each sensor
// is pinged at 33ms intervals. So, one cycle of all sensors takes 495ms (33 * 15 = 495ms). The results
// are sent to the "oneSensorCycle" function which currently just displays the distance data. Your project
// would normally process the sensor results in this function (for example, decide if a robot needs to
// turn and call the turn function). Keep in mind this example is event-driven. Your complete sketch needs
// to be written so there's no "delay" commands and the loop() cycles at faster than a 33ms rate. If other
// processes take longer than 33ms, you'll need to increase PING_INTERVAL so it doesn't get behind.
// ---------------------------------------------------------------------------


#define SONAR_NUM     2 // Number of sensors.
#define MAX_DISTANCE 100 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 33 // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo).

unsigned long pingTimer[SONAR_NUM]; // Holds the times when the next ping should happen for each sensor.
unsigned int cm[SONAR_NUM];         // Where the ping distances are stored.
uint8_t currentSensor = 0;          // Keeps track of which sensor is active.

NewPing sonar[SONAR_NUM] = {     // Sensor object array.
  NewPing(2, 4, 100), // Each sensor's trigger pin, echo pin, and max distance to ping.
  NewPing(36, 37, 100)
};

void setup() {
  Serial.begin(115200);
  pingTimer[0] = millis() + 75;           // First ping starts at 75ms, gives time for the Arduino to chill before starting.
  for (uint8_t i = 1; i < SONAR_NUM; i++) // Set the starting time for each sensor.
    pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;
}

void loop() {
  for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through all the sensors.
    if (millis() >= pingTimer[i]) {         // Is it this sensor's time to ping?
      pingTimer[i] += PING_INTERVAL * SONAR_NUM;  // Set next time this sensor will be pinged.
      if (i == 0 && currentSensor == SONAR_NUM - 1) oneSensorCycle(); // Sensor ping cycle complete, do something with the results.
      sonar[currentSensor].timer_stop();          // Make sure previous timer is canceled before starting a new ping (insurance).
      currentSensor = i;                          // Sensor being accessed.
      cm[currentSensor] = 0;                      // Make distance zero in case there's no ping echo for this sensor.
      sonar[currentSensor].ping_timer(echoCheck); // Do the ping (processing continues, interrupt will call echoCheck to look for echo).
    }
  }
  // Other code that *DOESN'T* analyze ping results can go here.
}

void echoCheck() { // If ping received, set the sensor distance to array.
  if (sonar[currentSensor].check_timer())
    cm[currentSensor] = sonar[currentSensor].ping_result / US_ROUNDTRIP_CM;
}

void oneSensorCycle() { // Sensor ping cycle complete, do something with the results.
  // The following code would be replaced with your code that does something with the ping results.
  for (uint8_t i = 0; i < SONAR_NUM; i++) 
    //Serial.print(i);
    //Serial.print("=");
    //Serial.print(cm[i]);
    //Serial.print("cm ");
  //}
  //Serial.println();
  
  0 = map(cm, 0, 100, 0, 127);
  1 = map(cm, 0, 100, 0, 127);
  
  MIDI.sendControlChange(7, 0, 1);
  MIDI.sendControlChange(7, 1, 2);
  }

The only problem with this is that in hairless I’m only sending a 0 and a 1, presumably as I didn’t include a scale so I need to find out how to word

  0 = map(cm, 0, 100, 0, 127);
  1 = map(cm, 0, 100, 0, 127);

to be understood by what is there already.

When I try to run what i have, i get the following error message:

new_stuff_ard2.ino: In function ‘void oneSensorCycle()’:
new_stuff_ard2:68: error: invalid conversion from ‘unsigned int*’ to ‘long int’
new_stuff_ard2:68: error: initializing argument 1 of ‘long int map(long int, long int, long int, long int, long int)’
new_stuff_ard2:68: error: lvalue required as left operand of assignment
new_stuff_ard2:69: error: invalid conversion from ‘unsigned int*’ to ‘long int’
new_stuff_ard2:69: error: initializing argument 1 of ‘long int map(long int, long int, long int, long int, long int)’
new_stuff_ard2:69: error: lvalue required as left operand of assignment

Can anyone find what I should be calling cm?

Grumpy_Mike:

  duration = pulseIn(echoPin, HIGH);

duration2 = pulseIn(echoPin2, HIGH);



This will not work. You launch two pulses and then measure the duration of the first, by which time the second has finished and so always returns zero.
Therefore you can not use pulseIn to measure more than one sensor.
You need to use the technique in the blink without delay example to measure multiple pulses.
This project might be just too hard for you at the moment. I recommend you learn more about programming first.

Thankyou for the observation. While programming is certainly something I wish I had the time to learn thoroughly so that I could do projects like this without requiring assistance, I am currently otherwise occupied attempting to learn how harmonic systems have evolved in tonal music from the medieval period. The purpose of this project is to complete an assignment to do with ‘conceptual extremes’. My concept is tone colour, I am attempting to make it extreme. Others have done other things that require little to no new learning or set-up, but few of their ideas interest me.

I fully intend to get this up and running so that I may submit it. That said when I do have to time and am looking for ways of building other interactive pseudo instruments I will remember your advice and make sure to at least have a beginners understanding of the coding I need and exactly how it function.

Again, thanks to all for helping and being patient with me.