Arduino and MAX MSP

xyluz's icon

I am working on a system using arduino and msp.

What i want to achieve is, when a button is pressed on arduino, max plays a particular video, and when another button is pressed on arduino max plays another video. So its more like if/else thing... presently,

I am able to send data to max, and i am able to play video on max, but i don't know how to:

1. Use the data from arduion
2. to determine the specific video to play on max
3. Pause the video when another button is pressed on arduino

I'll appreciate any help on this. Thank you.

Source Audio's icon

You could prepend a letter to serial print into arduino and use route
object in Max to recognise which button was pressed/released.
Like :
int button1 = digitalRead(2); Serial.print("A "); Serial.println(button1, DEC); // reads pin D2
int button2 = digitalRead(3); Serial.print("B "); Serial.println(button2, DEC); // reads pin D3
and so on
In Max just use {route A B C D ... }
This is just example, it would be much better to use some dedicated
button library, like bounce to just output to serial when button is pressed
or released.
If You have troubles doing so, just ask, I will upload the patch,
but I need all infos, how many buttons, on which pins, which arduino etc
Having both press and release reported opens possibilities to
start, stop or pause/resume Video with just 1 button.
Or like making it loop on long press etc ...

xyluz's icon

Hi, Thank you for the reply, so far, I have done part of what you said... (happy I am on the right track).

I have been able to:

1. Send data from arduino to max, there are 14 buttons all together.
2. take different actions based on the button that was pressed using the [select] command in max
and
3. In arduino I am sending, 1 to 9, and 'a' to 'e' based on the button pressed.

What is left to do, play different video based on the pressed button, pause a video based on a button and resume play based on another button.

Although, I am not sure how to achieve the pause and resume, but I really like the max documentation, its explicit and full of examples, so I guess it shouldn't be so hard.

But, please, upload your patch, i'll really like to learn from it. Not sure how to upload mine, but I will as soon as I tidy up a bit.

Thanks again.

Source Audio's icon

Why not use same button for 1 video ?
If You have 8 buttons You assign each button 1 video.
So if Button 1 controls video 1, You can first recognise the state of video
playback.
1 if stoped - button starts it, if running, same button pauses it.
if same button pressed again video resumes.If You hold it longer and release,
video plays from beginning, doesn't matter if it was playing or not.
Are You using one movie object for all videos?
What I meant with uploading a patch is to put something together
which would help You get forward, so You have to ask for specific things, decide
how You want that thing to behave.That makes it easier to think about solutions.

xyluz's icon

Actually, button 1 to 9, and a,b,c play different videos.... only, button 1 players a countdown video, button d reset(restart) the countdown video, button e pauses the countdown, or resume the countdown based on the current state of the countdown.

Source Audio's icon

I am sorry, but it is difficult to understand what exactly the problem is.
I need a simple explanation on what each button should do,
like : buttons 1-9 select the video
buttons a,b,c,d,e execute operation on selected Video.
If video 1 is playing, and You press button 2 what should happen ?
Nothing ? Does it wait till one of a-e buttons are pressed ?
Or something else ?
You see one can only guess...

xyluz's icon

Thanks. Sorry i wasn't clear enough.

I have been able to get it to work.

Thanks again.

xyluz's icon

So I have another issue...

my [jit.window] plays the two videos together... say, when button A is pressed, a video is played, and then button B is pressed, instead of removing video A and playing video B, it plays both at the same time...

Is there a way to flush [jit.window] before playing the next video, or is their any other way to handle this?

Thanks.

Source Audio's icon

jit.window is not playing anything by itself.
You probably have jit.qt.movie playing video files.
I would suggest that You start from simple movie object,
check all it can do, and read all the reference about loading,
disposing etc.
Maybe it can do all Your video playback needs.

xyluz's icon

Hi, still working on the same project, I got the videos to work and switch as needed.

But now, I need an audio file played along side the video... here's the problem..

