Help with controlling 2 servos!

Hello! I'm pretty new here so forgive me if this is an easy question.

I am working on a project where I need to control 2 servos each with a potentiometer (actually a Joy stick). One is a normal servo and the other is a continuous rotation servo. I am also controlling some LED's simultaneously. The code for each servo and the code for the leds seem to work fine individually, but when I combine them the servo's are very fidgety and the LR one doesn't work. I am wondering if this is because there is some issue with putting 2 analogWrite's very close to each other in the code or because of something else... Here is my code:

#include <Servo.h>
Servo updown;
int updownservopin = 11;
float angle = 90;
int UDpotPin = 4;
int ledred = 2;
int ledyell = 3;
int ledgre = 4;
int safety = 5;
int motorPin = 9;
int LRpotPin = 5;
int motorSpeed = 0;
boolean green = LOW;

void setup()
{
  Serial.begin(9600);
  updown.attach(updownservopin);
  pinMode(UDpotPin,INPUT);
  pinMode(ledred, OUTPUT);
  pinMode(ledyell , OUTPUT);
  pinMode(ledgre, OUTPUT);
  pinMode(safety, INPUT);
  pinMode(motorPin, OUTPUT);
}

void loop()
{
  int i = 0;
  while (green == HIGH) //all three LED's lit
  {
    if(digitalRead(safety) == HIGH)
    {
      i = 1;
    }
    else if(digitalRead(safety) == LOW && i == 1)
    {
      digitalWrite(ledred,LOW);
      digitalWrite(ledyell,LOW);
      digitalWrite(ledgre,LOW);
      green = LOW;
    }
    while(digitalRead(safety)==LOW)
    {
      writeAngle();
      delay(2);
      writeLRspeed();
      delay(15);
    }
    writeAngle();
    delay(2);
    writeLRspeed();
    delay(15);
  }
  while(green==LOW) //red yellow or no led's lit
  {
    digitalWrite(ledred,LOW);
    digitalWrite(ledyell,LOW);
    digitalWrite(ledgre,LOW);
    int val = 0;
    writeAngle();
    delay(2);
    writeLRspeed();
    delay(15);
    while(digitalRead(safety) == LOW) //button press starts lighting LED's one by one
    {
      val = val + 1;
      if (val >=1 && val <90)
      {
        digitalWrite(ledred,HIGH);
      }
      else if (val >=90 && val <180)
      {
        digitalWrite(ledyell,HIGH);
      }
      else if (val >=180)
      {
        digitalWrite(ledgre,HIGH);
        green = HIGH;
      }
      delay(15);
      writeAngle();
      delay(2);
      writeLRspeed();
    }
  }
}

void writeAngle()
{
  if(abs(1-(float)map(analogRead(UDpotPin),0,1023,0,2000)/1000)-.02 <0)  // if else buffer so centered joy keeps UD still
  {

  }     
  else
  {
    angle = angle-1.0+(float)map(analogRead(UDpotPin),0,1023,0,200)/100;
  }
  angle = constrain(angle,0,180);  //Constrains to sensical Library values
  updown.write(angle); 
}

void writeLRspeed()
{
  motorSpeed = map(analogRead(LRpotPin),0,1000,0,255);
  analogWrite(motorPin, motorSpeed);
  Serial.println
  (motorSpeed);
  
}

Thanks for any help!

Hi,

My iPad doesn't let me see all of your code, but you don't mention what you are using to power the servos and LEDs. If you are just using the arduino, that could be the problem, see the two links in my signature for details.

Duane B

I am using the arduino to power all. Setting up a new test with separate voltage source for the servo's I'll let you know if that fixes it. Thanks for the tip.

EDIT I am now using a separate power source. Unfortunately it changed nothing...

Add some debugging. Do something like this:

void writeLRspeed()
{
  int LRRead = analogRead(LRpotPin);
  motorSpeed = map(LRRead,0,1000,0,255);
  analogWrite(motorPin, motorSpeed);
  Serial.print("LR Read: ");
  Serial.println(LRRead);
  Serial.print("Speed: ");
  Serial.println(motorSpeed);  
}

If you are seeing that the "LRRead" is giving erratic values then you can either perform two analogReads and throw out the first or perform many analogReads and average them.

 (motorSpeed);

What's that supposed to do?

Comments would be useful.