Modifying Arduino2Max to output from Max to Arduino

Understanding the Arduino2Max program Arduino2Max is a simple and lightweight way to get data from Arduino into Max. But it doesn’t have built into it the ability to send data the other way, from Max to Arduino so Max can control things connected to the Arduino. Here we’ll walk through modifying Arduino2Max to allow it to do so. The standard Arduino2Max Arduino sketch works as follows: Setup runs once when the program is first started

1. Opens a serial communication port 2. Blinks led on pin 13 to show that it’s running 3. Pin 13 was just used as an output. Now it needs to be set to be an input.

Loop (repeats these steps over and over until the Arduino loses power) Read the comments in the code as well as my comments below.

1. Listens on the port for an incoming letter “r” from Max. (in the Arduino2Max Max patch, inside of the [p arduinoSerialreader] subpatcher, you can see that the [metro] bangs an (r) message that gets sent to the serial port. This message travels through the serial port to the Arduino. When the Arduino receives an “r” then it... 1

This makes it so the Arduino reads pins 2 through 11. Since those pins have been declared as outputs. I need to make sure that down in the loop() routine I’m not trying to read the values. our new setup() routine is identical to the original one except for the pinMode command setting pins 12 and 13 as outputs.2. Say we want to turn something on and off using digital pins 12 and 13. in the setup() routine we need to declare that those pins will be set to output. but doesn’t read 12 or 13. We can have it listen for other incoming messages and have it do things in response to them. Reads analog pins 0 through 5 and sends out the values it reads with a space in between each one 3. Any output pins have to be set in setup(). In the pink rectangle in the image of the code below you can see that I have changed the second number from 13 to 11. So. 2 . First. Reads digital pins 2 through 13 and sends out the values it reads with a space in between each one Modifying the Arduino sketch to do other things In part 1 of the Loop routine it listens for the incoming “r” message from Max and then when it receives it. it executes all the rest of the stuff in the routine.

At the very end of the program we will define those routines to tell the Arduino what instructions to execute if it receives those specified numbers.We can listen for specific numbers and if those numbers are received have the Arduino execute routines that we define farther down in the sketch. But the second part is new. If it receives a 1 then it will execute a routine we create called oneReceived(). Here we listen for specific numbers. If the number received is a 0 then Arduino will execute a routine that we’ll create and call zeroReceived(). Above. the first part is identical to the previous version of the loop() routine other than changing the range of pins read (from 2 -13 to 2 -11). 3 . etc.

but for the most part. separated by magenta lines. if Max sends Arduino a number 1 then the Arduino will execute the oneReceived() routine. So. but don’t worry about that for now. In the Arduino2Max patch. In the zeroReceived() routine the instruction is to write pin 12 low (set the voltage on pin 12 to 0). Sending the Messages from Max to Arduino To send the messages from Max to Arduino. I have made a [receive] object called digitalOut that will receive these messages and send them out the serial port to the Arduino. 4 . In oneReceived() the instruction is to set pin 12 high (to set it to +5 volts). Double click on it and you can see what’s inside of it. we’ll probably just be turning things on and off.Here are all of the new routines. If we attach a [receive] (shortened as [r] ) object to [serial] then we can send it messages from anywhere in the main patcher. the [serial] object is inside of the [p arduinoSerialreader] object. which sets pin 12 high.” In each of the routines above you see a single line of instructions to execute. I can program much more complex routines for Arduino to execute when it receives one of the specified numbers. We might get into analog writing with pulse-width modulation and servo control later. we just need to send the message to the [serial] object. In the middle of it is the [serial] object. Each routine starts with void nameOfRoutine () { In the parentheses could go a variable. The opening curly bracket says “from this point until the closing curly bracket are the instructions executed whenever this routine is called.

This allows us to use toggles to turn things on and off with the Arduino. anywhere we want to send a message to Arduino. I would send a number 2 to the [s digitalOut]. So if I want to turn on something connected to pin 13. Looking back at the Arduino sketch. In the accompanying patch. Then I could use the third toggle connected to the [p digitalOuts] subpatch to control that pin. I have made a subpatch called [p digitalOuts] that just does some simple math so I can use toggles to send out the specified numbers. we just attach a [send digitalOut] (or [s digitalOut] ). The next one adds 4 so it puts out a 4 or 5. send out 0 or 1. To turn that thing off. If I want to send other numbers then I can add to the 0 and 1. Inside the subpatch. a number is being added to all but the first inlet. Toggles. I just send a number 3 to a [s digitalOut] object. So. For example. The subpatch has 5 inlets that each have a toggle attached to them. 5 . which means that it puts out a 2 or 3. and so on. So. I can program the Arduino to write pin 11 high when it receives a 5 and to write it low when it receives a 4. as we know.Now. I can send a 0 or 1 with just one toggle. the second inlet receives a 0 or 1 from the connected toggle and adds 2. a message with the integer 2 sent to the Arduino sets pin 13 low and a 3 sets it high.

I can send any number between 0 and 255 to Arduino except 114 because that is the ASCII code for the letter ‘r’. which is the letter Arduino is listening for in the loop() routine. 6 .