From Arduino, as long as "button A" state is active (i.e it's HIGH) Arduino sends a letter "a" to Max MSP, in my max patch i'm using a "[select]" object to verify the letter being sent from Arduino, and if it's "a" then a video file, and an audio file should play. This is the basic setup, though I have 14 letters being sent from Arduino based on the specific button pressed on my setup.

Now, the problem is, as long as Arduino keeps sending "a" (based on my example") Max seems to always send a bang to my audio and video play setup, so at each bang the audio and video file replay. Which is not looking nor sounding nice at all.

What I need is, a way to keep receiving the letter from Arduino, and as long as its still that same letter, Max only sends a single bang and does not keep sending the bang, so that the video and audio file plays. But, Arduino must keep sending a letter, and Max must keep receiving a letter and keep checking the letter, to know what video and audio file to play.

Any help will be appreciated, thank you.

Source Audio's icon

I would need to have a look at both, Arduino sketch and Max patch to be able to help You.
Why are You sending repeatedly letters from Arduino to Max ?
Is it not enough to do it once ?
On the other hand, one can use change object or some combination of
select -> onebang to filter out repeated values.
But it would be better to do it from the source - Arduino.

xyluz's icon

Yes, I read about [onebang], but I couldn't get it to work. Can you help with how I can make it work?

Here's how I tried to use it:

My Arduino keeps checking the state of each button (I have 14 button all together) and report the state to Max every time, so that when another button is pressed its immediately reported to Max. Or, is their a better way to do this?

Please note that, some buttons are dependent on others... say for example, if one of button 1 to 10 is not active, then button 11 to 14 should not work. This is my main reason for making Arduino send continuous "signal" to Max.

Source Audio's icon

As I allready mentioned, I would need to have a look at both arduino sketch and
Max Patch, at least the parts that makes the switching logic.

Otherwise it is just loss of time, guessing why and what.


You should send buttons states to max ONLY if they are changed.
For that there are several options, to make it easy You could use
bounce2 or button library.
If You can't manage to do so, then You could at least do it in Max.
If buttom 1 - 10 represent 10 Movie files, then You can do some
logic in Max to enable or disable buttons 11 -14.
Onebang can't make any sense like it is seen on Your screenshot.
Onebang needs something that resets it to let one more bang pass.
So please, if You want any further help, than at least post
infos on each button that You use, what they do and how are they
depending on each other.
It would probably take less time to programm the whole thing, than to write this replies...



xyluz's icon

Okay,

Sorry, I didn't know how else to upload them for you to see.

I'll appreciate all recommendations.

Thanks for your help.


Source Audio's icon

I got the files, and will have a look in there
I would have prefered to get a simple description,
like "there are 10 players which can press their buttons,
and when they do so, then this or that happens."
From short look in the arduino code I see there are some
conditions, but it seems too complicated for what it shoul do.
Could You not make a text document with conditions ?
And also is this what it should be or are there more components
going to be put into the whole thing ?
I mean besides 1 Video, and 2 Audio clips ?
And don't worry, I'll try my best to help You.

xyluz's icon

Okay, the full description:

There are 10 player buttons, and 4 admin buttons.

10 player buttons represent 10 contestants.

Admin presses the reset button (one of the four buttons), which begins the countdown video. When this starts, the 10 player buttons become active... which means any of the 10 players can now indicate interest in answering the question (questions are read for the contestant to answer), when the player (say player 5) pushed his button to answer the question, the countdown pauses (say it paused at 8, its a 10 seconds countdown). After the count down pauses, the player info (a video file already pre-loaded) is played.

The player answers the question, and if correct, the admin presses one of his buttons which plays the answer correct video, and if the player answer is wrong, the admin presses the player wrong button which also plays the answer wrong video, and then the fourth button for the admin resumes the countdown.

So all together we have 10 buttons for the contestants, and 4 for the admin.

Each of the player buttons when pressed aside from pausing the countdown video, and playing the contestant details video, should also play the buzzer audio file.

I do hope this makes things clearer.

To answer your questions

Could You not make a text document with conditions ? : I don't understand what you mean.
And also is this what it should be or are there more components going to be put into the whole thing ?: Well, the things i explained above are the components relating to Max, in the Arduino there are LEDs and Relays... But i don't think you need that.

Thanks for the help so far. :)

Source Audio's icon

Here is a bit polished max patch with few changes.

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


Only 10 player infos are repeat-filtered, for the rest I want to know
what exactly has to happen.
Why do You use x instead of 10 ? Maybe because of LED Display ?

xyluz's icon

I tried using 10, but the select did not work, so I resulted to using 'x' so x == 10th player. And, c,w,r, and b are the admin buttons.

Oh, wow, the clean up is awesome. Thanks. Though i don't fully understand it, i'll take my time to study it.

Yes, it should playback.

How did you do this export?

Source Audio's icon

To post max patch here You select all and copy compressed from the file menu.
Thanks for the explanatioin of the whole procedure.
Here is another patch which also filters out repetitions of master buttons.

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

But I am sure that the whole thing needs further stuff, like what do you plan to do
to disable other players buttons after one of them has pushed the button
and admin waits for the answer ?
All that could be regulated in Arduino...
I have to leave now, and will not be online till tomorrow morning,
I'll be glad to explain all the stuff in the patch.

xyluz's icon

Thanks so much!

Yes, please do let me know when you are back.

Source Audio's icon

I am back again. In the meantime I had a good look at the Arduino sketch.
I would suggest one thing - as it seems Arduino has to manage 15 buttons,
10 relays and 10 LEDs.
I would let Arduino only do that, and all logic should go to Max,
because it is easier to reprogramm Max Patch than Arduino, and also
one can simulate all without Arduino.
I f You agree so, I would go on and make both patch & sketch.
One thing would be handy - if You could agree to group all
inputs for buttons, and all outputs in contigous order.
Player buttons from 2 -11, LEDs 12 - 21, relays 22 - 31, master buttons 32-36
Pins 0 & 1 are reserved for Serial communication.
That would make it easier to handle, for example :
for (int ledPin = 12; ledPin < 22; ledPin++) { pinMode(ledPin, OUTPUT); digitalWrite(ledPin, LOW);}
This single line sets outputs 12 -21 as inputs and writes them low. = all LEDs dark
Example 2 :
for (int relPin = 22; relPin < 32; relPin++) { pinMode(relPin, OUTPUT); digitalWrite(relPin, HIGH);}
This single line sets pins 22 - 31 to outputs and sets them HIGH = all RELAYS ON
Handy, isn't it ?
------------------------
Next thing - the reason You had problem with receiving 10 and
recognising it with {sel 10} is because serial transmission happens
using ASCII code, 1 number for each char.
in case of 10 it is ascii 49 48.
So if one expects more than single char, one has to group all incoming data
into a single list, and then output it.
Usualy it is done with zl group and output gets triggered when
new line gets received. On Arduino side "Serial.print(...)" does send
only data, and "Serial.println(...)" adds "new line" and "carriage return" at the end of data.
ascii code for new line is 10 and carriage return 13.
http://www.asciitable.com/
here is simple example :

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


So let me know what do You think about my suggestion.

xyluz's icon

Aye!

please go ahead and help with the optimised patch and sketch.

Thanks a lot.

Source Audio's icon

Fine, I'll try to do it all today.
I need some more infos - what are relays doing ?
here is a simple description the way I understand it, just to make sure:
Game has reset status where :
- all buttons except "game start" are inactive, all 10 LEDs are dark, all 10 relays are ON,
- Game start activates 10 player buttons, starts video countdown etc
- when 1 player presses button :
all 10 player buttons get disabled, that Player's LED lights, and that players Relay turns off,
other 9 relays remain ON, Master buttons reset, right, wrong are active, that Players movie
gets played in max, sound buzz plays once
-----------
The rest of the functions is not clear, You should make a list with
exact infos about what should be done in given situation, like
what should happen if no player presses the button and countdovn movie
comes to its end ?
----------
In the meantim I will make Arduino sketch that will just listen to all buttons,
send that info to max, and all Outputs including LEDs ane RELAYs
will be controled from Max

xyluz's icon

I didn't know I could use Max to control LEDs and RELAYs. Thanks for that, I still have a lot to learn. :)

what are relays doing:

Each player podium has lights, the Relays are turning on and off the lights. So when the player buzzes, his podium lights up to indicate who buzzed.

- all buttons except "game start" are inactive, all 10 LEDs are dark, all 10 relays are ON, - Game start activates 10 player buttons, starts video countdown etc - when 1 player presses button : all 10 player buttons get disabled, that Player's LED lights, and that players Relay turns off, other 9 relays remain ON, Master buttons reset, right, wrong are active, that Players movie gets played in max, sound buzz plays once

Yes, this is correct.

what should happen if no player presses the button and countdovn movie
comes to its end

The countdown video stops, and a time up audio is played.

In the meantim I will make Arduino sketch that will just listen to all buttons,
send that info to max, and all Outputs including LEDs ane RELAYs
will be controlled from Max

Thank you, I appreciate it.

I should be able to work my way around the other issue with that.

:)

