Making it compact

Hey,
I tried it now for quite some time and still didn't make it right... :-/
can someone help me to fit this in just one funktion please?

//---------------------------| SERVO_01 |-----------------------------------------------

void Servo_01(int min_1, int max_1, int speed_1)
{
  if ((micros()-previousMicros_1) >=speed_1)  {
    previousMicros_1 = micros();    
      
    if (pos_1 > servo_1.readMicroseconds())  { 
      servo_1.writeMicroseconds(servo_1.readMicroseconds() + 1);
    }     
    if (pos_1 < servo_1.readMicroseconds())  {
      servo_1.writeMicroseconds(servo_1.readMicroseconds() - 1);
    }
    if(servo_1.readMicroseconds() == max_1)  {
      pos_1 = min_1;
    }    
    if(servo_1.readMicroseconds() <= min_1)  {
      pos_1 = max_1;
    }     
  }  
}

//---------------------------| SERVO_04 |-----------------------------------------------

void Servo_02(int min_2, int max_2, int speed_2)
{
  if ((micros()-previousMicros_2) >=speed_2)  {
    previousMicros_2 = micros();    
      
    if (pos_2 > servo_2.readMicroseconds())  { 
      servo_2.writeMicroseconds(servo_2.readMicroseconds() + 1);
    }     
    if (pos_2 < servo_2.readMicroseconds())  {
      servo_2.writeMicroseconds(servo_2.readMicroseconds() - 1);
    }
    if(servo_2.readMicroseconds() == max_2)  {
      pos_2 = min_2;
    }    
    if(servo_2.readMicroseconds() <= min_2)  {
      pos_2 = max_2;
    }     
  }  
}

//---------------------------| SERVO_03 |-----------------------------------------------

void Servo_03(int min_3, int max_3, int speed_3)
{
  if ((micros()-previousMicros_4) >=speed_3)  {
    previousMicros_4 = micros();    
      
    if (pos_3 > servo_3.readMicroseconds())  { 
      servo_3.writeMicroseconds(servo_3.readMicroseconds() + 1);
    }     
    if (pos_3 < servo_3.readMicroseconds())  {
      servo_3.writeMicroseconds(servo_3.readMicroseconds() - 1);
    }
    if(servo_3.readMicroseconds() == max_3)  {
      pos_3 = min_3;
    }    
    if(servo_3.readMicroseconds() <= min_3)  {
      pos_3 = max_3;
    }     
  }  
}

//---------------------------| SERVO_04 |-----------------------------------------------

void Servo_04(int min_4, int max_4, int speed_4)
{
  if ((micros()-previousMicros_4) >=speed_4)  {
    previousMicros_4 = micros();    
      
    if (pos_4 > servo_4.readMicroseconds())  { 
      servo_4.writeMicroseconds(servo_4.readMicroseconds() + 1);
    }     
    if (pos_4 < servo_4.readMicroseconds())  {
      servo_4.writeMicroseconds(servo_4.readMicroseconds() - 1);
    }
    if(servo_4.readMicroseconds() == max_4)  {
      pos_4 = min_4;
    }    
    if(servo_4.readMicroseconds() <= min_4)  {
      pos_4 = max_4;
    }     
  }  
}

//----------------------------------------------------------------------------------------------------------------------

Read about arrays, probably arrays of structures.

I did read about arrays and I like to do it myself, if I would be able to....
Here is one of my aproaches, but that seems to be not the right way to do it. I just can't figure it out :cry:

#include <Servo.h>

int num;

Servo servo_1;                         // create servo object to control a servo 
Servo servo_2;        //------------- how do array these????
Servo servo_3;
Servo servo_4;

int pos_1;                             // variable to store the servo position 
int pos_2;
int pos_3;
int pos_4;



unsigned long previousMillis = 0;
unsigned long previousMicros_1 = 0;      
unsigned long previousMicros_2 = 0;      
unsigned long previousMicros_3 = 0;      
unsigned long previousMicros_4 = 0;      

long interval = 1000; 
const int ledPin =  13;      // the number of the LED pin                    
int ledState = LOW; 

  int min_[] = {0,1,2,3,4};
  int max_[] = {0,1,2,3,4};
  int speed_[] = {0,1,2,3,4};
  int pos_[] = {0,1,2,3,4};

//---------------------------| SERVO_01 |-----------------------------------------------

void Servo_X(int num,int min_1, int max_1, int speed_1)
{

  
  
  if ((micros()-previousMicros_1) >=speed_1)  {
    previousMicros_1 = micros();    
      
    if (pos_[num] > servo_[num].readMicroseconds())  { 
      servo_[num].writeMicroseconds([servo_[num]).readMicroseconds() + 1);
    }     
    if (pos_[num] < servo_[num].readMicroseconds())  {
      servo_[num].writeMicroseconds(servo_[num] .readMicroseconds() - 1);
    }
    if(servo_[num].readMicroseconds() == max_[num])  {
      pos_[num] = min_[num];
    }    
    if(servo_[num].readMicroseconds() <= min_[num])  {
      pos_[num] = max_[num];
    }     
  }  
}

