Monday, 20 February 2012


The code below connects the servo to a specific pin on the arduino board. In this case pin 9.
instruction is given to the servo via the write section of the code.
The position of the torque goes from 0 to 160 degrees.
it will take 1000ms to reach position in variable pos.

#include <Servo.h> 

Servo myservo;


const int pingPin = 7;
int pos = 160;
boolean extended = false;
int timer = 0;


int mySpeed = 200;

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  myservo.attach(9); 
  myservo.write(pos);
  delay(1000);
}

void loop(){
  timer = timer + 1;
  if (timer > 10000 && extended == false){
    while(pos > 1){
       myservo.write(pos);
       delay (mySpeed);
       pos --;
       int  duration = analogRead(0);
        if (duration > 300){
            pos = 160;
             myservo.write(pos);
            timer = 0; 
            extended = false; 
            break ; 
        }
    }
    extended = true;
    timer = 0;
  }
  



  int  duration = analogRead(0);
  if (duration > 300){
    pos = 160;
     myservo.write(pos);
      timer = 0; extended = false;     
  }




}


No comments:

Post a Comment