rhoadley.net   music   research   courses   software   blogs

ARU    Seminars    Comp&Imp    NMP    CMC2a    CBHH
Sensor Technology    Sonic Art    Major Project    MA Resources


Resources:    Bioacoustics    Jitter    MaxMSP    OSC    Physical    PD       CBHH    sTech    SuperCollider    C/Xcode

sTech Resources:     Home     Blog     Forum     Examples     Projects     Tasks     Tutorials


Sensor Technology Tasks

Arduino and Servos

Task 6 Name: Arduino and Servos Set: w8i Due: Thursday 16th December 2010 Weighting: 10% Courses: stech
Prev Task: Auduino Next Task: Arduino and Multiplexing
Task Summary All stech tasks

To cover:




 

About Servos

Example 1


// Sweep
// by BARRAGAN  
// additions by rhoadley
// servo red to 5v, black to ground, yellow to digital pin 9;

#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 
int pos2 = random(180);
 
void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
} 
 
void loop() 
{ 
  pos2 = random(18);
  for(pos = 0; pos < pos2; pos += 1)  // goes from 0 degrees to a random variation of 18 degrees - play with this! 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(random(20));               // wait a random time for the servo to reach the position 
  } 
  for(pos2; pos>=1; pos-=1)     // goes fromthe previous position to 0 degrees 
  {                                
    myservo.write(pos);         // tell servo to go to position in variable 'pos' 
    delay(random(20));          // waits a random time for the servo to reach the position 
  } 
} 


Example 2


// Sweep
// by BARRAGAN  
// additions by rhoadley
// servo red to 5v, black to ground, yellow to digital pin 9;

#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 < 45; pos += 1)  // goes from 0 degrees to 45 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(5);                       // waits 5ms for the servo to reach the position 
  } 
  for(pos = 45; pos>=1; pos-=1)     // goes from 45 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(1);                       // waits 1ms for the servo to reach the position 
  } 
} 


Example 3


// Sweep
// by BARRAGAN  
// additions by rhoadley
// servo red to 5v, black to ground, yellow to digital pin 9;

#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 
  Serial.begin(9600);
} 
 
 
void loop() 
{ 
  for(pos = 0; pos < 45; pos += 1)  // goes from 0 degrees to 45 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(5);                       // waits 15ms for the servo to reach the position 
  } 
 
  // Here we add a rhythm, choosing a delay if a random condition is met...
  if (random(100) < 50)
      { 
        Serial.println("200");
        delay(200); 
      }
    else
      { 
        Serial.println("1");
        delay(1); 
      }
  
  for(pos = 45; pos>=1; pos-=1)     // goes from 45 degrees to 0 degrees 
  {                                 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(1);                       // waits 15ms for the servo to reach the position 
    
  } 
  

} 


Example 4


// Two Servos
// by BARRAGAN  
// additions by rhoadley
// servo red to 5v, black to ground, yellow to digital pins 9 and 10;

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
Servo myservo2;   // a maximum of eight servo objects can be created 
 
int pos = 0;    // variable to store the servo position 
int pos2 = 0;
 
void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
  myservo2.attach(10); 
  Serial.begin(9600);
} 
 
 
void loop() 
{ 
  for(pos = 0; pos < 45; pos += 1)  // goes from 0 degrees to 45 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(5);                       // waits 15ms for the servo to reach the position 
  } 
 
  // Here we add a rhythm, choosing a delay if a random condition is met...
  if (random(100) < 50)
      { 
        Serial.println("200");
        delay(200); 
      }
    else
      { 
        Serial.println("1");
        delay(1); 
      }
  
  // second servo
  for(pos2 = 45; pos2>=1; pos2-=1)     // goes from 45 degrees to 0 degrees 
  {                                 
    myservo2.write(pos2);              // tell servo to go to position in variable 'pos' 
    delay(1);                       // waits 15ms for the servo to reach the position 
  } 
    for(pos2 = 0; pos2 < 180; pos2 += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo2.write(pos2);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos2 = 180; pos2>=1; pos2-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo2.write(pos2);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 

} 





to top of page The Task

  • Choose and obtain a servo.
  • Develop a simple demonstration using a servo or a motor. Ideally, this should involve the creation of some sort of sound.
  • For instance, make a simple beater to generate rhythm.
  • For extra browny points, you might make a swivel motor to provide an extra degree of freedom.
  • Extend the code above to make use of different rhythms in accordance with some other physical criteria (pot/button position/selection)...

  • Clearly, I don't want you to submit your Arduinos. That means you need to submit some documentary evidence that you've completed your task. The most obvious way to do this is to submit some sort of video of your task working. If you can't do this, take photos. Bear in mind that you'll get extra marks for 'added value', so try to adapt and make more interesting your files. Simply repeating what we've already done will get minimal marks.

Submission

  • Zip or Stuff your patches, demos, etc. into one file called your_student_number_"arduinoservos" (e.g. 0504335_arduinoservos.zip or 0504335_arduinoservos.sit), include a readme with your name and student number and, if necessary, how to use or just open the patch.
  • Submit a copy of the files to the i-Centre on Thursday 15th December 2011