Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Controls a servo from an iOS device.
- Loading branch information
0 parents
commit f834c0c
Showing
5 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
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. | ||
|
||
Requirements | ||
------- | ||
To use this project you'll need the following: | ||
* [TouchOSC](http://hexler.net/software/touchosc) on your iOS device and also the layout editor on your computer. | ||
* [Processing](http://processing.org/), A programming environment for compiling and running the intermediary OSC --> Serial controller app. | ||
* [Arduino](http://arduino.cc), A development environment for the Arduino microprocessor, you'll use this to install the serial --> servo sketch on your Arduino. | ||
* A standard servo connected to pin 9 of your Arduino. | ||
|
||
Installation | ||
-------- | ||
See [this video for an overview of the moving parts](http://www.youtube.com/watch?v=cA6ol-jXW4U) and [this SparkFun tutorial](http://www.sparkfun.com/tutorials/152), more details might be forthcoming. | ||
|
||
References | ||
--------- | ||
* Heavily based on this Sparkfun tutorial: http://www.sparkfun.com/tutorials/152 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// 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); | ||
} | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
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 | ||
} | ||
|
Binary file not shown.
Binary file not shown.