rhoadley.net   music   research   software   blogs

aru    seminars    m&t    critski    focm1a    cmc    circuit bending    mic2b    sensor technology    comp 3    sonic art    major project
youtube    vimeo    facebook


Sensor Technology Tasks

Arduino and Servos

Task 6 Name: Ping - Joining Code Set: w4i Due: Monday 19th December 2016 Weighting: assessable/recommended (10%) Courses: stech
Prev Task: Servos Next Task: Investigating sensors - Make your own sensor - Make your own arduino
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 
  } 

} 

Example 5: control servo with a potentiometer




/*
 Controlling a servo position using a potentiometer (variable resistor)
 by Michal Rinott 

 modified on 8 Nov 2013
 by Scott Fitzgerald
 http://www.arduino.cc/en/Tutorial/Knob

*/

#include 

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

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 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, 180);     // scale it to use it with the servo (value between 0 and 180)
  myservo.write(val);                  // sets the servo position according to the scaled value
  delay(15);                           // waits for the servo to get there
}








to top of page The Task

  • Choose and obtain a servo.
  • Develop a simple demonstration using a servo or a motor. This should involve the creation of some sort of sound.
  • For instance, make a simple beater to generate rhythm.
  • For extra brownie 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)...

  • Added value
    By completing the details of the task you will achieve at least a pass mark. By imaginatively and creatively considering how you might implement the task originally you can add value to your submission, and this added value may increase your mark significantly. Even when making videos of short demonstration tasks try to consider musical and performance criteria.

    What to submit
  • 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, as well as of the code running. If you can't do this, take photos.

  • Media files
    You must submit media files, such as video, audio or image files, but please ensure that video files are compressed to a reasonable degree. You should never submit dv files, but compress these to mp4. You should submit no file that is greater in size than 25MB/minute.

  • Compress (zip) your patches, demos, etc. into one file called your_student_number_"arduinoservos" (e.g. 0504335_arduinoservos.zip), 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 Monday 19th December 2016