Arduino - MAX/MSP sequencer play sound file to the end even if triggered again by sensor

Tamas Mann's icon

Hey guys! I'm pretty new to Max. For my university final project I create a sequencer. I would like to ask for your help, because I'm stuck at a point now. About the project: There is a wooden cover, in which there are drilled holes in centric circles. 16 hole each circle. (There will be 5-6 circles) Under the cover there is a 360 degree rotating stuff with the IR sensors. The IR sensors goes under the drilled holes. If a ball is put on a hole, I want Max to play an audio file. (Bass drum, snare, hihat etc) So in this way I create a 16 step sequencer.

I managed to do the communication between arduino and max, i can read the analog input values. These values goes through a Schmitt trigger, with its help I can easily calibrate when to trigger. If the sensors value goes above the set limit a soundfile is triggered. Here comes my problem: Now the soundfiles start over when triggered again. I want to play every soundfile to the end, even if triggered again. It would be necessary if the sound is a cymbal..or any sound that is longer than a simple bass kick for example.
Every little help would by highly appreciated. Thanks in advance!
I hope it makes sense, and sorry for my bad english. (My patch is attached)

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

Source Audio's icon

Ramp end of line~ reactivates onebang

By the way, doing trigger in Arduino instead in max would reduce
amount of serial communication a lot.
And for such playback, sfplay~ would do just fine, no need to load buffers.
sfplay also bangs on play end.

Tamas Mann's icon

Many thanks for your reply! I think there's a misunderstanding. I think i couldn't emplain my problem good enough. I tried what you suggested. In this way if the soundfile is triggered, then the soundfile plays to the end of the file, and I can't trigger it again, I have to wait till the end of the file to be able to trigger again. That is not really what i want. I want to be able to trigger the file whenever I want, and it should play till the end every single time it's triggered. So if the sound is a long cymbal sound, then i would like to be able to layer this sound, if it doesn't end before the next trigger. Does my explanation makes any sense? :D

Source Audio's icon

I see, than You need poly~ with several voices enabled.
Or You could use some vst plugin as sampler

Source Audio's icon

Another option : if You don't want to go into trouble with poly,
I mean if this is all only for drum sounds which are anyway one shot samples,
and I see that You have no velocity integrated, try this simple approach :

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


You can extend number of voices by ading more gate outputs and players

Tamas Mann's icon

Thanks! I'll try those methods today after work.

Source Audio's icon

You are welcome.
Just out of curiosity - how many IR Sensors will be involved ?
I am sure it would be better to let arduino do the trigger detection.

Tamas Mann's icon

The performance would be better that way? I did the trigger detection in Max because it was easier for me. There will be 6 Sparkfun QRE1113 sensors. I attach a very ugly 3D model of the final object..if you are curious. :) I will build the whole thing in a suitcase, and there will be a screen connected as well, which will be covered, so only a circle shape will be shown. Every sound will give a visual feedback too. Yeah now i try to figure out the video part as well :) I have to be able to trigger the videos with the sounds, and play on top of each other. (Or play with "add" blending mode, since the videos will be some abstract black and white stuff) But that's another question :)

The thing..

Drilled holes on the cover. If you cover a hole (put a ball there) a sound will play.

Sensors under the cover

Source Audio's icon

I am not sure if I understand how the sensors and balls mechanically work,
but it is not so important.
The important thing to know is that the sensors are sensitive to light as well,
so depending on environment light, results could be different.
Also speed of rotation makes difference, I mean if sensors rotate and
pass the place where ball is, to sense reflection.
That would force You to do fast scan of analog inputs,
which could be a bit of strain for serial port, if many sensors are connected.
If arduino would just send a number when one sensor crosses threshold,
that would drasticaly reduce data flow .
on receiver side You would need just sel 1 2 3 4 5 6 to trigger 6 samples.

But I think the man challenge is to construct the whole thing so that
sensors can reliably work, and not trigger if somebody just sticks a finger
over the hole where balls should be.

I had a similar project once for escape room, and at the end decided
to go with magnetic balls and sensors, because IR sensor was unreliable
under non ideal conditions.

Tamas Mann's icon

the sensors are sensitive to light as well,
so depending on environment light, results could be different.

Yes i know that. It might will be a problem, yes. I'm thinking about the different solutions, for example paint the bottom of the cover white, and the balls black...or paint the balls red, so it will be always different. I don't know it yet, but it doesn't really matter at the moment. I have to make it work on one light condition. This will be the first version. I shoot a video, and thats ok for now. Later i can try different solutions, to make it work independent from light conditions.

