How to communicate a button in Arduino with a bang in Max

Marlou Kellenaers's icon

I have my code in Max working. However, I want to create a button on an Arduino board and turn my code on/off in Max when pressing on this button in real life.
My code in Max starts with a bang. How can the Arduino button communicate with the bang in Max? I couldn't find any tutorials about this.

Source Audio's icon

in max you use serial object
in arduino, read one digital input,
let it print whatever ID you want when button is pressed to serial.
You should use bounce lib or something simple that prevents sending
repeated button states.
in Max read from serial object, and when ID for the button gets received
bang that bang.

Marlou Kellenaers's icon

The serial monitor in Arduino is giving numbers 0 and 1, 0 when the button is pressed.
How can I transform this information to Max MSP and turn on the bang when Arduino is giving a 0?

Source Audio's icon

int BUTTON; // current state of the button
int exBUTTON; // previous state of the button

void setup() {
pinMode(2, INPUT_PULLUP);
Serial.begin(9600);
}

void loop() {BUTTON = digitalRead(2);
if (BUTTON == HIGH) {exBUTTON = 1;}
if (BUTTON == LOW and exBUTTON == 1) {Serial.println("A"); exBUTTON = 0;}
delay(50);
}

in max

Max Patch
Copy patch and select New From Clipboard in Max.


You need to short D2 to ground with switch to output "A" to serial port.
you asked for a bang in max, not switch press and release states.
------
If you want to use that example from the link you posted, output in max will be 1 and 0, but will keep repeating in 100 ms interval.
You would need to insert change object after fromsymbol, and instead of
sel A, sel 1 0


Marlou Kellenaers's icon

Thank you so much, that is helping a lot!!

Marlou Kellenaers's icon

I have one last question. How to change the Arduino code if I have 4 buttons on digital pins 13, 14, 15 and 16.
The serial.println is then A, B, C or D according to which digital pin (button) is pressed.
I already know how to change the code in Max and gain different outputs according to the letters A, B, C and D.

Thank you in advance.

Source Audio's icon

You can simply multiply all for 4 buttons.
I shortened button to but . am lazy to type that much...
define variables : ( all can be in same line ...)

int BUT1; int exBUT1; int BUT2; int exBUT2; int BUT3; int exBUT3; int BUT4; int exBUT4;

void setup() { pinMode(13, INPUT_PULLUP); pinMode(14, INPUT_PULLUP);
pinMode(15, INPUT_PULLUP); pinMode(16, INPUT_PULLUP); Serial.begin(9600); }

void loop() {
BUT1 = digitalRead(13); if (BUT1 == HIGH) {exBUT1 = 1;}
if (BUT1 == LOW and exBUT1 == 1) {Serial.println("A"); exBUT1 = 0;}

BUT2 = digitalRead(14); if (BUT2 == HIGH) {exBUT2 = 1;}
if (BUT2 == LOW and exBUT2 == 1) {Serial.println("B"); exBUT2 = 0;}

// add lines for buttons 3 and 4 , delay and last bracket at the end to enclose the loop

delay(50);
}

-----------
That is mucho typing variant but is better for beginner to see the flow.
Otherwise one could have used arrays, and make all this in few lines of code .....

Marlou Kellenaers's icon

Thank you so much, that is helping a lot!

Source Audio's icon

sorry, I forgot to use digital inputs that you asked for,
replace 2 3 4 5 with 13 14 15 16 in both setup and loop
i fixed my previous post

Marlou Kellenaers's icon

Thank you, I already fixed that myself. Thanks for the help