Today we are going to be taking a look
at passing data from Arduino into Max using serial port.
Simply, what I've got
is a single button set up on Arduino on
my digital pin 2, and every time that's
pressed, we're gonna pass a message into
Max that allows us to control something.
1? of my last views, I’m going to start
outside of Max and deal with all the
externals before we go into the “meat and bones”, if you will.
Here's a quick
drawing off the circuitry that I'm using.
So in my Arduino, we're going to write a
simple program that checks to see if a
button’s been pressed. If it has, it’s going to
randomly generate a number between 0 and 1000
and then send it on to the
serial port. 
So I'm going to start off with my constant variables; 
I'm gonna have 1r ti constant it buttoned pin equals 2
This is on my digital pin 2, and I'm
going to have 1r constant LED pin equals 13.
I'm using an Arduino Uno. I'm going
to input just my changeable variables.
Variables; we're going to have 1r int button
state. Because of the way that the serial
port works, if I was to just have a
button that, every time it was pressed
send a value, it would constantly be
streaming to the serial port. 
So what I need to do is set a state value, so every time
that the button is released it then
sends a value, rather than just when it's
pressed.
With that, I'm also gonna need the
previous state of the button so I can
compare the two. Finally I need
my random value which at the moment is
just set to zero. 1r void set up.
Initialize the button; so we're gonna
have pin mode.1r button pin in picked. So my
button pin - which is on 2 - it's now being
set to an input. And then the exact opposite; we've
got my LED pin as an output because we
just use that for error checking if we
need it as we go through. Finally I'm
going to initialize - spelt way wrong - the
serial communication, which we do by
1r “serial. begin” and then I'm going to
use 96 thousand by out here
An important note if you're not familiar
with serial ports and Arduino, it's a
capital S at the start here. And that
ends our set up. We don't need any extra
functions here so we're just going to go
straight into 1r void loop, and in here the
first thing we're gonna do is set our
random number, so we're just gonna have
1r Rand well equals random I just want it to
be something between 0 and 1000.
And then what we need to do is,
we need to work out what the current
state of the button is. So 1r button the
state is equal to digital read read off
into button pin. So what that's gonna do
is it's gonna be a constant loop of
checking what state our button is in. And
what we need to do is check that button
state against the previous. So we're
gonna do 1r if button state does not equal
the last button state, if they're
different. So eg. if it's been pressed and
then released, it will change; we're gonna
have something happen. Now I'm gonna
leave nothing in here 
because we don't
actually want anything
1r  button state goes high. Now why do we
want nothing to happen here? because
if we had our serial print
in here; so if we sent our message to the
serial inside here, it means that every
time the button was pushed down it would
constantly update our serial with the
same random value, but we only want it to
happen once. And if we think about in max
terms, when the button is pushed down
1r state equals hi is constantly banging.
It's not until we release it - and then
the button is set to low again - that
our states will be different. So we're
gonna leave the sending of the value
until the button has been pushed down
and then released again, which is a
single bang; hence sending a single value.
So we do 1r else else is the button can only
be a higher loss or 1 or 0, so if
it's not high it must be low. And then
what we do is 1r if current state is low
send value and the sender value we're
going to do 1r serial dot print Ln. So we're
going to print a new line into the
serial which is 1r random Val and that's it
and then what we're gonna do is I'm
gonna do 1r  in case off balance; so in case
something happens with the button, I'm
gonna add a little delay of about 50ms,
just to make sure that
everything has a chance
to upload on to the serial report properly. And
then after all of that, outside of my 1r F
button state statement, we're gonna
update 1r last button state to equal the
current button state. So that means that
they will never equal each other; they'll
always be switching back and forth from
1 to 0, 0 to 1. In theory, if I verify
this, they’ll ask me to save it.
Verify, let it compile; gonna make sure
that everything's set up properly on my
board, and then I'm gonna upload it. So I
should see now if I open 1r they're doing
your serial monitor, is when I push the
button and then the button is released, we will get a randomly generated
number between 0 and 999. That's perfect.
So now I can close that; minimize Arduino,
you could actually close it. So in Max,
everything is going to come through our
serial command, and if you look at the
reference for this there's lots of
helpful stuff in here. The most important
1r and we need to send it let me bring this in.
Scale it out - is going to be the
print message, because we're gonna need
to learn which ports our devices are
still on. So if I 1r bang that, we can see
that I've got poor A twitch in my
Bluetooth incoming port 1r because my Bluetooth
1r
0:07:04.489,0:07:10.789
is switched on. And i've got port B
which is my USB modem 1 for 1 to 1 so I
1r
0:07:10.789,0:07:16.759
need to set that to B. So now our serial
port is ready to receive the messages
being sent out by Arduino. 
As everything with Max, these things won't happen
until serial receives a bang, so I'm going
to put a metro into it. Serial can both
send and receive messages from a from
the serial port by constantly buying it
1m
0:07:38.259,0:07:44.209
you will flush the the cache if you will
whereas if you were to buying a message
into it then you would upload that onto
the serial port we will look at that in
a later tutorial so now if I plug your
message into that a push right button
you'll see that we I'm not pushing the
button on our tweedle Arduino is off
looking at the serial port and I'm
getting 10 as a response out of my board
now why am I getting a number 10 so what
we're actually getting here is the raw
data so if I was to print raw just so we
can visualize in it and then push the
button we'll see that it's not just 10
that we're receiving but we're actually
receiving my string of numbers 10 just
happens to be last so that's where the
bang we receive is on our message so why
is that we're actually receiving here
thus when Arduino uploads on to the
serial port it transmits an ASCII and
Max is just reading these ASCII values
straight off the serial port so what
does this hats mean each of these digits
represents a single character where
there's a 13 and a 10 next to each other
that means that it's a termination
method message on a carriage return so
that's the end of our line on the serial
port so our actual message is contained
in the five five five two and five six
which represent different values in
ascii for example five two is going to
be four so the middle number this is a
three-digit number at the middle of
which is four how do we prove that what
we need to do is we need to form these
numbers into a list again as we put them
onto the serial port first thing we need
to do is select when we receive the end
message for our serial port which we
know is a thirteen followed by a ten and
when we do receive that 13 we can form a
group so I'm just going to do santel
group than a thousand which is an
unrealistically big sort of range so
what group would be expecting would be a
thousand characters and then how it
helped put it but if I see anything that
doesn't match thirteen or ten is our
message and then every time you receive
a bang that says thirteen terminate the
group so that means no matter how big or
small our number because we could either
have either three two or one characters
group will fill up the wire ever it
should be and then termini as it
receives the thirteen message to test
that we should be able to add a message
on to this so when I push the button
you'll see that we will receive lists of
the right amount of numbers
unfortunately I can do it your eyes well
at the moment unfortunately it's still
an ASCII list so what do we need to do
we need to turn it into a symbol first
this is something that max uses we're
going to use the ite which converts
integers into utf-8 unique
characters this is just purely something
that max understands but it's not a
usable value yet so once
what's that outfit thing for us now when
I push the button it's going to output a
symbol that is the representation of
what these ASCII codes stand apart
you're not just the speech marks as the
sign of a symbol because max doesn't
know this isn't integer J so finally
we're going to turn them from a symbol
convert our symbol into a number or
message and it can automatically tell
what this is receiving from what's
called the white space in max so from
symbol once again just for the sake of
arrow testing on will do literature from
symbol so now if I press the button on
my breadboard suddenly five three five
six five four represents our five and
eight and a six and now we've turned
that five eight and a six into an actual
integer that we can use let's see if
there's a way for me to prove my point
here you'll see that number doesn't
understand what I Toa is receiving but
it does understand what simples up
retain because it's now a number again
this is the most important part when
you're bringing date n from the serial
port from Arduino it's that conversion
from ASCII into useable data again so
how do we use it if we do something like
a slider a slider spell correctly let's
make it nice and long and here I'm going
to it's one to eat so we can scale I
know that my data and random goes from 0
to 999 and that scientist goes from zero
to one to eight by default so we plug
that in we plug that in now every time I
push my button on Arduino we're going to
generate lots of different values here
and then plug it into a scale than a
slider is just a completely sort of
visual way of representing you can do
stuff with these numbers now see we want
to send two values at the same time
if we go back trying to we know this is
really simple today so first thing we're
gonna do is add a second value for a
random value number two we're gonna
calculate our second random value and
just command T tail took four about that
so it's a bit neater and then down here
what we're gonna do is we're gonna
serial dot print and then it's important
that the newline is left until the end
here so I'm gonna have a space so
technically when I push the button it
will print random value to a space then
run the value one into the serial port
we test a code we upload that
understanding let's I'm awake so now our
serial port well print two numbers that
time separating the base space we can
close that minimize that and back into
max let's have a look at what we're
receiving it's also working the same but
if we look at the raw print in our max
console you'll see that we're now
getting five to four nine three to five
to four to five to thirteen ten
everything is still manageable here
we're just means we need to instead of
constraint or skill we now need to
unpack our message from here so we gotta
unpack we know it's two values were
receiving and as simple as that we
should now be receiving two values out
of it which still matches up with our
symbol of the ASCII code says under
integer from the symbol and the reason
unpack works is because space is a
universal sign for a split in a message
as we know from in max because you would
pass it some like 10 speed 12 space 10
space 12 and that would unpack into
twelve ten twelve so by having it at the
space and I do we know that means that
max automatically knows what to do with
it when it receives the ASCII code so
now you can start doing even more vision
things and that was just a quick look at
how to receive data on the serial port
from Arduino next time we will look at
how to send messages to Arduino on the
serial port
you
