Permalink
Browse files

Adds servo control

  • Loading branch information...
1 parent 6b1ad0b commit a8a642d3ef95829fe88bc44f1ada0aeaa8ee65e4 Brian Stanwyck committed Dec 16, 2011
Showing with 57 additions and 20 deletions.
  1. +20 −7 poll_server.rb
  2. +16 −9 server.rb
  3. +21 −4 string_input/string_input.ino
View
27 poll_server.rb
@@ -5,7 +5,7 @@
class Arduino
# Params for serial port
- PORT = "/dev/tty.usbmodemfa131"
+ PORT = "/dev/tty.usbmodemfd121"
DATA_BITS = 8
STOP_BITS = 1
BAUD_RATE = 9600
@@ -16,7 +16,7 @@ def initialize
end
def write_to_pin(number, high = true)
- @sp.write(("A".ord + number - 1).chr + (high ? ?1 : ?0 ) + "\r\n")
+ @sp.write(?P + ("A".ord + number - 1).chr + (high ? ?1 : ?0 ) + "\r\n")
end
def led_on
@@ -26,22 +26,35 @@ def led_on
def led_off
write_to_pin(13, false)
end
+
+ def turn_servo(angle)
+ sign = angle < 0 ? ?- : ?+
+ turn_char = angle.abs
+ @sp.write(?S + turn_char.chr + sign + "\r\n")
+ end
end
arduino = Arduino.new
uri = URI.parse("http://ec2-50-19-60-252.compute-1.amazonaws.com/query.json")
-current_state = {:led => false}
+current_state = {:led => false, :servo => 0}
while true do
response = Net::HTTP.get_response(uri)
result = JSON.parse(response.body)
- if result && result["led"] != current_state[:led]
- current_state[:led] = result["led"]
- puts "Change detected! LED is now #{current_state[:led] ? "on" : "off"}"
+ if result
+ if result["led"] != current_state[:led]
+ current_state[:led] = result["led"]
+ puts "Change detected! LED is now #{current_state[:led] ? "on" : "off"}"
+
+ current_state[:led] ? arduino.led_on : arduino.led_off
+ end
- current_state[:led] ? arduino.led_on : arduino.led_off
+ if result["servo"] != current_state[:servo]
+ current_state[:servo] = result["servo"]
+ arduino.turn_servo(current_state[:servo])
+ end
end
sleep 2
View
25 server.rb
@@ -3,34 +3,41 @@
require 'json'
require 'haml'
-params = {}
+query = {}
-get '/led' do
- haml :led
+get '/' do
+ haml :root
end
post '/led/start' do
- params.merge! :led => true
+ query.merge! :led => true
redirect '/led'
end
post '/led/stop' do
- params.merge! :led => false
+ query.merge! :led => false
redirect '/led'
end
get '/query.json' do
content_type :json
- params.to_json
+ query.to_json
+end
+
+post '/servo' do
+ query.merge! :servo_angle => params[:angle]
end
__END__
-@@ led
+@@ root
%html
%head
- %title Control LED
+ %title Control Arduino
%body
%form(action='/led/start' method='POST')
%input(type='submit' value='Turn on')
%form(action='/led/stop' method='POST')
- %input(type='submit' value='Turn off')
+ %input(type='submit' value='Turn off')
+ %form(action='/servo' method='POST')
+ %input(type='text' name='angle')
+ %input(type='submit' value='Turn servo (-180-180)')
View
25 string_input/string_input.ino
@@ -1,10 +1,14 @@
+#include <Servo.h>
+
int i = 0;
char curString[255];
int curInIndex = 0;
+int sign = 1;
char stringEnded = false;
int pin;
int level;
+Servo myservo;
void readInput() {
byte b;
@@ -30,18 +34,31 @@ void readInput() {
}
void setup() {
+ myservo.attach(9);
Serial.begin(9600);
pinMode(13, OUTPUT);
+ pinMode(12, OUTPUT);
for(i=0; i < 255; i++) curString[i] = 0;
}
void loop() {
readInput();
if (stringEnded) {
- pin = curString[0]-64;
- level = curString[1] == '1' ? HIGH : LOW;
- Serial.println("Got a signal!");
- digitalWrite(pin, level);
+ switch(curString[0]) {
+ case 'P':
+ pin = curString[1]-64;
+ level = curString[2] == '1' ? HIGH : LOW;
+ Serial.println("Got pin change signal");
+ digitalWrite(pin, level);
+ break;
+ case 'S':
+ sign = curString[2] == '-' ? -1 : 1;
+ Serial.print("Got servo rotate signal ");
+ Serial.println((int) (sign * curString[1]));
+ myservo.write((int) (sign * curString[1])); // sets the servo position according to the scaled value
+ delay(15); // waits for the servo to get there
+ break;
+ }
}
}

0 comments on commit a8a642d

Please sign in to comment.