Problem using SimpleTimer library for Servo

Help me~ I’m just a beginner. :-[ The program won’t repeat after running once with the servo. But without the servo, it works just fine with only the LED. Why does this happen? Any advice?

#include <SimpleTimer.h>
#include <Servo.h>

SimpleTimer timer;
int LED = 13;
Servo xservo;
int xpos = 0;

void RepeatTask()
{
  digitalWrite(LED, HIGH);
  delay(1000);
  digitalWrite(LED,LOW);
  delay(1000);
  
  for (xpos = 0; xpos < 90; xpos += 1)
    {
      xservo.write(xpos);
      delay(1000);
      xservo.detach();
      delay(8000);
      xservo.attach(9);
      xservo.write(90);
      delay(1000);
      xservo.detach();
    }  
}

void setup()
{
  Serial.begin(9600);
  xservo.attach(9); //
  xservo.write(90); //set to midpoint initially
 
  //timer action
  timer.setInterval(120000, RepeatTask);//repeat every 2mins
  pinMode(LED, OUTPUT);
}

void loop()
{ timer.run();
 }

Thanks in advanced!

Hi Ayam05

When RepeatTask() is called, this loop is executed 90 times.

  for (xpos = 0; xpos < 90; xpos += 1)
    {
      xservo.write(xpos);
      delay(1000);
      xservo.detach();
      delay(8000);
      xservo.attach(9);
      xservo.write(90);
      delay(1000);
      xservo.detach();
    }

Each iteration of the loop includes 10,000ms or 10 seconds of delay. That totals 900 seconds before RepeatTask() completes. Yet your timer is set to repeat every 120 seconds.

Also, not sure why you are attaching and detaching the servo in the loop?

Regards

Ray

Look at how the servo is controlled in the demo several things at a time, and, more generally, at how timing is managed just using millis() with no need for a library.

…R

How are you powering the servos? The most common mistake is trying to power motors/servos directly from the arduino. They need a separate external power supply.