Simplest Coding for Continuous Servo

Hello Everyone,

I'm in need of help. I want to teach some teenagers how to run a continuous servo in either direction for a specified amount of time with an arduino. This is kind of step 4 of 10 if you will, but if I can't teach them step 4 then I can't teach them steps 5-10. So I'm asking all of you for the most basic code to accomplish this task. I'm picturing an "if-then or else" command that will tell a servo to move in one direction for a specified amount of time if an analog sensor reads above a certain reading and moves in the opposite direction if reads below said reading. I've been looking for code that would work for me, and apparently coding an arduino to run a continuous servo is not as simple as one would think. Here is an example of some code I found on the forum (scroll down to comment #9 for the code in question), but I thought maybe there was a more basic way to accomplish what I want...maybe there isn't? Anyway, I'm a bit desperate at this time.

Thank you,

Brock

Usually we look at the code you steal write and ask what is not working with it.

You respond, we explain the problem(s) that we see in the sketch.

Show us your attempt at this, please use code tags.

It might also be useful for you to point out which parts might be a stumbling-block for your students.

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  int val = analogRead(yourSensor);   // reads the value from sensor
  if (val < someValue) { // choose your value and put it in here
    myservo.write(0);         // full speed one way
    delay(specified time);
  }
  else {
    myservo.write(180);   //  full speed the other way
    delay(specified time);
  }
}

That’s about as simple as it gets but you will almost certainly find that you have more detailed requirements for it than you’ve told us yet! And probably really simple code like that using delay() won’t do it for you. But at least it’s simple.

Steve

larryd:
Usually we look at the code your steal write and ask what is not working with it.

ROFL!!

-jim lee

@slipstick Are you sure the servo will still "go" if your sitting in a delay() loop? I never use the standard servo routine, so I don't actually know. But I think it may not.

-jim lee

jimLee:
@slipstick Are you sure the servo will still “go” if your sitting in a delay() loop? I never use the standard servo routine, so I don’t actually know. But I think it may not.

-jim lee

Yes, it will.

Thank you Slipstick,

That worked!

Now I just need to code the servo to rotate for a premeditated time interval, and I'll be all set. I'll get to it.

jimLee:
@slipstick Are you sure the servo will still "go" if your sitting in a delay() loop? I never use the standard servo routine, so I don't actually know. But I think it may not.

-jim lee

It definitely will. I may not be good at clever stuff but when you need some simple-minded code I'm your man.

Steve

brocko:
Now I just need to code the servo to rotate for a premeditated time interval, and I'll be all set. I'll get to it.

Errr...that's what the delay()s should do except I forgot to add a write(90) after them to stop the servo. You might need to fiddle with the 90 slightly but at somewhere around there the servo will stop.

Steve