max/msp piano sound with Arduino Capacitive sensor.
Hello! I am working on the Touch Piano Project.
I'm asking you a few questions in progress.
I am currently working on making touch piano using Aduino. Currently, the capacitive sensor in Aduino is delivered well by max/mp, but Max still outputs sound when I don't touch this sensor.
I've tried some coding to make sound when I touch the sensor, but it keeps ringing to see if the coding is wrong.
The quality of the reference image I refer to is poor and I can't copy it. I've circled the unknown part of the picture.
There are two things I want.
1. It only sounds when I touch Aduino's capacitive sensor.
Two circled part of the reference image of instruction.
I will upload the max/msp patch that I made now, so I hope you give me feedback.
As in Your other post about all this , post the max patch.
Otherwise NO HELP:
Unlock patch, go to file menu and select
Copy Compressed.
Paste it here.
And post finally real info about the project, like for example:
1. I am using Arduino to sense 6 touch sensors.
2. Max shoud read the 6 values. What are the Values ?
What range ? or only On / Off as a switch ?
3. Sensors play allways the same note but sensitivity
controls midi note velocity . Or something else?
Should sound be midi or some Synth in Max ?
---------
If You don't post proper infos about the projects,
it is no fun at all to spend any time trying to provide any help.
I made a lot of mistakes asking for advice. I'm sorry.
The current sensor value on the aduino coding is read very well at max/msp.
Touch the capacitive sensor and the sensor values in the max program are between 10000 and 30000.(in max)
However, when the sensor is not touched, the max still sounds. I want to make it sound when I touch each of the sensors with three pins. Like a piano.
Here's my future plan.
------------------------------------
Three pins will be used to complete the operation and an additional six pins will be created.
I want to create a piano sound when I touch each of these six pins.
------------------------------------
If possible, I would like each sensor to have several tones like a piano.
Also, the 'receive' command on max does not seem to work well.
It seems that the command value is being transferred to the 'if' value, but there is a lack of knowledge about it.
There may be some things that I didn't understand because I followed the current coding by looking at the reference image. I want you to understand even if you don't have enough explanation for this.
Thank you very much for your help.
input : human touch
output : sound (piano tone)
You made a lot of mistakes in that patch.
Let's start from the beginning:
forget about turning dsp on and off to start or stop piano sound.
That is like throwing the motor out of the car just to stop it.
---------------
Arduino values from sensors should be scaled
to some sensfull value in Arduino sketch.
if You know what maximum value is,
map that to velocity range 0 127.
That will make serial communication snappier.
And - add a bit delay at the end of the sketch,
like delay(10);
-------------------
In Max You get values which You must turn into
note on and note off information.
If You managed to rewrite arduino code to send
zero when sensors are not touched, higher values could be used for velocity.
--------
Here is changed Arduino Code:
#include <CapacitiveSensor.h>
CapacitiveSensor yellowCapSense = CapacitiveSensor(4,8);
CapacitiveSensor redCapSense = CapacitiveSensor(5,10);
CapacitiveSensor greenCapSense = CapacitiveSensor(6,11);
void setup() { Serial.begin(57600); // more speed !!!!
}
void loop() {
long yellowCapVal = yellowCapSense.capacitiveSensor(50);
yellowCapVal = map(yellowCapVal, 0, 30000, 0, 127);
long redCapVal = redCapSense.capacitiveSensor(50);
redCapVal = map(redCapVal, 0, 30000, 0, 127);
long greenCapVal = greenCapSense.capacitiveSensor(50);
greenCapVal = map(greenCapVal, 0, 30000, 0, 127);
Serial.print(yellowCapVal); Serial.print(" ");
Serial.print(redCapVal); Serial.print(" ");
Serial.println(greenCapVal);
delay(10);
}
-------------------
Change the map values 0 30000 depending on the sensor values to get wished result
Here is Max Patch ready for 6 sensors
-----------------
That should help You to start with.
I would be better to change Arduino sketch so that it sends only values for
single sensor at a time, and not the whole list.
But let's leave that for another time.
You are an angel or a genius or a savior to me!
Thank you very much for investing your time for me.
Thank you sooooooooo much!
I checked the value from max/msp through the sketch you told me. I think it's very stable.
But how do we implement sound with a touch?
I am really sorry to ask you again...
I tried to put a piano tone on the patch you sent me.
I see and see the help of the command in the pre-modification patch, but I don't understand it.
As for me who first encountered max/sp through YouTube, I find it very difficult to apply a piano tone to the code you sent me.
I asked you once again because I have no knowledge of coding. I tried to apply the command that made the sound in the previous patch, but it doesn't sound
If possible, could you also post a patch for implementing sound?
I'm really sorry.
You need to tell midiout where to send the notes.
Either doubleclick on it when patch is locked and select midi output,
or try midiinfo for that.
What OS are You using ?
Mac or Windows ?
I am using mac!
You mean double-click on Midiinfo and insert a piano tone in here?
But even if you double-click Midiinfo, it won't open.
I'm not sure what the output device below means.
You mean the sound device?
Select AU DSL Synth.
That is Mac Virtual Synth.

