Sunteți pe pagina 1din 6

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 doesnt 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 well 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 its 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

2. Reads analog pins 0 through 5 and sends out the values it reads with a space in between each one 3. 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. We can have it listen for other incoming messages and have it do things in response to them. Say we want to turn something on and off using digital pins 12 and 13. First, in the setup() routine we need to declare that those pins will be set to output. Any output pins have to be set in setup(). So, our new setup() routine is identical to the original one except for the pinMode command setting pins 12 and 13 as outputs.

Since those pins have been declared as outputs, I need to make sure that down in the loop() routine Im not trying to read the values. 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. This makes it so the Arduino reads pins 2 through 11, but doesnt read 12 or 13.

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.

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). But the second part is new. Here we listen for specific numbers. If the number received is a 0 then Arduino will execute a routine that well create and call zeroReceived(). If it receives a 1 then it will execute a routine we create called oneReceived(), etc. 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.

Here are all of the new routines, separated by magenta lines. Each routine starts with void nameOfRoutine () { In the parentheses could go a variable, but dont worry about that for now. The opening curly bracket says from this point until the closing curly bracket are the instructions executed whenever this routine is called. In each of the routines above you see a single line of instructions to execute. In the zeroReceived() routine the instruction is to write pin 12 low (set the voltage on pin 12 to 0). In oneReceived() the instruction is to set pin 12 high (to set it to +5 volts). So, if Max sends Arduino a number 1 then the Arduino will execute the oneReceived() routine, which sets pin 12 high. I can program much more complex routines for Arduino to execute when it receives one of the specified numbers, but for the most part, well probably just be turning things on and off. We might get into analog writing with pulse-width modulation and servo control later.

Sending the Messages from Max to Arduino To send the messages from Max to Arduino, we just need to send the message to the [serial] object. In the Arduino2Max patch, the [serial] object is inside of the [p arduinoSerialreader] object. Double click on it and you can see whats inside of it. In the middle of it is the [serial] object. If we attach a [receive] (shortened as [r] ) object to [serial] then we can send it messages from anywhere in the main patcher. I have made a [receive] object called digitalOut that will receive these messages and send them out the serial port to the Arduino.

Now, anywhere we want to send a message to Arduino, we just attach a [send digitalOut] (or [s digitalOut] ). Looking back at the Arduino sketch, a message with the integer 2 sent to the Arduino sets pin 13 low and a 3 sets it high. So if I want to turn on something connected to pin 13, I just send a number 3 to a [s digitalOut] object. To turn that thing off, I would send a number 2 to the [s digitalOut].

In the accompanying patch, 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. The subpatch has 5 inlets that each have a toggle attached to them. Toggles, as we know, send out 0 or 1. So, I can send a 0 or 1 with just one toggle. 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. So, the second inlet receives a 0 or 1 from the connected toggle and adds 2, which means that it puts out a 2 or 3. The next one adds 4 so it puts out a 4 or 5, and so on. This allows us to use toggles to turn things on and off with the Arduino. For example, 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. Then I could use the third toggle connected to the [p digitalOuts] subpatch to control that pin.

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.

S-ar putea să vă placă și