Announcements
A new show presented as a part of the Theater Arts and Digital Art New Media Program at UCSC
Two weekends:
Feb. 26-27-28 and March 4-5-6-7
Thu-Fri-Sat at 7:00 pm & 9:00 pm
Sundays at 3:00 pm only
Servos!
Controlling One Servo With A Trim Pot
Original Arduino Tutorial
// Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott
// Modified by Khazar, 11/2008
#include <Servo.h> // Include the Servo Library.
// Libraries are kept in the "hardware/libraries" subdirector of the
// Arduino application directory.
Servo myservo; // Create servo object to control a servo
int potpin = 0; // Analog pin used to connect the potentiometer
int val; // Variable to read the value from the analog pin
int servopin = 9; // Digital pin used to connect to the servo's control pin
void setup()
{
Serial.begin(9600);
myservo.attach(servopin); // Attaches the servo on servopin to the servo object
}
void loop()
{
val = analogRead(potpin); // Reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // Scale "val" to use it with the servo (value between 0 and 180)
// (See reference on "map" at http://arduino.cc/en/Reference/Map)
// map(value, fromLow, fromHigh, toLow, toHigh);
myservo.write(val); // Sets the servo position according to the scaled value
// the Servo library. It goes from 0 to 180 and corresponds to the
// degree of rotation.
Serial.println(val); // Send the value out thru the serial port.
delay(15); // Waits for the servo to get there.
}
Servo Sweep
// Sweep
// by BARRAGAN
#include <Servo.h>
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
}
void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
Two Servos
// A sample of how to control 2 servos at the same time. // Note – Apparently, the Servo library can handle this. The library on Arduino's playground at // http://www.arduino.cc/playground/ComponentLib/Servotimer1 is really broken and generates // a bunch of errors straight from the import command. // // Khazar, 11/08 #include <Servo.h>Servo myservo1; // Create a servo object to control servo 1 Servo myservo2; // Create a servo object to control servo 2 int pos1 = 0; // Variable to hold the position of servo 1 int pos2 = 0; // Variable to hold the position of servo 2 long timer1Val = 100; // Variable to hold the timer for servo 1 long timer2Val = 100; // Variable to hold the timer for servo 2 int updown1 = 1; // Variable to hold the positive or negative change in angle int updown2 = 1; // for servo 1, ditto for servo 2 void setup() { myservo1.attach(9); // Attaches servo1 to pin 9; myservo2.attach(10); // Attaches servo2 to pin 10; Serial.begin(9600); // Turn on serial communication just in case... timer1Val = millis(); // init timer 1; timer2Val = millis(); // init timer 2; } void loop() { // Timer 1 loop if ((timer1Val + 100) <= millis()) { myservo1.write(pos1); // do movement Serial.print("Servo 1 position: "); Serial.println(pos1); // check value pos1 = pos1 + updown1; // increment or decrement value depending on updown's value if (pos1 == 180) { // If pos1 reaches 180, change directon by making updown negative updown1 = -1; } if (pos1 == 0) { // If pos1 reaches 0, change direction by making updown positive updown1 = 1; } timer1Val = millis(); // Reset the timer } if ((timer2Val + 50) <= millis()) { myservo2.write(pos2); // do movement Serial.print("Servo 2 position: "); Serial.println(pos2); // check value pos2 = pos2 + updown2; // increment or decrement value depending on updown's value if (pos2 == 180) { // If pos1 reaches 180, change directon by making updown negative updown2 = -1; } if (pos2 == 0) { // If pos1 reaches 0, change direction by making updown positive updown2 = 1; } timer2Val = millis(); // Reset the timer } }
Servo Chaos & the all important doSwing()
Download servo_chaos01.txt file
#include <Servo.h>// Include Servo library // Create Servo object Servo myservo ; // Variables // pins int servopin = 10; // timers long timer = 0; int offset = 15; // servo angle int angle = 0; int targetangle = 180; // Setup void setup(){ Serial.begin(9600); pinMode(servopin,OUTPUT); myservo.attach(servopin); myservo.write(angle); } // Das Loop void loop() { // Check the timer if(timer + offset < millis()) { // if the timer has passed, do the swing doSwing(); // reset the timer timer = millis(); } } void doSwing() { //Serial.println(angle); if(angle == targetangle) { targetangle = random(181); Serial.println(targetangle); } if (angle < targetangle) { angle = angle + 1; } if (angle > targetangle) { angle = angle - 1; } myservo.write(angle); }