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?

Egm
  • 65
  • 1
  • 10

1 Answers1

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);   
}
001
  • 963
  • 6
  • 11