Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Use the Arduino Servo lib to make servos work better

  • Loading branch information...
commit 8ce13b6e7885562ad47798ea9e1bd7548d502f0b 1 parent df69fdc
@vickash vickash authored
View
6 CHANGELOG.md
@@ -1,5 +1,11 @@
# Changelog
+## 0.11.2
+
+* Make servos work better by using the existing Arduino Servo library.
+ * Up to 12 servos can be controlled, on digital pins 2-13 only.
+ * Flashing the updated sketch to the board is required.
+
## 0.11.1
### New Features
View
7 examples/servo/servo.rb
@@ -7,7 +7,10 @@
board = Dino::Board.new(Dino::TxRx::Serial.new)
servo = Dino::Components::Servo.new(pin: 9, board: board)
-loop do
- servo.position += 9
+# Add different angles (in degrees) to the array below to try out your servo.
+# Note: Some servos may not have a full 180 degree sweep.
+
+[0, 90].cycle do |angle|
+ servo.position = angle
sleep 0.5
end
View
16 lib/dino/board.rb
@@ -77,13 +77,15 @@ def set_pullup(pin, pullup)
end
PIN_COMMANDS = {
- digital_write: 01,
- digital_read: 02,
- analog_write: 03,
- analog_read: 04,
- digital_listen: 05,
- analog_listen: 06,
- stop_listener: 07,
+ digital_write: '01',
+ digital_read: '02',
+ analog_write: '03',
+ analog_read: '04',
+ digital_listen: '05',
+ analog_listen: '06',
+ stop_listener: '07',
+ servo_toggle: '08',
+ servo_write: '09'
}
PIN_COMMANDS.each_key do |command|
View
14 lib/dino/components/servo.rb
@@ -5,14 +5,18 @@ class Servo < BaseComponent
def after_initialize(options={})
set_pin_mode(:out)
- self.position = 0
+ board.servo_toggle(pin, 1)
+ position = options[:position] || 0
end
- def position=(new_position)
- @position = new_position % 180
- analog_write(@position)
+ def position=(value)
+ board.servo_write(pin, angle(value))
+ @position = value
+ end
+
+ def angle(value)
+ value == 180 ? value : value % 180
end
end
end
end
-
View
2  lib/dino/version.rb
@@ -1,3 +1,3 @@
module Dino
- VERSION = "0.11.1"
+ VERSION = "0.11.2"
end
View
1  src/du/du.ino
@@ -1,4 +1,5 @@
#include "Dino.h"
+#include <Servo.h>
Dino dino;
// Dino.h doesn't handle TXRX. Setup a function to tell it to write to Serial.
View
1  src/du_ethernet/du_ethernet.ino
@@ -1,6 +1,7 @@
#include "Dino.h"
#include <SPI.h>
#include <Ethernet.h>
+#include <Servo.h>
// Configure your MAC address, IP address, and HTTP port here.
byte mac[] = { 0xDE, 0xAD, 0xBE, 0x30, 0x31, 0x32 };
View
28 src/lib/Dino.cpp
@@ -43,6 +43,8 @@ void Dino::process() {
case 5: addDigitalListener (); break;
case 6: addAnalogListener (); break;
case 7: removeListener (); break;
+ case 8: servoToggle (); break;
+ case 9: servoWrite (); break;
case 90: reset (); break;
case 97: setAnalogDivider (); break;
case 98: setHeartRate (); break;
@@ -192,6 +194,32 @@ void Dino::removeListener() {
#endif
}
+// CMD = 08
+// Attach the servo object to pin or detach it.
+void Dino::servoToggle() {
+ if (val == 0) {
+ #ifdef debug
+ Serial.print("Detaching servo"); Serial.print(" on pin "); Serial.println(pin);
+ #endif
+ servos[pin - 2].detach();
+ }
+ else {
+ #ifdef debug
+ Serial.print("Attaching servo"); Serial.print(" on pin "); Serial.println(pin);
+ #endif
+ servos[pin - 2].attach(pin);
+ }
+}
+
+// CMD = 09
+// Write a value to the servo object.
+void Dino::servoWrite() {
+ #ifdef debug
+ Serial.print("Servo write "); Serial.print(val); Serial.print(" to pin "); Serial.println(pin);
+ #endif
+ servos[pin - 2].write(val);
+}
+
// CMD = 90
void Dino::reset() {
heartRate = 4000; // Default heartRate is ~4ms.
View
6 src/lib/Dino.h
@@ -4,7 +4,9 @@
#ifndef Dino_h
#define Dino_h
+
#include "Arduino.h"
+#include <Servo.h>
// Allocate listener storage based on what board we're running.
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
@@ -55,6 +57,8 @@ class Dino {
void (*_writeCallback)(char *str);
void writeResponse();
+ Servo servos[12];
+
// API-accessible functions.
void setMode ();
void dWrite ();
@@ -64,6 +68,8 @@ class Dino {
void addDigitalListener ();
void addAnalogListener ();
void removeListener ();
+ void servoToggle ();
+ void servoWrite ();
void reset ();
void setAnalogDivider ();
void setHeartRate ();
Please sign in to comment.
Something went wrong with that request. Please try again.