Random notes in Max through Midi keyboard using noteout

miso's icon

Hi,

I'm working on a sensor project with Arduino for school and would like my output to be sounds from Max when it receives a 1 or 0 from Arduino. I'm a complete beginner, just exploring possibilities , and wondered if you could help with this option - playing random notes / short melodies through my midi keyboard (has onboard speakers). So far I'll get one note through and it stays for a long time, changing to another note unexpectedly. I realise there's a lot I need to learn, would appreciate any guidance! Thank you.

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

Source Audio's icon

check makenote object.
It will create note off's for note on's you create using random object
Limit note range a bit, it's hard to hear that low notes .

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

miso's icon

That did the trick, thank you. Would you mind one other question, related to the other ways I'm trying to output sound? With playlist, is there a way to make the full track play without retriggering? (currently every time the sensor sends a 1 it starts the track over).

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




Source Audio's icon

For that you issue playing state and disable receiving of sensor
messages till track stops playing.
but if track plays in loop mode ... that's more a logic question, and not how to do it.

miso's icon

Hi, thanks very much for your reply. I wasn't able to figure it out disabling receivng of sensor messages in Max, but nevermind for now since my deadline is coming up , the delay in Arduino helps! But I have a big problem now - for some reason, the random note generator does not turn off and goes on until it seems to hit an infinite loop, and I have to shut down. I used the makenote and clear msg but it seems to have no effect. Also switched up my sensors to test, and indeed whenever the random note patch is attached to a sensor, it gets stuck on 1. Would you kindly have a look at my patch, if there's something I've done wrong?

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


miso's icon

I just realised the pgmout might have something to do with it, as when both random generators are running it puts it into forever mode - it'd be great to understand why and how to avoid this problem! Thank you.

Source Audio's icon

I want to see Arduino code to be able to see what is going on.
Maybe it is simply badly programmed and sends data too often.
Remove that port z and port b.
If you want to close the serial port in Max, use close message.
But that is needed only if another app needs to use that serial port, like
Arduino IDE to upload the code.

Then ...
You should not send pg change simply like that, as it might cause hanging notes.
You understand what I mean with that ?
Not all synths are set to turn all notes off if they receive program change message.

You use 2 makenote objects, which can cause problems if they talk to same midi
device on same midi channel.

Source Audio's icon

Playlist has this playback notifications outlet.
This is not perfect solution, as it won't work if pause is used or
playback loops.
But for your simple case it would do.

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

miso's icon

Thank you so much!
I changed the midi channel to match on the makenote/noteout and pgmout, and also saw it worked when I changed to different devices. I liked the effect of the random midi programme change but I can see it's not ideal... when I have time to do something more musical I won't have to resort to random things.
Playback notifications outlet - thanks for showing me how this works with your patch, really helpful and it works great.
I've left the Arduino code from Adafruit MPR121 almost as is except the print statements to send 0's and 1's. I will get some help with this part when I go to school today. Really appreciate, I don't really understand what I'm doing yet so it feels great to learn these small details :)

Source Audio's icon

I see, adafruit touch sensor.
You say :
I've left the Arduino code from Adafruit MPR121 almost as is except the print statements to send 0's and 1's.

I guess you replaced :


Serial.print(i); Serial.println(" touched");}
Serial.print(i); Serial.println(" released");}
with
Serial.print(i); Serial.println(" 1");}
Serial.print(i); Serial.println(" 0");}
which would come out as list
0 1 or 0 0 in Max for 1st Sensor Pin, 1 1 and 1 0 for 2nd etc
(*** touch pins are numbered from zero up.)

But that is not implemened in your Max patch.
You need to distinguish between touch and release and also which
pin is touched/released, otherwise you will double trigger.
If all you want is to detect touch and ignore release
remove this part in Arduino code: (or comment it out)
if (!(currtouched & _BV(i)) && (lasttouched & _BV(i)) ) {
Serial.print(i); Serial.println(" released");}
---------------------
If you want to keep both, then in Max you need to do something like this

*** Replaced that example, Adafruit numbers pins from zero

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


But without knowing the Arduino Code exactly,
it is just a guess.

miso's icon

Wow, thank you for continuing to follow up, it's encouraging. That's right, as you said, this is what I have:

for (uint8_t i=0; i<12; i++) {
// it if *is* touched and *wasnt* touched before, alert!
if ((currtouched & _BV(i)) && !(lasttouched & _BV(i)) ) {
Serial.print(i);
Serial.print(" ");
Serial.print(1);
Serial.println(" ");
//Serial.print(i); Serial.println(" touched");
}
//if it *was* touched and now *isnt*, alert!
if (!(currtouched & _BV(i)) && (lasttouched & _BV(i)) ) {
Serial.print(i);
Serial.print(" ");
Serial.print(0);
Serial.println(" ");
//Serial.print(i); Serial.println(" released");
}
}

The number boxes attached to unpack show me sensor number and whether it's touched/released, I thought? The retriggering is the issue though? Sorry that I'm not quite understanding. I haven't had a chance to play with only touch, but thanks - it's something I should think about.

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

Source Audio's icon

you need to send only pin number, space and state including linefeed (println)
Serial.print(i);
Serial.print(" ");
Serial.println(1); ( or zero for released )

Problem is in max patch.
You receive a list with pin AND state, for both touch AND release.
Did you look at patch I posted ?

You have to understad what max objects do.
Sel 1 2 3 4 5 etc will output a bang if it receives matched number.
That bang sent to toggle will change the toggle state.

You receive for example 5 1 from sensor = pin 5 touched

sel object sends a bang from outlet which matches 5 to toggle.
If toggle was off it will turn on = starts metro.
When pin 5 gets released it sends 5 0
AGAIN sel 1 2 3 4 5 ...
will bang and change toggle state to off .
You don't want that.
----------
You have 2 options
1 remove sending of release state from arduino
or
2 use stuff in my patch to bang only on touched input.

I can not explain that in simpler words.

Source Audio's icon

AND remove that clear from makenote,
use stop if you need to clear hanging notes.
clear message does the oposite - prevents makenote to turn notes off,
and so they hang !!!!

Source Audio's icon

P.S. if you want to control toggle with both touch and release,
than use the left example I posted - metro will run only while pin is touched,
and stop when released.

miso's icon

Amazing, thank you, very kind to explain that explicitly for me. I had wondered what the difference between clear and stop was too - I didn't understand what the documentation meant by cancel future note-offs, so I get it now!
I want to control with both touch and release so will use the left example you posted !
Thank you so much!

miso's icon

Just wanted to say, the next day the pin finally dropped I adapted your example on the right for use. Lists, how to use the if statement and zl change, got it now. Thank you again.