Hi,
I wrote a code for driving multible servos. Now I want to make it more compact. I am sure it is possible but I have no idea how...
I want to be able to change the variables into an other Variable so that I need just one funktion istead of (in this case) two. Later I want to have four servos, each servo will get a diffrent max-min-position and speed over I²C .
This is the main part of the code I have right now ( it is not fuinished yet,more something like a test trial):
#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
long interval = 1000;
const int ledPin = 13; // the number of the LED pin
int ledState = LOW;
void Servo_01(int Min_1, int Max_1, int servospeed_1)
{
if ((micros()-previousMicros_1) >=servospeed_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;
}
}
}
void Servo_02(int Min_2, int Max_2, int servospeed_2)
{
if ((micros()-previousMicros_2) >=servospeed_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;
}
}
}
void setup()
{
servo_1.attach(9, 554, 2400); // 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()
{
Servo_01(554, 2400, 500);
Servo_02(800, 2000, 1000);
//-------------------------------------------------------------------------------- not important
if( (millis() % 1000) == 0 ) {
Serial.println(millis());
Serial.println("A second has passed");
}
//-------------------------------------------------------------------------------
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();
}
}
I want something like that:
void Servo(int Servo_X, int Min_X, int Max_X, int servospeed_X)
{........}
void loop()
{
Servo(1,Min_X,Max_X,servospeed_X); // these will get their values from the I²C network
Servo(2,Min_X,Max_X,servospeed_X);
....
}
To make that work servo_1 needs to change into servo_2, pos_1 into pos_2 and so on.
Is there a easy way to do that or is it to complicate?
I hope I explained my problem well enouth.
best regards,
Florian