Generic Code For Servo Motor - Struggling with servo attach

Hi All,

Firstly Thanks for looking and Many Thanks if you can help solve my issue.

Persistance is the key - but i''m running out of persistance >:(

Basically i am trying to write generic code for as much of my project as possible and for a large part i have suceeded... until now

I have 3 functions to control the output of a single servo motor rotation to a specified position, namely Danger, Clear and Clear with Bounce. I call these functions in a FSM when needed and everything works as it should. I have duplicated these functions for a second servo motor (renaming the relevant variables and function name) and again it all works as it should.

Thats 2 servo motors but the project requires 20 so thats a lot of coding and renaming so I am at the Generic Code Crossroads.

I'm new at coding and this generic malarky is frying my brain so Is there a way of making this generic within a function ?

void servoToDanger() {

    downMainStarterServo.attach (downMainStarterServoMotorPin);

    // Signal From Clear Position To Danger Position
    for (signalPosition = (downMainStarterClearPosition); signalPosition >= (downMainStarterDangerPosition); signalPosition = (signalPosition - (downMainStarterDangerDegreeSteps))) // for (goes from position, goes to position, increment position each time through for loop)
    {
      downMainStarterServo.write(signalPosition);
      delay(downMainStaterDangerTravelSpeed);
    }
    downMainStarterServo.detach ();
  }

and my variables

int downMainStarterDangerPosition = 70;       // Sets the Danger Position (This number can be altered to fine tune the arm position to horizontal position).
int downMainStarterDangerDegreeSteps = 1;      // Sets the Degree of Rotation for each step through the code [1. Must be whole number; 2.The higher the number the quicker it rotates through the code)
int downMainStaterDangerTravelSpeed = 25;     // Sets a delay between each step through the code [1. Has to be a whole number; 2. The higher the number the slower it rotates]

int downMainStarterClearPosition = 100;        // Sets the Clear Position (This number can be altered to fine tune the arm position to dropped position).
int downMainStarterClearDegreeSteps = 2;      // Sets the Degree of Rotation for each step through the code [1. Must be whole number; 2.The higher the number the quicker it rotates through the code)
int downMainStarterClearTravelSpeed = 15;     // Sets a delay between each step through the code [1. Has to be a whole number; 2. The higher the number the slower it rotates]

bool downMainStarterBounceOn = false;       // Set to "false" there will be no Signal Bounce; Set to "true" there will be Signal Bounce
int downMainStarterBouncePosition = 76;     // Sets theBounce Position (This number can be altered to fine tune the arm position that the arm bounces up too).
int downMainStarterBounceDegreeSteps = 1;   // Sets the Degree of Rotation for each step through the code [1. Must be whole number; 2.The higher the number the quicker it rotates through the code)
int downMainStarterBounceTravelSpeed = 15;  // Sets a delay between each step through the code [1. Has to be a whole number; 2. The higher the number the slower it rotates]

And more importantly how to make the attach part of the servo generic

    downMainStarterServo.attach (downMainStarterServoMotorPin);

So why am i attaching and detaching the servo?... only to reduce chatter, they are cheap servos, whether or not thats a good idea i'm sure i'll find out. ::slight_smile:

This is one of the 3 functions BUT im positive the answer to them all is the same as the answer to this piece of code as they are all similar.

Thanks in advance

whether or not thats a good idea i'm sure i'll find out.

It is not. There is no reason to detach and attach the servos every time.

There IS a reason to use arrays, though, and to learn to write functions that take arguments. The Arduino people did not saddle you with 14 different functions to read the state of the 14 digital pins on a 328-based Arduino, did they?

It is not. There is no reason to detach and attach the servos every time

Well ok i've learnt something new - Thank You

There IS a reason to use arrays, though, and to learn to write functions that take arguments. The Arduino people did not saddle you with 14 different functions to read the state of the 14 digital pins on a 328-based Arduino, did they?

This is exactly why I and many people like me, not gifted with the art of writing code naturally, struggle on, whilst people like you write comments like this.

I asked for help and i thankyou for your help.

So if i remove the facetious comments i guess i'm looking at arrays.

Arrays are what you need. Here is a small example. It could be tidied up quite a lot but will maybe give you some ideas.

//sweep 2 servos at different speeds
//servo details are held in an array

#include <Servo.h>

Servo servo[2];                //array of servos
const byte servo_pin[] = {11 , 12}; //pins for servos

long servoDelay[] = {100, 80};    //delays between moving
long servoPrevTime[] = {0, 0};     //times of previous moves
int servoStep[] = {1, 2};     //step values
int servoPos[] = {90, 90};   //start positions

void setup()
{
  for (int i = 0; i < 2; i++)
  {
    servo[i].attach(servo_pin[i]);
  }
}

void loop()
{
  for (int currentServo = 0; currentServo < 2; currentServo++)
  {
    if ((millis() - servoPrevTime[currentServo]) > servoDelay[currentServo])
    {
      servoPos[currentServo] += servoStep[currentServo];
      if (servoPos[currentServo] > 180)
      {
        servoPos[currentServo] = 180;
        servoStep[currentServo] = servoStep[currentServo] * -1;
      }
      else if (servoPos[currentServo] < 0)
      {
        servoPos[currentServo] = 0;
        servoStep[currentServo] = servoStep[currentServo] * -1;
      }
      servo[currentServo].write(servoPos[currentServo]);
      servoPrevTime[currentServo] = millis();
    }
  }
}

Note that there is no blocking code, such as delay() in the program.

Which Arduino are you using ? 20 servos is going to be a stretch