0

I'm still struggling with the Servotimer2 library

I have an Arduino uno board with a servo motor with the servotimer2 library because I'm using a music shield beside the servomotor and the music shield library and the servo.h wasn't compatible. But finally I make the two of them working together with the servotimer2.h but now I want my servo to rotate 90 or 180 and then "stop" for a minute and come back again, rotate, stop, rotate,stop, rotate, stop... short of thing so I read this what is commonly done to stop a servo after reaching desired point and now I know that servo can't stop, but they can detach so I tried making something like this

#include <ServoTimer2.h>  // the servo library
#define rollPin  9

ServoTimer2 servoRoll;   


void setup() {
  servoRoll.attach(rollPin);     

}


// this function just increments a value until it reaches a maximum 
int incPulse(int val, int inc){
   if( val + inc  > 2000 )
      return 1000 ;
   else
       return val + inc;  

}

void loop()
{ 
   mover();
   parar();

}

void mover() { 
 int val;

   servoRoll.attach(rollPin);
   val = incPulse( servoRoll.read(), 2);
   servoRoll.write(val);
   delay(10);

}
void parar() { 

  servoRoll.detach();
     delay(100);
}

but the servo is only moving very slow and I think is not detaching in any moment. I'm quite lost, I'm very noobie and in the post(what is commonly done to stop a servo after reaching desired point) they said things like making while(true){} loop in the setup? or use millis() and other things that I don't understand quite well But someone tried to do something like control the time the servo is stopping with the delay, so I tried something like this

#include <ServoTimer2.h>  // the servo library

// define the pins for the servos
#define rollPin  9

int pos = 0;


ServoTimer2 servoRoll;    // declare variables for up to eight servos


void setup() {
  servoRoll.attach(rollPin);     // attach a pin to the servos and they will start pulsing

}






//from https://learn.adafruit.com/multi-tasking-the-arduino-part-1/a-clean-sweep

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

}

but with this code, my servo is only moving once, and then stops and don't move again

and I think that my servo with the servotimer2 library is only moving with this

   int incPulse(int val, int inc){
   if( val + inc  > 2000 )
      return 1000 ;
   else
       return val + inc;  
}

void loop()
{ 
 int val;

   val = incPulse( servoRoll.read(), 1);
   servoRoll.write(val);
  delay(10);   
}

so, any ideas of what I'm doing wrong?

1 Answer 1

1

my servo is only moving once, and then stops You mean it turns 180° then no more? Once it has reached 180° (or 2000 µs) you need to decrease val by one to go the other way (from 2000 to 1000). For example:

int direction = 1;  // 1 or -1
void loop()
{ 
   int val;

   val = incPulse( servoRoll.read(), direction);
   servoRoll.write(val);
   if (direction <= 1000 || direction >= 2000) {
      direction = -direction;
   }
   delay(10);   
}
2
  • thank you! can you explain me or redirect me to some place where I can learn more about this kind of expressions " if (direction <= 1000 || direction >= 2000) { direction = -direction;"
    – Egm
    May 30, 2017 at 12:54
  • @Egm That is a "logical or" which means only one of the conditions has to be true for the body of the if statement to be executed. Search for "c logical operators".
    – 001
    May 31, 2017 at 0:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.