0

So I had a test last week with the Arduino Uno starter kit. We got a task including the servo. I had to get the servo to go from 0 to 90 degrees and it was supposed to use 4 seconds to do so. Than back from 90 degrees to 0 again. My code is like this:

#include <Servo.h>

Servo myservo;
int pos = 0;
void setup() {
  myservo.attach(9);
}
void loop() {
  for (pos = 0; pos <= 90; pos += 1) {
    myservo.write(pos);
    delay(40);
  }
  for (pos = 90; pos >= 0; pos -= 1) {
    delay(13);
  }
}

As you see the delay should be just 40 and 13 milliseconds, but it used 4 seconds and 1.3 seconds. but since the code was, wrong I didn't pass on this task. So what have I done wrong.

  • 1
    90*40ms = 3,6s. 90*13ms=1,17s. So you have 3,6s for the first movement and 1,3s for the seconds movement. Please describe further, how this does not comply with the requirements, since you wrote, that it needs to take 4s and you saw 4s. Thats what I understood – chrisl Mar 14 at 9:31
  • 90*40ms = 3,6s, but there is also a bit of overhead as myservo.write(pos); and the for loop also take some time to execute (there are also interrupts that could add some additional delays). But I'm guessing that this overhead is negligible. It might be that the examiner wanted you to take that into account. Note that even if you had a delay(44), you'd only get 3.96s, so you'd be 40ms off. The examiner might not like this. – Gerben Mar 14 at 10:19
  • Also your second loop isn't doing anything with the servo... – Majenko Mar 14 at 10:58
0

I would use the millis() function to set up a timing as accurate as possible.

Before starting your operations store the actual value of the millis() timer to a variable time_passed. After finishing your operation, in this case a 1° step of the Motor, wait until the 90's part of your desired time (4s) is reached. Then you increase your time_passed with the 90's part of your desired time bevor going on.

#include <Servo.h>

unsigned long time_passed;
unsigned long timestep = 4000 / 90; // necessary period to reach 4s in 90 steps
Servo myservo;
int pos = 0;

void setup() {
  myservo.attach(9);
}
void loop() {
  time_passed = millis();
  for (pos = 0; pos <= 90; pos += 1) {
    myservo.write(pos);
    while ( millis() - time_passed < timestep) {
    }
    time_passed = time_passed + timestep; // this was time_passed = millis(); at testing, but this should be even more accurate. Hope I made no mistake, since I tested only the previous Version ;-)

  }

  for (pos = 90; pos >= 0; pos -= 1) {
    myservo.write(pos);
    while ( millis() - time_passed < timestep) {
    }
    time_passed = time_passed + timestep; // this was time_passed = millis(); at testing, but this should be even more accurate. Hope I made no mistake, since I tested only the previous Version ;-)
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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