You are here: Home Documentation Tutorials Max/MSP Read the Analog Inputs
Document Actions

Read the Analog Inputs

Next we'll learn how to read values back from the board.
How to use the Make Controller Kit with Cycling 74's Max/MSP.
Page 3 of 5.
There are two ways to get messages back from the board.  One is to ask the board each time you want a message.  This is fine if you're just asking for values every now and then, but if you're going to be reading messages all the time, you'll probably want to use the autosend feature.  If you want to use the manual style, jump down to that section

Autosend

First, choose whether you want the Make Controller to send messages to you over USB or Ethernet.  If you want Ethernet, send it the message
/system/autosend-udp 1
The board will send messages wherever it last received a message from.  If you want USB, send the message
/system/autosend-usb 1
Next, decide how often you want the Make Controller Kit to send you messages.  We'll have it look every 10 milliseconds, or 100 times a second.  To do this, send the message
/system/autosend-interval 10
Lastly, turn the analog inputs autosend on.  We'll turn on autosend for all 8 of the analogin channels by sending the message
/analogin/*/autosend 1
Now, anytime the value changes on one of your inputs, the Make Controller will send you a message.  Read on below to see how to deal with the incoming messages.

Manual Reads

To manually read an analog input, we need to send an OSC message to ask for the current value, and the board will send an OSC message back with the value. Unlike the OSC messages sent to the LEDs, an OSC message asking to read a value will not have an argument on the end of it.

The name of the analog input subsystem is analogin, there are 8 devices numbered 0 - 7, and they have a value. 'Read' messages have the form /subsystem/device/property, with no argument.  So, our message to read analog input 7, the trimpot, will look like
/analogin/7/value
Note there's no argument after our address, so the board will know it's a read, and not a write.  We expect the board to send back a message like
/analogin/7/value 925
so we know the value of analogin 7 is 925.


Via Ethernet

We're going to set up to read analog in 7 10 times a second, or once every 100 milliseconds.  To do this we'll use the metro object to send out our message as often as we want.
analogin_label.jpg
Poll the analog ins to read them.

Explanation
  1. Use a metro to send the 'read' messages at regular intervals - adjust the interval to read more or less frequently.
  2. The message /analogin/7/value means, "send me back the value at analog in 7" - note there is no argument.
  3. udpsend sends this message onto your network. Make sure the address matches the address of your board.
  4. udpreceive receives the message back from the board off the network. The value of the third argument, MakeCtrl, is arbitrary but a third argument must be included to get full OSC messages through UDP.
  5. OpenSoundControl creates a Max message from the OSC message in the incoming UDP packet.
  6. osc-route extracts messages that match its argument from the stream of messages coming back from the board. In this instance, we're listening for messages coming from /analogin/7/value, since that's what we asked for.
  7. Use the value for whatever you like. The values coming back from the analog ins will be from 0 - 1023.


Via USB

The approach via USB is essentially the same, but for the purposes of this example, we're going to use a slightly different strategy.  Say we have several different sensors connected, and we want to read all of them.  It gets pretty inefficient to send separate messages for each of them, so we'll use one of the nice features of OSC, and instead of asking to read just analog in 7, we'll ask to read all the analog ins, by using the *, or wildcard, character as the device number in our message.  So now we'll be sending the message
/analogin/*/value
which means, "Tell me the values for all the analog ins you have".

We'll also use this opportunity to go a little further with the way you can route the incoming OSC messages.  The osc-route object can listen for a particular pattern, or part of a pattern, and then pass on messages if they match it.  In our case, we're expecting to see several different /analogin messages come back - from each of the analog ins, in fact, since we sent a message asking for all of them. 

We can grab all messages that start with /analogin by placing down an osc-route object with /analogin as its argument.  osc-route will chop the /analogin part of the message off and send the rest of it on, so we'll look for messages from the specific analog ins after that.
mc.usb - analogin
Explanation
  1. Use a metro to send the 'read' messages at regular intervals - adjust the interval to read more or less frequently.
  2. The message /analogin/*/value means, "Tell me the values for all the analog ins you have" - note there is still no argument.
  3. mc.usb sends this message onto your network, and listens for messages back from the board.
  4. osc-route looks for all the messages that start with /analogin
  5. Then we use another osc-route to look for the values from analog in 0, 1, 3 and 7.  The remaining part of the address, after the /analogin part gets chopped off, will look like /1/value, so that's what we try to match against.  Change these around to suit your application, of course.