with this pretty error:

In function 'void Servo_01(int, int, int, int)':
error: request for member 'writeMicroseconds' in 'servo_[num]', which is of non-class type 'int

I woudn't ask for helf if I wouldn't really need it.

[edit]

why does this code from the night rider work?
isn't it similar to it? exept the " .readMicroseconds"

for (count=5;count>=0;count--) {
   digitalWrite(pinArray[count], HIGH);
   delay(timer);
   digitalWrite(pinArray[count], LOW);
   delay(timer);
  }

[/edit]

If you look at your code, you'll see pattern:

void Servo_03(int min_[glow]3[/glow], int max_[glow]3[/glow], int speed_[glow]3[/glow])
{
  if ((micros()-previousMicros_4) >=speed_[glow]3[/glow])  {
    previousMicros_4 = micros();    

    if (pos_3 > servo_3.readMicroseconds())  {
      servo_3.writeMicroseconds(servo_3.readMicroseconds() + 1);
    }    
    if (pos_3 < servo_3.readMicroseconds())  {
      servo_3.writeMicroseconds(servo_3.readMicroseconds() - 1);
    }
    if(servo_3.readMicroseconds() == max_3)  {
      pos_3 = min_3;
    }    
    if(servo_3.readMicroseconds() <= min_3)  {
      pos_3 = max_3;
    }    
  }  
}

Not sure about that "previousMicros_4" :wink:

Now, imagine some arrays :

int max [N_SERVOS];
int min [N_SERVOS];
int speed [N_SERVOS];
Servo servo [N_SERVOS];

So:

  if(servo[3].readMicroseconds() == max[3])  {
      pos[3] = min[3];
    }

Now, change those "3"s into an index generated by a "for" loop.

Better yet, would be to define a structure to contain max, min, pos etc, for a single servo, and then have an array of these. But that can come later.

I searched so much already for a explaination where I can learn how to use structures, but wasn't able to find any. do you know a helpful link or do you have a good and simpel explainationfor them that I can do them by my self?

If you add [] after the variable name, it is an array.
You've allready assigned an array of values.. you've used this for making an array of numbers (integers).
It really works the same way with other variables, objects and structures aswell.
But instead of putting numbers in a comma seperated list, in between curly brackets, you list the values, this would be objects for objects and structures.. same principle though.

Servo servos[] = {Servo(), Servo(), Servo()}; //create 3 instances of the servo class

That is a very valid declaration, what bothers me is why you didn't thought of that? :-/
No offense, but you allready had the answer to your question yourself :sunglasses:

Thank you very much!
I simply didn't know that you can put variables inside an array :slight_smile:
you were a great help :wink:

darn...
I thought I did understand it but seems I did not.
I did this and got this error code:

servo_[] = {servo_1, servo_2, servo_3, servo_4};
servo_[] = {0, min_1, min_2, min_3, min_4};
max_[] = {0, max_1, max_2, max_3, max_4};
speed_[] = {0, speed_1, speed_2, speed_3, speed_4};
error: expected constructor, destructor, or type conversion before '=' token

please don't kill me :-[

There isn't enough to go on there, please post the whole sketch
You haven't given your arrays a type, and you've got two servo arrays

#include <Servo.h>

Servo servo_1;            // create servo object to control a servo 
Servo servo_2;
Servo servo_3;
Servo servo_4;

int pos_1;                             // variable to store the servo position 
int pos_2;
int pos_3;
int pos_4;


unsigned long previousMillis_1 = 0;
unsigned long previousMicros_1 = 0;      // the variables is a long because the time, measured in miliseconds,
unsigned long previousMicros_2 = 0;      // will quickly become a bigger number than can be stored in an int.
unsigned long previousMicros_3 = 0;
unsigned long previousMicros_4 = 0;      // will store last time servo was updated

int servospeed = 900;



long interval = 1000; 
const int ledPin =  13;      // the number of the LED pin                    
int ledState = LOW; 


[glow]servo_[] = {0, servo_1, servo_2, servo_3, servo_4};[/glow]
min_[] = {0, min_1, min_2, min_3, min_4};
max_[] = {0, max_1, max_2, max_3, max_4};
speed_[] = {0, speed_1, speed_2, speed_3, speed_4};


//---------------------------| SERVO_01 |-----------------------------------------------

void ServoX(int num, int min_1, int max_1, int speed_1)
{
  if ((micros()-previousMicros_1) >=speed_[num])  {
    previousMicros_1 = micros();    
      
    if (pos_1 > servo_[num].readMicroseconds())  { 
      servo_[num].writeMicroseconds(servo_[num].readMicroseconds() + 1);
    }     
    if (pos_1 < servo_[num].readMicroseconds())  {
      servo_[num].writeMicroseconds(servo_[num].readMicroseconds() - 1);
    }
    if(servo_1.readMicroseconds() == max_[num])  {
      pos_1 = min_[num];
    }    
    if(servo_1.readMicroseconds() <= min_[num])  {
      pos_1 = max_[num];
    }     
  }  
}










