Help Combining Arduino Codes for Ping and LED

Hello,

I'm an Art student at UCSC and need help programming the Arduino Decimilia to control a Ping))) and LED. I am trying to merge the Messenger Arduino Code and Ping Code. I'm controlling each one individually with Max/MSP. I found both codes on the Arduino site.
arduino.cc/playground/Code/Messenger
arduino.cc/en/Tutorial/Ping

I used the MAX/MSP patches that came along with each. I want both the LED and Ping to work independently and not together. I'm using the Ping to trigger a video in which when it hits a certain point in the video the LED will turn on. Very simple concept.

Thank you in advance for all and any help.
Any help will be greatly appreciated.

-Leo

You need to post your code before we can help.

Posting the same problem multiple times doesn't help.

Messenger Code:
#include <Messenger.h>
// Instantiate Messenger object with the default separator (the space character)
Messenger message = Messenger();

// Create the callback function
void messageReady() {
int pin = 0;
// Loop through all the available elements of the message
while ( message.available() ) {
// Set the pin as determined by the message
digitalWrite( pin, message.readInt() );
pin=pin+1;
}
}

void setup() {
// Initiate Serial Communication
Serial.begin(115200);
// Attach the callback function to the Messenger
message.attach(messageReady);
}

void loop() {
// The following line is the most effective way of using Serial and Messenger's callback
while ( Serial.available() ) message.process(Serial.read () );
}

Ping COde:
/* Ping))) Sensor

This sketch reads a PING))) ultrasonic rangefinder and returns the
distance to the closest object in range. To do this, it sends a pulse
to the sensor to initiate a reading, then listens for a pulse
to return. The length of the returning pulse is proportional to
the distance of the object from the sensor.

The circuit:

  • +V connection of the PING))) attached to +5V
  • GND connection of the PING))) attached to ground
  • SIG connection of the PING))) attached to digital pin 7

http://www.arduino.cc/en/Tutorial/Ping

created 3 Nov 2008
by David A. Mellis
modified 30 Jun 2009
by Tom Igoe

This example code is in the public domain.

*/

// this constant won't change. It's the pin number
// of the sensor's output:
const int pingPin = 7;

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 PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);

// The same pin is used to read the signal from the PING))): 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(pingPin, INPUT);
duration = pulseIn(pingPin, 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;
}

How do I combine these two to work and serve their regular functions?

Both work great on seperate arduinos but I want to make it work with one arduino.

Put the code from the first setup function into the second setup. Put the code from the first loop into the second loop function.

When you receive a message, see if it's a request to ping. If it is, execute the pinging code, and send back a reply.

Thanks PaulS!
IT seems to work on just the arduino but now I seem to have a problem in MAX/MSP.

My current MAX code seems to give me inaccurate readings. I checked the readings in the Arduino Serial Monitor seems accurate to me.
Here is the new arduino code:

int pingPin = 7;
#include <Messenger.h>

unsigned long previousMillis = 0;

unsigned long interval = 20;

Messenger message = Messenger();

void messageCompleted() {
int pin = 0;

while ( message.available() ) {
int pin = message.readInt();
int state = message.readInt();

digitalWrite( pin, state);
}
}

void setup()
{
Serial.begin(9600);
message.attach(messageCompleted);
}

void loop()
{

while ( Serial.available( ) ) message.process(Serial.read( ) );

if ( millis() - previousMillis > interval ) {
previousMillis = millis();
for ( byte i = 0; i < 6; i++) {
Serial.print( analogRead(i) );
Serial.print(' ');
}
Serial.println();
}

long duration, inches, cm;

pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);

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

inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

Serial.print(inches);

Serial.println();

delay(100);
}

