Help with servo controller.

Hello all,
First time poster, long time lurker.
Im in the preliminary stages of building an BLDC motor for an AIRSOFT AEG Rifle. Ive already got a properly sized BLDC and esc. From what i gather, a servo tester is needed. Since id like to avoid a big knob, i decided to trying and make one using a nano. Ive got the nano on order, and i wanted to get ahead of the coding.
Im going for a 2 button setup, and pressing both buttons sets position to 0 which im hoping is off!.
Alot of my code is borrowed.
Please tell me if this would work, or wjat changes i need to make.

#include <Servo.h>

Servo myservo; // define servo
#define upPin 4
#define downPin 5
int pos = 90; // set angle
int delayPeriod = 150; // smooths changes
void setup()
{
myservo.attach(10);

myservo.write(pos); // center servo

pinMode(upPin, INPUT);
pinMode(downPin, INPUT);

digitalWrite(upPin, HIGH); // pullup resistor
digitalWrite(downPin, HIGH); // pullup resistor

//servo movement
{
if(digitalRead(upPin) == LOW && digitalRead(downPin) == HIGH)
{
// by 1 degree
if( pos > 0)
--pos;
myservo.write(pos);
delay(delayPeriod);
}
if(digitalRead(downPin) == LOW && digitalRead(upPin) == HIGH)
{
if( pos < 180)
++pos;
myservo.write(pos);
delay(delayPeriod);
}
}
if(digitalRead(downPin) == LOW && digitalRead(upPin) == LOW)
{
pos = 0;
myservo.write(pos);
delay(delayPeriod);
}
} }

Thanks in advamce for the help!

Mgmgtech

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Have you looked in the IDE examples for Servo Knob and Sweep examples?

Thanks.. Tom.. :slight_smile:

The code has an extra closing brace on the end. This is much more obvious if you Auto Format the code in the IDE. You also have extra unnecessary braces in the body of the code.

#include <Servo.h>

Servo myservo;          // define servo
#define upPin 4
#define downPin 5
int pos = 90;          // set angle
int delayPeriod = 150;   // smooths changes
void setup()
{
  myservo.attach(10);
  myservo.write(pos);    // center servo
  pinMode(upPin, INPUT);
  pinMode(downPin, INPUT);
  digitalWrite(upPin, HIGH);      // pullup resistor
  digitalWrite(downPin, HIGH);    // pullup resistor
  //servo movement
  {
    if (digitalRead(upPin) == LOW && digitalRead(downPin) == HIGH)
    {
      // by 1 degree
      if ( pos > 0)
        --pos;
      myservo.write(pos);
      delay(delayPeriod);
    }
    if (digitalRead(downPin) == LOW && digitalRead(upPin) == HIGH)
    {
      if ( pos < 180)
        ++pos;
      myservo.write(pos);
      delay(delayPeriod);
    }
  }
  if (digitalRead(downPin) == LOW && digitalRead(upPin) == LOW)
  {
    pos = 0;
    myservo.write(pos);
    delay(delayPeriod);
  }
}
}

How are the inputs wired ? It is important that they are at a known state at all times so pullup or pulldown resistors are a good idea. Best of all, use INPUT_PULLUP in the pinMode()s for the inputs to turn on the internal pullup resistors and wire the switches to take the pins to GND when activated.

It would be neater if you read the input states once at the start of loop(), put the values in variables and used the value of the variables later rather than doing multiple digitalRead()s.. With 2 inputs their states can be in one of four combinations. It may not matter, but you have not defined actions for all of them.

How are the servos powered ? The Arduino can only supply a limited current on its 5V output so using an external battery pack for the servos is a good idea.

If I understand correctly you're trying to control an ESC/brushless motor. No servos are involved. If so please use more sensible variable names and comments.

You may well need some special code in setup() to initialise the ESC...and it can vary for different types of ESC. Also depending on the type of ESC/motor "Off" may be 0 or it may be 90.

With a delay of 150ms after every extra 1 degree write() it's going to take you about 30 seconds to go from off to full speed. That's a lot of smoothing.

Steve