void setup() 
{
  servo_1.attach(9);                   // attaches the servo on pin 9 to the servo object
  servo_2.attach(10);
  servo_3.attach(11);
  servo_4.attach(12);

  pinMode(ledPin, OUTPUT);  
  Serial.begin(9600);
}

void loop()
{

  ServoX(1,700,2200,1000);
  
 
  
  
  if (millis() - previousMillis_1 > interval)  {
    // save the last time you blinked the LED 
    previousMillis_1 = millis();   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)  {
      ledState = HIGH;
    }
    else  {
      ledState = LOW;
    }
      
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
    
    Serial.println(millis());
    Serial.println();
  }
}
error: expected constructor, destructor, or type conversion before '=' token In function 'void ServoX(int, int, int, int)':
Servo servo_[] = {0, servo_1, servo_2, servo_3, servo_4};

I think your other arrays should be "int".

Why don't you have an entry for servo [0]?

So I changed it so that there is no error anymore. the "0" was just so that I can type for the servos 1,2,3,4 instead of 0,1,2,3 but I erased that.

the yellow marked places are the changed ones. I tried to but the ints into the arry but that didn't seem to work. It then says expected primary expression before int.

#include <Servo.h>

[glow]Servo servo_1;            // create servo object to control a servo 
Servo servo_2;
Servo servo_3;
Servo servo_4;

int min_1;
int  min_2;
int min_3; 
int min_4;

int max_1;
int max_2;
int max_3;
int max_4;

int speed_1;
int speed_2;
int speed_3; 
int speed_4;

int pos_1;                             // variable to store the servo position 
int pos_2;
int pos_3;
int pos_4;[/glow]


unsigned long previousMillis_1 = 0;
unsigned long previousMicros_1 = 0;      // the variables is a long because the time, measured in miliseconds,
unsigned long previousMicros_2 = 0;      // will quickly become a bigger number than can be stored in an int.
unsigned long previousMicros_3 = 0;
unsigned long previousMicros_4 = 0;      // will store last time servo was updated

int servospeed = 900;



long interval = 1000; 
const int ledPin =  13;      // the number of the LED pin                    
int ledState = LOW; 


[glow]Servo servo_[] = {servo_1, servo_2, servo_3, servo_4};
int min_[] = {min_1, min_2, min_3, min_4};
int max_[] = {max_1, max_2, max_3, max_4};
int speed_[] = {speed_1, speed_2, speed_3, speed_4};
[/glow]

//---------------------------| SERVO_01 |-----------------------------------------------

void ServoX(int num, int min_1, int max_1, int speed_1)
{
  if ((micros()-previousMicros_1) >= speed_[num])  {
    previousMicros_1 = micros();    
      
    if (pos_1 > servo_[num].readMicroseconds())  { 
      servo_[num].writeMicroseconds(servo_[num].readMicroseconds() + 1);
    }     
    if (pos_1 < servo_[num].readMicroseconds())  {
      servo_[num].writeMicroseconds(servo_[num].readMicroseconds() - 1);
    }
    if(servo_1.readMicroseconds() == max_[num])  {
      pos_1 = min_[num];
    }    
    if(servo_1.readMicroseconds() <= min_[num])  {
      pos_1 = max_[num];
    }     
  }  
}










void setup() 
{
  servo_1.attach(9);                   // attaches the servo on pin 9 to the servo object
  servo_2.attach(10);
  servo_3.attach(11);
  servo_4.attach(12);

  pinMode(ledPin, OUTPUT);  
  Serial.begin(9600);
}

void loop()
{

  ServoX(1,700,2200,1000);
  
 
  
  
  if (millis() - previousMillis_1 > interval)  {
    // save the last time you blinked the LED 
    previousMillis_1 = millis();   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)  {
      ledState = HIGH;
    }
    else  {
      ledState = LOW;
    }
      
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
    
    Serial.println(millis());
    Serial.println();
  }
}

You have no "pos" array, and you still have references to "servo_1".
All those copies of "int"s (max_1, min_1 etc) in your arrays are pointless.

here is a fragment showing how to declare and use an array of servos

 #include <Servo.h>

const int servoCount = 4;

Servo servos[servoCount];
const int pins[servoCount] = {9,10,11,12};  

void setup()  {
 for(int i=0; i < servoCount; i++)
    servos[i].attach(pins[i]);    
} 

void writeServo(int index, int value) {
   servos[index].write(value); 
}

void loop() {
   writeServo(0, 90); // write a value of 90 to the first servo
   delay(20);
}