Bonjour a tous , j'ai récemment construit un petit bras articulé avec 4 servo et ma carte ArduinoMega. J'avais tout dabor ecrit un code pour commander seulement 2 servo (avec 4 interrupteurs) qui a parfaitement fonctionné puis je l'ai "upgrader" pour 3 servo (avec 6 interrupteurs ) ce qui me donna un code avec beaucoup de répétitions et dur a amélioré.
Voici le code de base :
#include <Servo.h>
Servo myservo1; // create servo object to control a servo
Servo myservo2;
Servo myservo3;
int servoPos3;
int servoPos1; // create servo possition variable
int servoPos2;
int button1 = 22; // create buttons object
int button2 = 23;
int button3 = 24;
int button4 = 25;
int button5 = 26;
int button6 = 27;
int buttonState1 = 0; // create buttons state
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;
int buttonState6 = 0;
void setup()
{
myservo1.attach(9); //attach the servos
myservo2.attach(10);
myservo3.attach(11);
pinMode(button1, INPUT); //set the 4 switch
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
pinMode(button5, INPUT);
pinMode(button6, INPUT);
digitalWrite(button1, HIGH); // pull-up on the switch
digitalWrite(button2, HIGH);
digitalWrite(button3, HIGH);
digitalWrite(button4, HIGH);
digitalWrite(button5, HIGH);
digitalWrite(button6, HIGH);
}
void loop()
{
//myservo1.write(180/2); // set the servo in the middle
//myservo2.write(180/2);
buttonState1 = digitalRead(button1); // set the buttons pressure
buttonState2 = digitalRead(button2);
buttonState3 = digitalRead(button3);
buttonState4 = digitalRead(button4);
buttonState5 = digitalRead(button5);
buttonState6 = digitalRead(button6);
if (buttonState1 == HIGH) { servoPos1++; } // read th buttons pressure
if (buttonState2 == HIGH) { servoPos1--; }
if (buttonState3 == HIGH) { servoPos2++; }
if (buttonState4 == HIGH) { servoPos2--; }
if (buttonState5 == HIGH) { servoPos3++; }
if (buttonState6 == HIGH) { servoPos3--; }
if (servoPos1 > 180) { servoPos1 = 180;} //stop the servo1 at the max or min
if (servoPos1 < 0) { servoPos1 = 0; }
if (servoPos2 > 180) { servoPos2 = 180;} //stop the servo2 at the max or min
if (servoPos2 < 0) { servoPos2 = 0; }
if (servoPos3 > 180) { servoPos3 = 180;} //stop the servo2 at the max or min
if (servoPos3 < 0) { servoPos3 = 0; }
myservo1.write(servoPos1);
myservo2.write(servoPos2);
myservo3.write(servoPos3);
delay(15); // waits for the servo to get there
}
J'ai donc changer tout le code pour un code avec des boucle (for)
#include <Servo.h>
int i;
const int nombreServo = 3;
Servo myservo[nombreServo];
int servoPos[nombreServo];
int nServo = 0;
const int nombreButton = 6;
int button[nombreButton];
int buttonState[nombreButton];
void setup()
{
for (i=0;i<nombreServo;i++) //attach the servos
{
myservo[i].attach(9+i);
servoPos[i] = 90;
}
for (i=0;i<nombreButton;i++) //set the switch
{
button[i] = 22+i;
buttonState[i] = 0;
pinMode(button[i], INPUT);
digitalWrite(button[i], HIGH);
}
}
void loop()
{
for (i=0;i<nombreButton;i++) //set the button
{
buttonState[i] = digitalRead(button[i]);
}
for (i=0;i<nombreButton;i+=2) // read the button "+"
{
if (buttonState[i] == HIGH) { servoPos[nServo]++; nServo+=1; }
}
nServo = 0;
for (i=1;i<nombreServo;i+=2) // read the button "-"
{
if (buttonState[i] == HIGH) { servoPos[nServo]--; nServo+=1; }
}
nServo = 0;
for (i=0;i<nombreServo;i++) //stop the servo[i] at the max or min
{
if (servoPos[i] > 180) { servoPos[i] = 180;}
if (servoPos[i] < 0) { servoPos[i] = 0; }
myservo[i].write(servoPos[i]);
}
delay(15); // waits for the servo to get there
}
Mais ce code ne fonctionne pas ! et je ne voit pas où est mon erreur !
Quelqu'un aurait une petite idée ?