If You don't have it in the list, something
is wrong with Your OS installation.
What system version is it ?
I`am using max8 version.
so I could found something that matched your image.
I'm looking for a YouTube video clip related to this command. By any chance, could you show me more patch about how to make a piano sound with this command?
I wish you luck all the time!
There is no more command, or I don't understand what You are asking for.
Select AU DLS Synth 1 from the menu and it should sound.
or loadbang message "AU DLS Synth 1" to midiout.
You wrote that nothing happens on doubleclick on midiout.
Is that really true ???
I can hear it! The value of touch sensing was so low that it couldn't be heard! I don't know how to express my gratitude. I'll try to control the sensitivity of sensing. Thank you very much. Thank you for saving me!
You have to check the mapping in Arduino code.
I suggested
yellowCapVal = map(yellowCapVal, 0, 30000, 0, 127);
because You wrote that sensor values (when touched) range from
10000 - 30000 .
Maybe You have to adjust that.
To start with I would quit Max
(because only 1 software should connect to Arduino)
and check in serial monitor the raw, unmaped values.
So You know what to deal with.
If You build all touch stripes the same, 1 instance would be enough:
#include <CapacitiveSensor.h>
CapacitiveSensor yellowCapSense = CapacitiveSensor(4,8);
void setup() { Serial.begin(57600); }
void loop() { long yellowCapVal = yellowCapSense.capacitiveSensor(50);
Serial.print(yellowCapVal); Serial.print(" ");
delay(20);
}
---------
just set Arduino serial monitor to 57600
that will show You what values to map.
You'll be full of luck.
There are times when Max can't accept Arduino serial (ex serial B 57600). so it is not accept serial value.. If The serial port is correct. Is there a solution when this happens?
57600 is STANDARD baud rate.
Never heard of arduino not accepting it.
What board are You using ?
The other thing is that Arduino will not allways appear as "b" port.
It depends on all connected serial devices,
so sometimes it could be a, b, c, or whatever.
Ah You need to learn soooooo much.
Send print message to arduino to show devices and ports available.
Then You can send port a, or port b to initialise it.
Or even better device name.
Send me a screenshot of max status window
after You send print message to serial object.
Then I can build something for You
to detect and initialise correct Arduino device.
for example:
I made a board named usbserial-Pedal,
so I can activate it by name, or even check if it is
connected and alert if it is missing.
Also You can send close message to disconnect
serial port.
Hi.
I received your feedback last time and succeeded in connecting the three pins. So I hooked up my final target of six extra pins to Aduino. The serial monitor confirmed that the six pins are working normally and the max phase does not seem to get a real-time value.
When I used 3 pins, the patch you sent me and the aduino code used to make sound after receiving the normal real-time value, but after connecting 3 more pins, the max did not respond.
I tried cereal a, b, and c according to your advice and checked that the value is coming from serial b, but the sensor value is not reflected in real time, so I posted this message.
I think three more pins are made and this value doesn't match the coding or you don't get these values at max.
Can you give me any advice on these points?
------max (serial port)
------Arduino Serial Monitor Value
-----Arduino Code:
#include <CapacitiveSensor.h>
CapacitiveSensor yellowCapSense = CapacitiveSensor(4,8);
CapacitiveSensor redCapSense = CapacitiveSensor(5,10);
CapacitiveSensor greenCapSense = CapacitiveSensor(6,11);
CapacitiveSensor blueCapSense = CapacitiveSensor(7,13);
CapacitiveSensor purpleCapSense = CapacitiveSensor(9,12);
void setup() { Serial.begin(57600); // more speed !!!!
}
void loop()
{
long yellowCapVal = yellowCapSense.capacitiveSensor(50);
yellowCapVal = map(yellowCapVal, 0, 30000, 0, 127);
long redCapVal = redCapSense.capacitiveSensor(50);
redCapVal = map(redCapVal, 0, 30000, 0, 127);
long greenCapVal = greenCapSense.capacitiveSensor(50);
greenCapVal = map(greenCapVal, 0, 30000, 0, 127);
long blueCapVal = blueCapSense.capacitiveSensor(50);
blueCapVal = map(blueCapVal, 0, 30000, 0, 127);
long purpleCapVal = purpleCapSense.capacitiveSensor(50);
purpleCapVal = map(purpleCapVal, 0, 30000, 0, 127);
Serial.print(yellowCapVal); Serial.print(" ");
Serial.print(redCapVal); Serial.print(" ");
Serial.print(greenCapVal); Serial.print(" ");
Serial.print(blueCapVal); Serial.print(" ");
Serial.println(purpleCapVal);
delay(20);
}
If Arduino Serial Monitor receives all 5 values as expected,
than max should also do so.
If max is receiving nothing, than You are probably having
Arduino Serial monitor open.
That prevents max from connecting to device.
If You want to switch between the both, than make this 2 messages in max patch
1 close
2 port usbmodem14201
and connect them to serial object.
When You want to use arduino serial monitor or upload code,
send close to serial object.
When You want to use Max then send message port usbmodem14201
to serial object.
----------
usbmodem14201 is arduino UNO
--------
Which sensor library are You using ?
One more thing :
Capacitive Sensor library uses interrupts to read
pins in use.
If any pin is included in the code, but is not really connected
to send pin, than the loop will be stuck, waiting for result
from missing pin.
Also I don't think You can use pin 13 on UNO bcause it has
LED connected.
From what I remember, one send pin was
enough for 3 sensor pins.
so You could maybe go with 2 send pins, each havin 3 receiver pins.
Thanks to you,
I've solved the existing signal problem. Thank you.
By any chance, you made a patch to make the piano sound through the pin value.
I want to add a sound that like a drum sound.
I also want to add various mp3 sounds.
If possible, could you make me a simple patching example?
Good luck with all your parts.
As first, I am glad You got the sensors working.
As second - You need to learn things Yourself,
it is allways better than getting stuff premade.
If You want to make other sound than piano,
You need to learn what AU DLS Synth is,
and when You find out that it is General Midi ( GM )
capable software synth, than You need to learn what that means,
and then what midi is, in order to
understand that drum sounds are playable
on midi channel 10.
Also that on all midi channels You can send program
change to recall different sounds, or different drum sets
on midi channel 10.
read this :
https://www.midi.org/specifications-old/item/gm-level-1-sound-set
Max uses 2 approaches for midi communication.
1 Raw midi using midiin and midiout
2 Using dedicated objects for each type of midi messages
like notein , noteout, pgmin, pgmout, ctlin, ctlout etc
midiin & midiout work by using raw midi messages
144 100 66 means midi note 100 with velocity 66 on midi channel 1
145 100 66 means midi note 100 with velocity 66 on midi channel 2
192 33 means program change 33 on midi channel 1
193 33 means program change 33 on midi channel 2
etc etc
You must read all that stuff in max lessons, tutorials, examples,
helpfiles etc.
so for playing drum sounds on AU DLS Synth :
153 37 $1 will play sidestick with received sensor velocity.
153 = note on midi channel 10
37 = midi note, in GM drum set = sidestick
------------
If You want to trigger non midi sounds from the triggers,
You need audio playback in max,
try sfplay~.
-----------
I am always grateful for your kind explanation.
As you said, I am trying to understand max by looking at various patch tutorials.
But I want to ask you one question.
I am developing the max patch and aduino sauce that you made for me. I want one thing.
In this case, the "value" comes in but does not meet the expected value, making it sound small or not at all.
I want to adjust the sensitivity so that even a small touch can make a sound right away.
So I thought,
I made several attempts to control this value.
In my case i`ve roughly adjusted the value on the arduino source. ex) 10000~30000. (15000, 20000, 25000...etc)
But there's no noticeable change, and there's no sensitive value I want. Do you happen to know how to solve this problem?
The real-time value of max is fluctuating between 20 and 60.
It is the same if i adjust the 'expected value' in max.
I think i need to modify the Aduino code, but if you know what part to modify, please let me know.
your very kind people.
I`m always thanks for your opinion.
You have to adjust Arduino map values.
I suggested to map raw sensor value
from 10000 - 30000 to 0 - 127 (midi velocity)
Because You wrote at the beginning that this is the
range that You get in max.
I posted Arduino code to measure RAW sensor output
in this thread at Nov 11 2019 | 3:47 pm
You have to see what the range that You want is
and put that values into map.
For example :
If lightest touch that You measure is 6000
and strongest 25000, You would make :
long yellowCapVal = yellowCapSense.capacitiveSensor(50);
yellowCapVal = map(yellowCapVal, 5000, 25000, 0, 127);
yellowCapVal =
constrain(yellowCapVal, 0, 127);
Constrain keeps values between 0 and 127.
Do that for all 6 sensors.
That way You should get more or less the range that You want.
But than in Max You could also try to shift the velocity to be more sensitive.
That could be done in several ways, but simplest is to
just add and clip :
I adjusted the sensing value as you told me, and I was able to get a value that almost matched the expected value.
-Aduino
As you can see, on the Aduino phase, it's about 10 to 20 on average for a light touch.
-max
Approximate average values are shown here because of the additions to the max.
But, it has a new problem!
In the previous patch, the piano sounds were accompanied by touch values, and for some reason, the piano sounds are not available even though the touch values have reached their expected values. Can you see if my settings are wrong?
-additional shot.
I`m using 10m resistor and
currently using 3pins.

