I am trying to figure out how to have the arduino stop trying to increment the angle of a servo position if the desired position cannot be obtained within a time frame of 2 or 3 seconds. Basically, the servo in this project is used to open and close a door to let dog food flow through the system. When the closing procedure happens, the servo will get jammed with food and never reach the angle I set it for. When this happens, no other functions for the feeder can happen because of the jam. However, stopping the servo at the point of the jam is sufficient enough as no food will flow at this point. I just need to know how to get the servo to stop at this position so it doesn't burn out and also that the rest of the functions I have set for the system can run. My thought process for handling the jam is the following pseudo code:
- Start the servo close process
- If the close process does not reach desired angle in set time frame then stop servo from trying to reach the angle, and continue to other functions.
- else continue to other functions.
I've been trying to achieve this by modifying the basic servo example below.
Any help is greatly appreciated as I am a junior programmer and this is for a school project.
Code: Select all
#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 curAngle;
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
curAngle = myservo.read();
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(20); // waits 15ms for the servo to reach the position
}
if(curAngle <= 170){
myservo.detach();
digitalWrite(9, LOW);
}else{
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
} // end setup
void loop()
{
}