Max MSP to Arduino IDE

twill91's icon

Hi,

Over the last year I've been learning Max and made some good progress, but I struggle a little when making the transition between Max and other coding forms.

I'm currently working on a project that requires I build the software within the Arduino IDE.

I'm using an infrared proximity sensor and need the Arduino to trigger sound file playback at different distance thresholds.

I was wondering if anyone new an equivalent process of the "Change" Object in Max, that can be used in the Arduino IDE to filter repeating values when the sensor read is within a certain threshold.

I hope this makes sense, any help or advice would be greatly appreciated.

Thanks in advance!

nickel_alloy's icon

A quick and dirty way to achieve this could be to use the map() function. It is basically the same as the scale object in max.Reference here

I'm going to assume you're reading values from an analog pin and using the serial port to send values out to MAX.
You could tweak the following to suit your needs:

int newVal;
int oldVal;

void setup()
{
   Serial.begin(9600);
}

void loop()
{
   newVal = map(analogRead(A0), 0, 1023, 1, 5);
   if (newVal != oldVal)
   {
     Serial.println(newVal);
     oldVal = newVal;
   }
}

This is completely untested and just a rough example (it should work though!...). Change the 5 to however many different steps you want.
You can reverse the values too:map(analogRead(A0), 0, 1023, 5, 1)

You might need to tweak the input values (0, 1023) to get the best results for your particular setup.

Max will only receive a new value when the output changes thanks to the if() function. The threshold is provided by the map() function due to the scaling.

Hope that helps!

twill91's icon

Hey,

This works a charm, thanks. I had the map part figured but I was a little unsure of the if (newVal != oldVal) part. I've got it now and it's working well.

Thank you very much for the help!