long microsecondsToInches(long microseconds)
{

return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{

return microseconds / 29 / 2;
}

And here is my MAX/Msp patch:

{
"patcher" : {
"fileversion" : 1,
"rect" : [ 1.0, 45.0, 966.0, 608.0 ],
"bglocked" : 0,
"defrect" : [ 1.0, 45.0, 966.0, 608.0 ],
"openrect" : [ 0.0, 0.0, 0.0, 0.0 ],
"openinpresentation" : 0,
"default_fontsize" : 12.0,
"default_fontface" : 0,
"default_fontname" : "Arial",
"gridonopen" : 0,
"gridsize" : [ 15.0, 15.0 ],
"gridsnaponopen" : 0,
"toolbarvisible" : 1,
"boxanimatetime" : 200,
"imprint" : 0,
"enablehscroll" : 1,
"enablevscroll" : 1,
"devicewidth" : 0.0,
"boxes" : [ {
"box" : {
"maxclass" : "message",
"text" : "1 1",
"outlettype" : [ "" ],
"id" : "obj-71",
"fontname" : "Arial",
"fontsize" : 12.0,
"numinlets" : 2,
"numoutlets" : 1,
"patching_rect" : [ 4.0, 312.0, 222.0, 18.0 ]
}

}
, {
"box" : {
"maxclass" : "message",
"text" : "baud 9600",
"outlettype" : [ "" ],
"id" : "obj-16",
"fontname" : "Arial",
"fontsize" : 12.0,
"numinlets" : 2,
"numoutlets" : 1,
"patching_rect" : [ 571.0, 201.0, 74.0, 18.0 ]
}

}
, {
"box" : {
"maxclass" : "newobj",
"text" : "if $i1 != 139 then $i1",
"outlettype" : [ "" ],
"id" : "obj-8",
"fontname" : "Arial",
"fontsize" : 12.0,
"numinlets" : 1,
"numoutlets" : 1,
"patching_rect" : [ 325.0, 251.0, 119.0, 20.0 ]
}

}
, {
"box" : {
"maxclass" : "number",
"outlettype" : [ "int", "bang" ],
"id" : "obj-7",
"fontname" : "Arial",
"fontsize" : 12.0,
"numinlets" : 1,
"numoutlets" : 2,
"patching_rect" : [ 325.0, 280.0, 73.0, 20.0 ]
}

}
, {
"box" : {
"maxclass" : "toggle",
"outlettype" : [ "int" ],
"id" : "obj-75",
"numinlets" : 1,
"numoutlets" : 1,
"patching_rect" : [ 273.0, 84.0, 20.0, 20.0 ]
}

}
, {
"box" : {
"maxclass" : "message",
"text" : "dtr $1",
"outlettype" : [ "" ],
"id" : "obj-78",
"fontname" : "Arial",
"fontsize" : 12.0,
"numinlets" : 2,
"numoutlets" : 1,
"patching_rect" : [ 273.0, 112.0, 50.0, 18.0 ]
}

}
, {
"box" : {
"maxclass" : "newobj",
"text" : "p createMenu",
"outlettype" : [ "" ],
"id" : "obj-106",
"fontname" : "Verdana",
"fontsize" : 9.0,
"numinlets" : 1,
"numoutlets" : 1,
"patching_rect" : [ 381.0, 180.0, 72.0, 17.0 ],
"patcher" : {
"fileversion" : 1,
"rect" : [ 519.0, 114.0, 213.0, 363.0 ],
"bglocked" : 0,
"defrect" : [ 519.0, 114.0, 213.0, 363.0 ],
"openrect" : [ 0.0, 0.0, 0.0, 0.0 ],
"openinpresentation" : 0,
"default_fontsize" : 12.0,
"default_fontface" : 0,
"default_fontname" : "Verdana",
"gridonopen" : 0,
"gridsize" : [ 15.0, 15.0 ],
"gridsnaponopen" : 0,
"toolbarvisible" : 1,
"boxanimatetime" : 200,
"imprint" : 0,
"enablehscroll" : 1,
"enablevscroll" : 1,
"devicewidth" : 0.0,
"boxes" : [ {
"box" : {
"maxclass" : "button",
"outlettype" : [ "bang" ],
"id" : "obj-1",
"numinlets" : 1,
"numoutlets" : 1,
"patching_rect" : [ 114.0, 142.0, 15.0, 15.0 ]
}

}
, {
"box" : {
"maxclass" : "message",
"text" : "clear",
"outlettype" : [ "" ],
"id" : "obj-2",
"fontname" : "Verdana",
"fontsize" : 9.0,
"numinlets" : 2,
"numoutlets" : 1,
"patching_rect" : [ 114.0, 168.0, 33.0, 15.0 ]
}

}
, {
"box" : {
"maxclass" : "newobj",
"text" : "prepend append",
"linecount" : 2,
"outlettype" : [ "" ],
"id" : "obj-3",
"fontname" : "Verdana",
"fontsize" : 9.0,
"numinlets" : 1,
"numoutlets" : 1,
"patching_rect" : [ 24.0, 167.0, 78.0, 28.0 ]
}

}
, {
"box" : {
"maxclass" : "newobj",
"text" : "zl iter 1",
"outlettype" : [ "", "" ],
"id" : "obj-4",
"fontname" : "Verdana",
"fontsize" : 9.0,
"numinlets" : 2,
"numoutlets" : 2,
"patching_rect" : [ 24.0, 116.0, 47.0, 17.0 ]
}

}
, {
"box" : {
"maxclass" : "newobj",
"text" : "route port",
"outlettype" : [ "", "" ],
"id" : "obj-5",
"fontname" : "Verdana",

It looks like your max patch code got cut off. Create a simple max patch to just talk to the Arduino. Post that.

The pinging happens every time through loop, not just when serial data has been received.

The pinging code should be moved into the callback function (messageCompleted). Then, you can get rid of the calls to millis() and the interval and previousMillis variables.