send Pot & Button as OSC Messages

Sketch

[code lang=”arduino”]/*
little example sketch for ArdOSC, pot, button & led
based on ArdOSC example sketches.
download ArdOSC Library: https://github.com/recotana/ArdOSC

arduino converts pot & button values to osc messages.
the led is mapped to the button state & its osc message.

______HOST SETTINGS______
Pot Path = /1/Pot1
Button Path = /1/button1
LED Path = /1/led1 (helpfull for debuging)

Connection setup:
Host = myIp
Host Port (outgoing) = serverport
Host Port (incoming) = destPort
_________________________
created by Herr RIP Paule
more infos: http://www.irieelectronics.iriemovement.de

*/
#include <SPI.h>
#include <Ethernet.h>

#include <ArdOSC.h>

// pot section
int PotValue = 0;
int lastPotValue = 0;
int pin0 = 0; // define variable for reading of pin0

// button section
int pushButton = 2 ;
int pushButton_old = 0;

// led section
int ledPin = 9;
int led = 0;
byte myMac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte myIp[] = { 192, 168, 0, 177 };
int serverPort = 8000;
int destPort = 10000; // set port for outgoing messages over udp
byte destIp[] = { 192, 168, 0, 4 };

OSCServer server;
OSCClient client;

OSCMessage global_mes; // create new osc message

void setup(){

Serial.begin(19200);
Ethernet.begin(myMac ,myIp);
server.begin(serverPort);

//set callback function & oscaddress
server.addCallback("/1/button1",&ledv);

pinMode(ledPin, OUTPUT);
pinMode(pushButton, INPUT);
}

void loop(){

if(server.aviableCheck()>0){
// Serial.println("alive! ");
}

sendPot();
sendButton();

}

void sendPot() {

PotValue = (analogRead(0))/8; // set PotValue to pin0 & pot value from 0 – 10253 to midi value range of 0 – 127

if ( PotValue != lastPotValue ){

global_mes.setAddress(destIp,destPort);

global_mes.beginMessage("/1/rotary1"); // OSC Path
global_mes.addArgInt32(PotValue);

client.send(&global_mes);

global_mes.flush(); // object data clear

delay(1);

lastPotValue = PotValue; // store old state as pot Value
}
}

void sendButton() {

int buttonState = digitalRead(pushButton);

if (buttonState != pushButton_old){

global_mes.setAddress(destIp,destPort);

global_mes.beginMessage("/1/button1"); // OSC Path
global_mes.addArgFloat(buttonState);

client.send(&global_mes);

global_mes.flush(); // object data clear

delay(1);
digitalWrite (ledPin, buttonState);

pushButton_old = buttonState; // store old state as button state
}
}

void eBook ledv(OSCMessage *_mes){
int led = _mes->getArgFloat(0);

//create new osc message
OSCMessage newMes;

//set destination ip address & port no
newMes.setAddress(_mes->getIpAddress(),destPort);
newMes.beginMessage("/1/led1");

if(led==0){
led=0;
digitalWrite(ledPin, LOW);
}
else{
led=1;
digitalWrite(ledPin, HIGH);
}
newMes.addArgInt32(led);

//send osc message
client.send(&newMes);

}[/code]

Download: ArdOSC send POT/BUTTON Sketch