Source Audio's icon

I am almost done with Arduino code, need just to have a good look, just to make sure nothing was typed wrong, and all Pins are correct. Here are few infos :

10 Player buttons on Pins 2 - 11
10 Leds on Pins 12 - 21
10 Relays on Pins 22 -31
Master Buttons : RESET Pin 32, START pin 33, RIGHT Pin 34, WRONG Pin 35, EXTRA Pin 36 EXTRA just in case one needs this "Unknown" function.
Player Buttons send 1 - 10 to Arduino, Master Buttons 11 - 15. Buttons use bounce Library to detect transition from High to Low, and output is only produced when buttons really are pressed. One has to activate internal Pull Up Resistors on Arduino, and connect buttons as in this picture :
I will send You the library together with the Sketch

xyluz's icon

This is great!

Thanks.

Source Audio's icon

Here is Arduino sketch and bounce library.
I don't know which Arduino IDE version You use, and
if You are on Mac or windows, You should just put the bounce
library where other Arduino libs are.
And don't update it, if You have automatic updates for libs activated.
It is old lib, there is new one bounce2, but I am used to simplicity of old one.
You can test LEDs and Relays by just sending 1 -10 from max, 0 = reset

BallGame.zip
application/zip 11.07 KB

Here is simple test patch:

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

You are using Mega 2560, right ?

Source Audio's icon

The logic in Max for Audio/Video Playback, handling of
which buttons are active/unactive depending on Game Status
and all the rest should be easy thing to do, I will help You with that,
I just need the scenario ...
I am away for 2 hours now, if You have time in the meantime to check
the stuff, just let me know if everything works the way it should.

Source Audio's icon

Here is Max patch to go with new arduino sketch.
It is by no means finished, because exact handling of
players and admin actions are not known to me.
But most of stuff is done, like gating of player buttons etc
Important questions for me are what exactly happens after the game starts,
what when players miss the countdown time, what if
answer is correct, what if wrong, does repeatedly triggered START
button pauses/resumes countdown movie, or starts it from
beginning etc
Only when everything is clearly stated, one can build the proper logic.

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

I will be away for the rest of the day, and also most of the tomorrow,
but just post here whatever questions or problems might arrise.
I will answer as soon as possible.

xyluz's icon

Hi, thanks for all help.

Everything works!

Only one issue.... the buzz sound play is not consistent. Let me explain, if player A presses the button, the sound comes up... the admin resets, if player A presses the button again, the buzz sound does not play, except another player presses.

You get?

Source Audio's icon

No I am sorry but I don't get it.
You should use better explanation.
And as I posted in last mail, the whole order of
what shoud happen when... is not clear to me.
So if You want me to do anything here, write proper
description.Just to make sure here one example of what I want:
Everything is RESET.
To continue, Master has to press START Button.
At that moment ONLY :
10 Player Buttons and Master Reset Button are active.
When 1 Player presses the button BEFORE countdown movie is finished,
countdown movie gets replaced by that player's movie, sound buzz.mp3 plays,
All other player buttons are inactive, Master right and wrong and reset buttons
are active.
What happens if master presses right or wrong button before buzz sound plays to the end ?
If no player presses the button and countdown movie finishes ...
And so on...
Otherwise I don't know what You really want.
----------
Do You mean by :
"the buzz sound does not play, except another player presses"
maybe this :
"the buzz sound SHOULD not play, except another player presses"
That is a big difference.
RESET means everything is reset, meaning any player can press the button.
If You want to disable last active player, than You need some other statement than reset.

xyluz's icon

Hi,

Thanks for all your help.
I have been able to achieve all i want for the project.

Thanks a lot.

Source Audio's icon

That's good news.