Forums > MaxMSP

ArduinoMax-InOut-forDummies patch…loadbang settings…

September 22, 2013 | 10:59 pm

Greetings Max gurus,

This is for an interactive installation. I’m using an Arduino to have a monetary button send a bang to Max. I’ll have the patch autorun on startup of the computer.

I’m trying to [loadbang] 1)Pin 2 2)Digital-Analog In 3)Toggle poll on…

When I do this. It doesn’t work unless I manually select from the [unmenu] what I want using the mouse. Any ideas or solutions to this?
This is something probably very simple that I’m just not getting.

Thank you for your time.

Patch belo

<code>

– Pasted Max Patch, click to expand. –

</code>


September 23, 2013 | 12:52 am

Hi
if the items from the umenu are never changed, why not ditch them and just loadbang the message (2, 0) in the [pak] object. Remember to use the [trigger] object after loadbang, to chronologically order your messages!

Brendan


September 23, 2013 | 6:58 am

Hi Brendan,
Thanks for the suggestion. I made those changes but still experiencing the same problem.

I have an arduino connected and using a simple monetary push button.

When I launch the patch and press the push button nothing happens. It’s only when I click the loadbang or the 0 and 2 message objects with the mouse, then pressing the push button I see the Pin2_Digiln toggle button turn on.

<code>

– Pasted Max Patch, click to expand. –

</code>

Here is the Arduino code:

/*
Beta, Beta, Beta, Beta, Beta, Beta, Beta, Beta, Beta, Beta, Beta, Beta, Beta, Beta

Shared APRIL 2012 by Lasse Vestergaard

The protocol for communicating with this sketch is:

SERIAL INPUT:
– set pin state (p) og set pin value (v)
– When setting a pin state you can write 0 (input),1 (output),2 (pwm)
– set pin number
– set value
– Example: [p 3 2] & [v 3 128] == Pin 3 is set to be pwm and addressed with 50/50 duty cycle (medium "speed")
(just send as char values from any program that can communicate with serial ports ex. Max/MSP)
– You´ll need "[" and "]" to start and end requests.

SERIAL OUTPUT:
– pin number
– value
– Example: 7 0 == Pin 7 is set to receive a digital input (a carriage return is send to end request.)
– The board will only output through serial if an Arduino port is set to input.

*/

unsigned long sendtime;

int portregister[]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
String sample="";
String explodedSample[3];

void setup(){
sendtime=millis();
Serial.begin(115200);
}

void loop(){
while(Serial.available()>0){
char c=(char)Serial.read();
if(c == ‘[')
sample="";
else if(c == ']‘){
explodeString(sample);
if(explodedSample[0].equalsIgnoreCase("v")){
setPinValue(convertStringToInt(explodedSample[1]),convertStringToInt(explodedSample[2]));
}
else if(explodedSample[0].equalsIgnoreCase("p")){
setPinState(convertStringToInt(explodedSample[1]),convertStringToInt(explodedSample[2]));
}
}
else
sample+=c;
}

if(millis()>sendtime+5){
readSendInput();
sendtime=millis();
}

}

void readSendInput(){
for(int i=0;i<18;i++){
if(portregister[i]==0){
if(i<12){
Serial.print(i+2);
Serial.print(" ");
Serial.println(digitalRead(i+2));
}
else{
Serial.print(i+2);
Serial.print(" ");
Serial.println(analogRead(i+2));
}
}
}
}

void setPinState(int pin, int pinState){
portregister[pin-2]=pinState;
if(pinState==2)
pinState=1;
pinMode(pin, pinState);
}

void setPinValue(int pin, int val){
int pinState=portregister[pin-2];
if(pinState==1){
digitalWrite(pin,val);
}
else if(pinState==2){
analogWrite(pin,val);
}
}

/***********************************/
/* HELPER FUNCTIONS */
/***********************************/

void explodeString(String s){
String tem="";
int tempointer=0;

for(int i=0;i<s.length();i++){
if(s[i] == ' '){
explodedSample[tempointer++]=tem;
tem="";
}
else
tem+=s[i];
}
explodedSample[2]=tem;
}

int convertStringToInt(String s){
char test[s.length()+1];
s.toCharArray(test, sizeof(test));
return atoi(test);
}

You can also download it yourself here:

http://playground.arduino.cc/uploads/Interfacing/ArduinoMax-InOut-forDummies01.zip


September 23, 2013 | 2:35 pm

Hi
I don’t have (or want :p) the ArduinoInOut stuff, so I’m just guessing here but; if the Arduino is expecting a call-and-response (while and if conditions above), then it appears that your button press is what kicks off the process. A lazy guess would be to try and send the char "[", as stated at the head of the code above:

"
– Example: [p 3 2] & [v 3 128] == Pin 3 is set to be pwm and addressed with 50/50 duty cycle (medium "speed")
(just send as char values from any program that can communicate with serial ports ex. Max/MSP)
You´ll need "[" and "]" to start and end requests.
"
Sorry not to be more help, but I’m crap at troubleshooting these prefabs, I prefer to roll my own code and patches for Arduino stuff.

Brendan


Viewing 4 posts - 1 through 4 (of 4 total)