If arduino would just send a number when one sensor crosses threshold,
that would drasticaly reduce data flow .
on receiver side You would need just sel 1 2 3 4 5 6 to trigger 6 samples.

This method would make the readings faster? I mean, with no delay. (but ofc there will be a little tiny delay, I know that :D ) I will try to implement this threshold system what i got to arduino, thanks. Do you have maybe some arduino sketch that can help me?

not trigger if somebody just sticks a finger over the hole where balls should be.

That is not a problem yet. People will only see this stuff in December, for now I just have to make a video documentation about it. And in the video I won't stick my finger there :) But of course I want to make it stable, so later I will use the best possible solution, but for now...I ordered the wooden balls and IR sensors yet, so..I have to make this thing work.

Another question to you: Do you have any experience with the multiple video playback I mentioned in my previous reply?

Source Audio's icon

This is simple patch that could serve the purpose.
I did not go for array for readouts etc so that You can
easily follow what is going on.

--------------------------------------

int T1 = 0; int T2 = 0; int T3 = 0; int T4 = 0; int T5 = 0; int T6 = 0;
int exT1 = 0; int exT2 = 0; int exT3 = 0; int exT4 = 0; int exT5 = 0; int exT6 = 0;
int THRESHOLD = 100; // threshold

void setup() {Serial.begin(57600); } // faster serial port - set in max as well

void loop() {
int S1 = analogRead(A0); if (S1 > THRESHOLD) {T1 = 1;} else {T1 = 0; exT1 = 0;} if (T1 == 1 and T1 !=exT1) {Serial.println(1); exT1 = T1;}
int S2 = analogRead(A1); if (S2 > THRESHOLD) {T2 = 1;} else {T2 = 0; exT2 = 0;} if (T2 == 1 and T2 !=exT2) {Serial.println(2); exT2 = T2;}
int S3 = analogRead(A2); if (S3 > THRESHOLD) {T3 = 1;} else {T3 = 0; exT3 = 0;} if (T3 == 1 and T3 !=exT3) {Serial.println(3); exT3 = T3;}
int S4 = analogRead(A3); if (S4 > THRESHOLD) {T4 = 1;} else {T4 = 0; exT4 = 0;} if (T4 == 1 and T4 !=exT4) {Serial.println(4); exT4 = T4;}
int S5 = analogRead(A4); if (S5 > THRESHOLD) {T5 = 1;} else {T5 = 0; exT5 = 0;} if (T5 == 1 and T5 !=exT5) {Serial.println(5); exT5 = T5;}
int S6 = analogRead(A5); if (S6 > THRESHOLD) {T6 = 1;} else {T6 = 0; exT6 = 0;} if (T6 == 1 and T6 !=exT6) {Serial.println(6); exT6 = T6;}
}

---------
If You wish to set threshold value from max, so that it does not
allways need to be reprogrammed just post here, it can be easily added.
And if nothing else needs to be sent from arduino, one could use Serial.write
which would send plain ints and one would not need that
sel 10 13 , zl group etc stuff in max

I am not using video in max at all, have absolutely no interest in it,
so can't really help You with that, but I think that it should not be a big deal
to mix several videos in same window.

Tamas Mann's icon

You have no velocity integrated, try this simple approach :
Pasted Max Patch: Click to Expand

You can extend number of voices by ading more gate outputs and players

Hey, I forgot to say a big thank you for this patch, and everything. That's exactly what I wanted.

For now, I will do the threshold setting in Max, it's fast enough, but if I'll have more time, I will try to do it in arduino.
Many thanks!

Source Audio's icon

You are welcome, I know well enough that learning
both arduino and max at same time can be quite challenging,
so I am glad to be of any help.

Tamas Mann's icon

Yeah, it is :D Now I could try the Adafruit QRE1113 sensors, and they are genius! Really. You should give it a try..if you have project where you could use it:) They are NOT sensitive to light conditions. They can distinguish my white and black surfaces 100% percent it pitch dark, or in a room with lights on. To be honest..I'm very suprised. And they respond silly fast. I love them.

Source Audio's icon

Glad to hear everything works for You.
I did use 1113 sensors in the past - not adafruit boards,
but sensors as is, and they did get influenced by light.
But that all depends on where one puts them in.

tmhglnd's icon

As of Max8 MC is great for these sample-based polyphonic instruments!

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