Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Issue 1 #2

Open
wants to merge 9 commits into from
This page is out of date. Refresh to see the latest.
View
2  README.md
@@ -1,7 +1,7 @@
Arduino / TouchOSC controller of a servo
===================
-A simple project that controls the position of a servo via an on-screen rotary dial for an iOS device.
+A simple project that controls the position of two servos via on-screen rotary dials for an iOS device.
Requirements
-------
View
74 Servo_select_over_serial.pde
@@ -1,74 +0,0 @@
-// Sweep
-// by BARRAGAN <http://barraganstudio.com>
-// This example code is in the public domain.
-
-// Range appears to be 0-171, and 600-2300 micros, 1350 midpoint
-
-
-#include <Servo.h>
-
-#define LED_PIN 13
-
-Servo myservo; // create servo object to control a servo
- // a maximum of eight servo objects can be created
-
-int pos = 0; // variable to store the servo position
-
-void setup()
-{
- myservo.attach(9); // attaches the servo on pin 9 to the servo object
- Serial.begin(9600);
- digitalWrite(LED_PIN, LOW);
- //Serial.println("Enter the servo position in degrees (0 - 180), followed by a period.");
-}
-
-
-void loop()
-{
- boolean update = false;
- while(Serial.available() > 0) {
- byte temp = Serial.read();
- if(temp == '.') {
- update = true;
- break;
- } else {
- pos *= 10; // shift the currently stored position
- pos += (temp - '0'); // Add the new digit;
- }
- }
-
- if(update && pos >= 0 && pos <= 180) {
- int temp = map(pos, 0, 180, 550, 2300); // Map degrees (0-180) to Futaba servo microseconds...
- myservo.writeMicroseconds(temp);
- //Serial.print("Moving to position ");
- //Serial.println(pos, DEC);
- pos = 0;
-
- // Blink LED as a sign of movement, twice over a quarter-second
- //blinkLed(LED_PIN, 2, 250);
- }
- // Warn on invalid data
- else if(update) {
- //Serial.print("Invalid position [");
- //Serial.print(pos, DEC);
- //Serial.println("]. Please enter a position between 0 and 180 degrees");
- pos = 0;
- }
-}
-
-void blinkLed(int pin, int times, int duration) {
- // The length of being on or off (total duration devided by the number of blinks + spaces in between)
- int pulseDuration = duration / ((times * 2) - 1);
-
- for(int i = 0; i < times; i++) {
- digitalWrite(pin, HIGH);
- delay(pulseDuration);
- digitalWrite(pin, LOW);
- // if its not the last blink...
- if(i != times - 1) {
- delay(pulseDuration);
- }
- }
-
-}
-
View
84 Servo_select_over_serial/Servo_select_over_serial.pde
@@ -0,0 +1,84 @@
+// Sweep
+// by BARRAGAN <http://barraganstudio.com>
+// This example code is in the public domain.
+
+// Range appears to be 0-171, and 600-2300 micros, 1350 midpoint
+
+
+#include <Servo.h>
+
+#define LED_PIN 13
+#define SERVO_A_PIN 9
+#define SERVO_B_PIN 10
+
+#define STATE_WAITING 0 // waiting for input
+#define STATE_READ_A 1 // currently reading the value for servo a
+#define STATE_READ_B 2 // currently reading the value for servo b
+#define STATE_UPDATE_A 3 // done reading value for servo a, ready to update
+#define STATE_UPDATE_B 4 // done reading value for servo b, ready to update
+
+Servo myservoA; // create servo object to control a servo
+ // a maximum of eight servo objects can be created
+Servo myservoB;
+
+int state = STATE_WAITING;
+int posA = 0; // variable to store the servo position
+int posB = 0; // variable to store the servo position
+
+void setup()
+{
+ myservoA.attach(SERVO_A_PIN); // attaches the servo on pin 9 to the servo object
+ myservoB.attach(SERVO_B_PIN); // attaches the servo on pin 10 to the servo object
+ Serial.begin(9600);
+ digitalWrite(LED_PIN, LOW);
+ //Serial.println("Enter the servo position in degrees (0 - 180), followed by a period.");
+}
+
+
+void loop()
+{
+ while(Serial.available() > 0) {
+ byte temp = Serial.read();
+ // 'a' signifies the start of data for servo A
+ if(temp == 'a') {
+ state = STATE_READ_A;
+ // 'b' signifies the start of data for servo A
+ } else if(temp == 'b') {
+ state = STATE_READ_B;
+ } else if(temp == '.') {
+ // transistion state to update whichever value we've been reading
+ state = state == STATE_READ_A ? STATE_UPDATE_A : STATE_UPDATE_B;
+ break;
+ } else if(state == STATE_READ_A) {
+ posA *= 10; // shift the currently stored position
+ posA += (temp - '0'); // Add the new digit;
+ } else if(state == STATE_READ_B) {
+ posB *= 10; // shift the currently stored position
+ posB += (temp - '0'); // Add the new digit;
+ } else {
+ // TODO: Warn about reading data outside of a/b context
+ }
+ }
+
+ if(state == STATE_UPDATE_A && posA >= 0 && posA <= 180) {
+ updateServo(myservoA, posA);
+ posA = 0;
+ state = STATE_WAITING;
+
+ // Blink LED as a sign of movement, twice over a quarter-second
+ //blinkLed(LED_PIN, 2, 250);
+ }
+ else if(state == STATE_UPDATE_B && posB >= 0 && posB <= 180) {
+ updateServo(myservoB, posB);
+ posB = 0;
+ state = STATE_WAITING;
+ }
+ // TODO: Warn about invalid position data
+
+
+}
+
+void updateServo(Servo servo, int pos) {
+ int temp = map(pos, 0, 180, 550, 2300); // Map degrees (0-180) to Futaba servo microseconds...
+ servo.writeMicroseconds(temp);
+}
View
47 arduino_touchosc_controller.pde
@@ -1,47 +0,0 @@
-import oscP5.*; // Load OSC P5 library
-import netP5.*; // Load net P5 library
-import processing.serial.*; // Load serial library
-
-Serial arduinoPort; // Set arduinoPort as serial connection
-OscP5 oscP5; // Set oscP5 as OSC connection
-
-int redLED = 0; // redLED lets us know if the LED is on or off
-int [] led = new int [2]; // Array allows us to add more toggle buttons in TouchOSC
-int currentPos = 0;
-int desiredPos = 0;
-
-void setup() {
- size(100,100); // Processing screen size
- noStroke(); // We don’t want an outline or Stroke on our graphics
- oscP5 = new OscP5(this,8000); // Start oscP5, listening for incoming messages at port 8000
- arduinoPort = new Serial(this, Serial.list()[0], 9600); // Set arduino to 9600 baud
-}
-
-void oscEvent(OscMessage theOscMessage) { // This runs whenever there is a new OSC message
-
- String addr = theOscMessage.addrPattern(); // Creates a string out of the OSC message
- if(addr.indexOf("/1/servo") !=-1){ // Filters out any toggle buttons
- //int i = int((addr.charAt(9) )) - 0x30; // returns the ASCII number so convert into a real number by subtracting 0x30
- desiredPos = int(theOscMessage.get(0).floatValue()); // Puts button value into led[i]
- // Button values can be read by using led[0], led[1], led[2], etc.
-
- }
-}
-
-void draw() {
- background(50); // Sets the background to a dark grey, can be 0-255
-
- if(currentPos != desiredPos) {
- String pos = str(desiredPos);
- println("Moving to position " + pos);
- arduinoPort.write(pos);
- arduinoPort.write('.');
- currentPos = desiredPos;
- }
-
- redLED = int(map(desiredPos, 0, 180, 0, 255));
- fill(redLED,0,0); // Fill rectangle with redLED amount
- ellipse(50, 50, 50, 50); // Created an ellipse at 50 pixels from the left...
- // 50 pixels from the top and a width of 50 and height of 50 pixels
-}
-
View
85 arduino_touchosc_controller/arduino_touchosc_controller.pde
@@ -0,0 +1,85 @@
+import oscP5.*; // Load OSC P5 library
+import netP5.*; // Load net P5 library
+import processing.serial.*; // Load serial library
+
+Serial arduinoPort; // Set arduinoPort as serial connection
+OscP5 oscP5; // Set oscP5 as OSC connection
+NetAddressList myNetAddressList = new NetAddressList();
+int outgoingPort = 9000;
+int incomingPort = 8000;
+
+int redLED = 0; // redLED lets us know if the LED is on or off
+int greenLED = 0; // greenLED lets us know if the LED is on or off
+int [] led = new int [2]; // Array allows us to add more toggle buttons in TouchOSC
+int currentPosA = 0;
+int desiredPosA = 0;
+int currentPosB = 0;
+int desiredPosB = 0;
+
+void setup() {
+ size(200,100); // Processing screen size
+ noStroke(); // We don’t want an outline or Stroke on our graphics
+ oscP5 = new OscP5(this,incomingPort); // Start oscP5, listening for incoming messages at port 8000
+ arduinoPort = new Serial(this, Serial.list()[0], 9600); // Set arduino to 9600 baud
+}
+
+void oscEvent(OscMessage theOscMessage) { // This runs whenever there is a new OSC message
+
+ connect(theOscMessage.netAddress().address());
+
+ String addr = theOscMessage.addrPattern(); // Creates a string out of the OSC message
+ if(addr.indexOf("/1/servoA") !=-1){ // Filters out any toggle buttons
+ //int i = int((addr.charAt(9) )) - 0x30; // returns the ASCII number so convert into a real number by subtracting 0x30
+ desiredPosA = int(theOscMessage.get(0).floatValue()); // Puts button value into led[i]
+ // Button values can be read by using led[0], led[1], led[2], etc.
+ }
+ else if(addr.indexOf("/1/servoB") !=-1){ // Filters out any toggle buttons
+ //int i = int((addr.charAt(9) )) - 0x30; // returns the ASCII number so convert into a real number by subtracting 0x30
+ desiredPosB = int(theOscMessage.get(0).floatValue()); // Puts button value into led[i]
+ // Button values can be read by using led[0], led[1], led[2], etc.
+ }
+}
+
+void connect(String theIPaddress) {
+ if (!myNetAddressList.contains(theIPaddress, outgoingPort)) {
+ myNetAddressList.add(new NetAddress(theIPaddress, outgoingPort));
+ }
+}
+
+
+void draw() {
+ background(50); // Sets the background to a dark grey, can be 0-255
+
+ if(currentPosA != desiredPosA) {
+ String pos = str(desiredPosA);
+ println("Moving to position A " + pos);
+ arduinoPort.write('a');
+ arduinoPort.write(pos);
+ arduinoPort.write('.');
+ currentPosA = desiredPosA;
+
+ redLED = int(map(desiredPosA, 0, 180, 0, 255));
+ oscP5.send(new OscMessage("/1/ledA", new Object[]{redLED}), myNetAddressList);
+ }
+
+ if(currentPosB != desiredPosB) {
+ String pos = str(desiredPosB);
+ println("Moving to position B " + pos);
+ arduinoPort.write('b');
+ arduinoPort.write(pos);
+ arduinoPort.write('.');
+ currentPosB = desiredPosB;
+
+ greenLED = int(map(desiredPosB, 0, 180, 0, 255));
+ oscP5.send(new OscMessage("/1/ledB", new Object[]{greenLED}), myNetAddressList);
+ }
+
+ fill(redLED,0,0); // Fill rectangle with redLED amount
+ ellipse(50, 50, 50, 50); // Created an ellipse at 50 pixels from the left...
+ // 50 pixels from the top and a width of 50 and height of 50 pixels
+
+ fill(0,greenLED,0); // Fill rectangle with redLED amount
+ ellipse(150, 50, 50, 50); // Created an ellipse at 50 pixels from the left...
+ // 50 pixels from the top and a width of 50 and height of 50 pixels
+}
+
View
BIN  arduino_touchosc_layout.touchosc
Binary file not shown
View
BIN  arduino_touchosc_layout_ipad.touchosc
Binary file not shown
Something went wrong with that request. Please try again.