-Current
As you told me, I am studying tutorials and examples that make various sounds. (ex)midiout / ad/dls sync) I'll make a great sound.
You should do the measurement first.
NOT taking examples I post, because that is not the values You receive.
AGAIN :
You have to measure raw sensor output without map first.
Then one can adjust the proper values for the map.
Do this first and post here only 3 infos :
1 what value You get when You don't touch anything
2 what value You get at lightest touch
3 what value You get at maximim touch contact.
WITHOUT map !!!!!
After that I will look at the rest.
Here is the complete code, upload this and look into serial monitor:
#include <CapacitiveSensor.h>
CapacitiveSensor yellowCapSense = CapacitiveSensor(4,8);
void setup() { Serial.begin(57600); }
void loop() {
long yellowCapVal = yellowCapSense.capacitiveSensor(50);
Serial.println(yellowCapVal);
delay(50); }
P.S.
As now more objects are in the Midi / Velocity part,
timing is wrong, so use this version of Max Patch :
You will not get any sound if You don't receive 0 (ZERO)
when there is no touch contact.
That's because only zero velocity enables another midi note trigger.
Without it, You would get repeated midi notes like machine gun.
okay, I understood that...
As you mentioned, I measure that your told me thing.
1 what value You get when You don't touch anything
-Repeated 0~7.
2 what value You get at lightest touch
-briefly 400~1100 value.
-The light touch reacted sensitively and even went up to 1300 from 400 when the hand first touched it.
3 what value You get at maximum touch contact.
-
It may not suit the subject, but what country are you from? Is everyone in your country so kind? :)
Thank you for your help!
Ok, that seems a bit low for what is expected from the sensor.
Usually one uses stripes of copper foil or similar,
and then the difference could get bigger from no touch - light touch - full contact.
Also sensors should not touch anything else, like in the picture You posted
the wires were placed on MacBook which is grounded.
I am telling that because if You change the sensors wire length,
or use something different to touch them the measured values might
get very much different.
Changing the resistor values and adding a little capacitor
could help, but this is rather tricky project.
-----------
Which sensor library are You using ?
this is the link to newest one :
https://github.com/PaulStoffregen/CapacitiveSensor
take version 05.
here good infos about hardware :
https://www.pjrc.com/teensy/td_libs_CapacitiveSensor.html
-----------
If Your sensors hardware remains as is and You get raw values like You posted,
than try this :
long yellowCapVal = yellowCapSense.capacitiveSensor(50);
yellowCapVal = map(yellowCapVal, 10, 1700, 0, 127);
yellowCapVal = constrain(yellowCapVal, 0, 127);
mapping from 10 instead of 0 makes sure that You allways send zero
when sensor is not touched.
----------------
look at this :
https://www.youtube.com/watch?v=BHQPqQ_5ulc
----------------
If this project is important for You, and You want to get steady result,
think about using teensy LC instead, which has dedicated
touch input pins, and ready to use library.
From PRJC :
Teensy LC, 3.0, 3.1, 3.2 & 3.6 have superior hardware-based capacitive touch sensing circuitry on specific pins. They do not require this library. To read capacitance on those pins, simply use touchRead(pin). Hardware-based sensing offers much faster measurements with better stability over temperature variation.
https://www.pjrc.com/store/teensylc.html
----------
and - i am situated in switzerland ...
Sensitivity of sensors seems to have been modified to some extent, but I have one question.
Why can't I hear the sound when I put a certain value in it?
I can't hear the patch file that I used to touch.
What did I do by mistake?
I guess the patch you left me looks like no problem...
post compressed patch that You are using.
I have explained all needed in my posts.
So You need to upload compressed patch that You are using
if You want me to look at it.
Are You are using the patch I posted
on NOV 25 2019 ?
I'm sorry. I'm a little late.
I studied your reference materials and Au dls synth.
And I solved the problem of not being heard. It was because the MIDI settings were changed.
The sensing value is stable now, and the drum sound is coming out very well.
I used the data that told me about the sound of the drums.
Now I only have to increase the number of pins 3 to three to six
Thank you very much.
I am Korean. Switzerland is a wonderful country.
Because there's someone like you. :)