Skip to content

Instantly share code, notes, and snippets.

@rioleo
Created October 20, 2012 20:32
Show Gist options
  • Save rioleo/3924701 to your computer and use it in GitHub Desktop.
Save rioleo/3924701 to your computer and use it in GitHub Desktop.
ArduinoForClient
#include <Servo.h>
byte byteRead;
long num1, num2,answer;
boolean mySwitch=false;
Servo servoTilt, servoPan;
void setup() {
Serial.begin(9600);
num1=0;
num2=0;
servoTilt.attach(2); //The Tilt servo is attached to pin 2.
servoPan.attach(3); //The Pan servo is attached to pin 3.
servoTilt.write(90); //Initially put the servos both
servoPan.write(90); //at 90 degress.
}
void loop() {
while (Serial.available()) {
byteRead = Serial.read();
if(byteRead>47 && byteRead<58){
if(!mySwitch){
num1=(num1*10)+(byteRead-48);
}else{
num2=(num2*10)+(byteRead-48);
}
}
/*Listen for an equal sign (byte code 61)
to calculate the answer and send it back to the
serial monitor screen*/
if(byteRead==61){
servoTilt.write(num1);
servoPan.write(num2);
num1=0;
num2=0;
mySwitch=false;
/* Listen for the addition sign (byte code 43). This is
used as a delimiter to help define num1 from num2 */
}else if (byteRead==43){
mySwitch=true;
}
}
}
/**
* oscP5broadcastClient by andreas schlegel
* an osc broadcast client.
* an example for broadcast server is located in the oscP5broadcaster exmaple.
* oscP5 website at http://www.sojamo.de/oscP5
* FINAL FINAL FINAL
*/
import oscP5.*;
import netP5.*;
import processing.serial.*;
Serial myPort;
OscP5 oscP5;
/* a NetAddress contains the ip address and port number of a remote location in the network. */
NetAddress myBroadcastLocation;
void setup() {
size(400,400);
frameRate(25);
myPort = new Serial(this, Serial.list()[0], 9600);
/* create a new instance of oscP5.
* 12000 is the port number you are listening for incoming osc messages.
*/
oscP5 = new OscP5(this,57110);
/* create a new NetAddress. a NetAddress is used when sending osc messages
* with the oscP5.send method.
*/
/* the address of the osc broadcast server */
myBroadcastLocation = new NetAddress("192.168.33.21",32000);
}
void draw() {
background(0);
}
void mousePressed() {
/* create a new OscMessage with an address pattern, in this case /test. */
OscMessage myOscMessage = new OscMessage("/test");
/* add a value (an integer) to the OscMessage */
myOscMessage.add(100);
/* send the OscMessage to a remote location specified in myNetAddress */
oscP5.send(myOscMessage, myBroadcastLocation);
}
void keyPressed() {
OscMessage m;
switch(key) {
case('c'):
/* connect to the broadcaster */
m = new OscMessage("/server/connect",new Object[0]);
oscP5.flush(m,myBroadcastLocation);
break;
case('d'):
/* disconnect from the broadcaster */
m = new OscMessage("/server/disconnect",new Object[0]);
oscP5.flush(m,myBroadcastLocation);
break;
}
}
/* incoming osc message are forwarded to the oscEvent method. */
void oscEvent(OscMessage theOscMessage) {
/* get and print the address pattern and the typetag of the received OscMessage */
//println("### received an osc message with addrpattern "+theOscMessage.addrPattern()+" and typetag "+theOscMessage.typetag());
//theOscMessage.print();
String pan = theOscMessage.get(0).stringValue();
//int tilt = theOscMessage.get(1).intValue();
myPort.write(pan);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment