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",