Issues Using Multiple HC-SR04 Sensors and other components within Max MSP, HELP!

I am currently making a patch within Max For Live. Ive got various components such as a 2-axis joystick, potentiometer and LDR's. These are not the issue as I can get these working completely fine. However, I am having issues with getting a HC-SR04 distance sensor to work.

Here is a code that I have put into Arduino to get the HC-SR04 to work as an individual:

const int trigPin = 2;
const int echoPin = 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, 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;
}

Now this code works by itself, I am just wondering if I can combine this with the Standard Firmata to get everything working together. I have attempted and successfully uploaded the sketch with them combined, however, it is only the HC-SR04 that seems to work.

Is there anyone that can shed some light on the situation?

Go easy on me i'm fairly new to arduino use.

You should show us the combined sketch which is not working and not the working one if you want help from us. And use code tags (the #-Button).

Ok, I have attached a PDF with the sketch in as it has too many characters to post within the text.

I'm using a MEGA and once it is uploaded, I still get readings within the serial monitor from the HC-SR04, however, the standard firmata does not seem to work.

Any suggestions?

Arduino Combined Sketch.pdf (81.1 KB)

A PDF, especially one exported from MS Word, is a very bad format to post code. Why not simply attaching the .ino? The indents are almost invisible and copying the text to a editor does produce a not compilable code that would have to be edited a lot.

Anyway, I think you problem is, that you renamed setup() and loop() of Firmata to setupStandard() and loopStandard() but you're not calling them in setup() and loop(), so I would also not expect Firmata to run.

Apologies, I will know for next time.

I changed the names of setup() and loop() as if i didn't then an error would appear due to there being a setup() and loop() within the first sketch. So what would I have to do to stop these errors?

Cheers

Does that mean you haven't combined the sketches but just copied some routines from one to the other?

At least you have to call setupStandard() from setup() and loopStandard() from loop() but I haven't checked if this will be enough changes for this to work. It's easier if you tell us what part you inserted into the Firmata sketch.

I changed the names of setup() and loop() as if i didn't then an error would appear due to there being a setup() and loop() within the first sketch.

You need to read this:-
http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html

Well I simply put both sketches into one and then corrected any errors and it uploaded successfully, however the standard firmata was still not working.

Cheers Grumpy_Mike, but funnily enough that is the tutorial I follows for merging the sketches.

Pylon, what I put into the firmata was the first sketch I posted. Are you saying that I need to change both the call setup () name and the setup() name?

Pylon, what I put into the firmata was the first sketch I posted. Are you saying that I need to change both the call setup () name and the setup() name?

No. If you read my last post, you'll have the answer. Are you familiar with what calling a function inside another function means? I didn't write that you should change names, did I?

I get the impression that you have not enough knowledge about programming in C/C++, so you probably should start with easier projects and try to understand what the code does and why.

I haven't done much Arduino sketching no, but I am focusing more on Max MSP as i am using Max for Live and I am stronger with that program. I just need to get both sketches uploaded so I can use everything with Max MSP.

I'm still learning, so I was just asking a question, apologies if I misunderstood your post. This project is for a University assignment and I am pretty much finished in terms of the Max MSP side. A simple explanation of what you meant would be much appreciated.

Thanks

I'm still learning, so I was just asking a question, apologies if I misunderstood your post. This project is for a University assignment and I am pretty much finished in terms of the Max MSP side. A simple explanation of what you meant would be much appreciated

This is what I meant by calling a function:

void setup() {
  // here is some other code
  setupStandard();
  // here might be some other code
}

In this case setupStandard() is called inside setup(), you'll have to do the same for loop(). This is just an example to show you what calling a function inside another function means, you have to apply this to your code yourself.
If you're unsure, insert that in your code an post it again.

O ok, I have input that.

Here is the error message I have received:

StandardFirmata.ino: In function 'void setup()':
StandardFirmata.572: error: 'setupSensor' was not declared in this scope
StandardFirmata.ino: In function 'void loop()':
StandardFirmata.615: error: 'loopSensor' was not declared in this scope

Note that i changed the setup and loop names.

I have attached the .ino of the attempted combined code

Attempted_Combined_Sketch.ino (21.5 KB)

You don't have to call non-existing functions. As you haven't defined a loopSensor() function or a setupSensor() function, just don't call them. It seems that you combined the two setup() and loop() functions into one, which is good.

O right, yes that has worked! Thanks alot Pylon :slight_smile:

Much appreciated for all the help, now gettin readings in MaxMSP

I'm guessing if i want multiple hc-sr04 sensors at once, I will just state with pins to use for the others?

Cheers

I'm guessing if i want multiple hc-sr04 sensors at once, I will just state with pins to use for the others?

Theoretically yes, but you have to be careful with multiple sensor that they don't influence each other. The timing might change considerably but how much that changes the behavior of your project depends a lot on what you do with the other pins and Firmata.

Hey guys :smiley:
I'm currently working on a project with maxuino, firmata and the HC-SR04.(arduino uno btw)
I uploaded the code with the two "dead" functions (lines) deleted.
But I don't get any connection with the firmata_test program.

Do you have any advice?

best
Gertsch