Incremental Servo Movement

Hello All,

I’m on another arbitrary project I conjured up just to continue practicing the basics.

I’ve got my project wired up so that 3 successive LED’s strike. But after the 3rd I’d like my attached servo motor to move in a desired increment (not the popular sweeping situation).

Right now, the LED’s are working great but just the servo…not so much. Here is the code, which may not be very logical at this point after a few hours of tinkering with the code and trying anything I could think of -

#include <Servo.h>
Servo myServo;
int pos = 0;
int prevPos = pos + 45;
int greenPin = 11;
int redPin = 12;
int bluePin = 13;

void setup(){
  myServo.attach(9);
  pinMode (greenPin, OUTPUT);
  pinMode(redPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}

void loop()
{
  digitalWrite (greenPin, HIGH);
  digitalWrite (redPin, LOW);
  digitalWrite (bluePin, LOW);
  delay(250);
  
  digitalWrite (greenPin, HIGH);
  digitalWrite (redPin, HIGH);
  digitalWrite (bluePin, LOW);
  delay(250);
  
  digitalWrite (greenPin, HIGH);
  digitalWrite (redPin, HIGH);
  digitalWrite (bluePin, HIGH);
  delay(250);
  
  digitalWrite (greenPin, LOW);
  digitalWrite (redPin, LOW);
  digitalWrite (bluePin, LOW);
  myServo.write(prevPos + 45);
  delay(250);
}

Thank you all - be well!

prevPos is never updated, so it never changes.

When you post in this forum (or others), it's generally a good idea to state what the problem actually is. "This code doesn't do what I want" is useless unless you also tell us what it does AND what you want it to do.

Paul, I don't know what's so cryptic about "

I've got my project wired up so that 3 successive LED's strike. But after the 3rd I'd like my attached servo motor to move in a desired increment (not the popular sweeping situation).

I want the servo to move once the 3rd LED has illuminated.

First mention of "cryptic", but no description of what actually happens.

Have you tried something simple like below? Do your LEDs actually light up?

#include <Servo.h>
Servo myServo;
int pos = 0;
int prevPos = pos + 45;
int greenPin = 11;
int redPin = 12;
int bluePin = 13;

void setup(){
  myServo.attach(9);
  pinMode (greenPin, OUTPUT);
  pinMode(redPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  myServo.write(0);
}

void loop()
{
  digitalWrite (greenPin, HIGH);
  digitalWrite (redPin, LOW);
  digitalWrite (bluePin, LOW);
  delay(250);
  
  digitalWrite (greenPin, HIGH);
  digitalWrite (redPin, HIGH);
  digitalWrite (bluePin, LOW);
  delay(250);
  
  digitalWrite (greenPin, HIGH);
  digitalWrite (redPin, HIGH);
  digitalWrite (bluePin, HIGH);
  delay(250);
  
  digitalWrite (greenPin, LOW);
  digitalWrite (redPin, LOW);
  digitalWrite (bluePin, LOW);
  
  delay(250);
  myServo.write(90);
}

AWOL - I described what is happening in my 1st post

Right now, the LED's are working great but just the servo...not so much.

And yes, zoomkat they are lighting up in succession as desired.

Did you take note and act on reply #1 ?

Downtownmjb:
after the 3rd I'd like my attached servo motor to move in a desired increment (not the popular sweeping situation).

The initial position of the servo is undefined; I think you need to call myServo.write(pos); during setup, preferably before you call attach, to move it to its initial position.

If you intention is that the servo will move by 45 degrees each time the loop executes then you need to replace this:

myServo.write(prevPos + 45);

with something like this:

pos += 45;
myServo.write(pos);

After running this for a few seconds you will realise what the next problem is ...