Sections
You are here: Home Documentation Tutorials Processing Read the Analog Inputs

Read the Analog Inputs

We'll continue with another example - this time, how to read values back from the board.

To read an analog input, 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 - this is what distinguishes a read message from a write message, the lack of an argument.

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. The code below creates two circles and modifies their size based on the value coming from the trimpot. Copy and paste the code below into Processing - you're ready to run your program.


Analog Ins
The size of the circles in the sketch is controlled by the trimpot on the Application Board.


// Make Controller Kit - Analog In
// Send out 'read' messages every frame, and then grab the message coming back from the board.
// based on oscP5message by andreas schlegel
import oscP5.*;
import netP5.*;
OscP5 oscP5;
NetAddress makeControllerAddress;
OscMessage myMessage;
int trimpot;

void setup()
{
size(400,400);
frameRate(25);
//start oscP5, listening for incoming messages at port 10000
oscP5 = new OscP5(this, 10000);
makeControllerAddress = new NetAddress( "192.168.0.200", 10000 );
myMessage = new OscMessage("/analogin/7/value");
trimpot = 0;
}

void draw()
{
background(25);
oscP5.send(myMessage, makeControllerAddress); // send the message
// use the trimpot value to size the circles
fill( 240 );
ellipse( 100, 100, trimpot/2, trimpot/2 );
fill( 150 );
ellipse( 250, 250, trimpot/4, trimpot/4 );
}

// this gets called back whenever there's a new OSC message coming back from the board.
void oscEvent(OscMessage theOscMessage)
{ // compare the incoming address to the address we asked for.
if( theOscMessage.addrpattern().equals( "/analogin/7/value" ) )
trimpot = theOscMessage.get(0).intValue();
}

By placing the call to oscP5.send( ) in draw( ), we're asking for an update from the board at the framerate of the sketch - 24 times a second in this case, which we specified in setup( ).

oscEvent( ) gets called when there's an incoming OSC packet - there are a number of methods provided to read the different parameters of the incoming OSC message, but we only care about whether the address matches the address we sent out. So we check the addrpattern( ) of the incoming OscMessage and test to see if it matches myMessage, which is reading analog in 7, the trimpot. Confirm the trimpot jumper is on, and you should be able to adjust the size of the circles onscreen by turning the trimpot.

 
Log in


Forgot your password?
New user?