Bonjour,
et voici un nouveau probleme, pour la conception du robot de notre equipe nous avons un bug quand on rajoute les servos moteurs ( 6 au total), du moment que je rajoute servo.h les moteurs ne sont plus commandable par le joystick. J'ai lu pas mal de chose mais pas de solution: Bogue avec la bibliothèque Servo, comment corriger ce problème ??? - #19 by LamiRene.
/* Arduino DC Motor Control - PWM | H-Bridge | L298N
Rajout d'un servo moteur pour simuler la porte etverifier comptabilité avec les moteurs A et B
*/
#include <Servo.h>
#define in1A_MotorLeftA 4
#define in2A_MotorLeftA 5
#define enA_MotorLeftA 6
#define in1B_MotorRightB 7
#define in2B_MotorRightB 8
#define enB_MotorRightB 9
#define PinSwitchLED 13
int Speed_MotorLeftA = 0;
int Speed_MotorRightB = 0;
int SwitchOnLED = HIGH; // HIGH = switch OFF
int TimerLED = 0;
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {
pinMode(enA_MotorLeftA, OUTPUT);
pinMode(enB_MotorRightB, OUTPUT);
pinMode(in1A_MotorLeftA, OUTPUT);
pinMode(in2A_MotorLeftA, OUTPUT);
pinMode(in1B_MotorRightB, OUTPUT);
pinMode(in2B_MotorRightB, OUTPUT);
pinMode(PinSwitchLED, OUTPUT);
pinMode(A0, INPUT);
pinMode(A1, INPUT);
myservo.attach(11); // attaches the servo on pin 9 to the servo object pb des que l'on attache le servo un moteur ne reponds plus
}
void loop() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
int xAxis = analogRead(A0); // Read Joysticks X-axis
int yAxis = analogRead(A1); // Read Joysticks Y-axis
int ySign = 1 ; // Identify if Y is move forward 1 or move backward -1 ; will be used to accomodate speed of the wheels
int UTurnAllowed = 0; // when Y is in neutral position and willing to make U turn without going forward or backgroud ; 0 for no UTurn, 1 for UTurn
// Y-axis used for forward and backward control
if (yAxis < 470) {
// Set Motor Left A backward
digitalWrite(in2A_MotorLeftA, HIGH);
digitalWrite(in1A_MotorLeftA, LOW);
// Set Motor Right B backward
digitalWrite(in1B_MotorRightB, HIGH);
digitalWrite(in2B_MotorRightB, LOW);
// Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
Speed_MotorLeftA = map(yAxis, 470, 0, 0, 255);
Speed_MotorRightB = Speed_MotorLeftA ;
}
else if (yAxis > 550) {
// Set Motor Left A forward
digitalWrite(in1A_MotorLeftA, HIGH);
digitalWrite(in2A_MotorLeftA, LOW);
// Set Motor Right B forward
digitalWrite(in1B_MotorRightB, LOW);
digitalWrite(in2B_MotorRightB, HIGH);
// Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
Speed_MotorLeftA = map(yAxis, 550, 1023, 0, 255);
Speed_MotorRightB = Speed_MotorLeftA ;
}
// If Y axis of the joystick stays in neutral position then allow U Turn and reduce speed to zero
else {
// Autorize U Turn
UTurnAllowed = 1;
//Set PWM to zero
Speed_MotorLeftA = 0;
Speed_MotorRightB = Speed_MotorLeftA ;
}
// Sign of Y will be -1 if yAxis < 512 otherwise Sign of Y will be +1 (default value)
if (yAxis < 512) {
ySign = -1 ;
}
// X-axis used for left and right control
if (xAxis > 650) { // Go to the left
// Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value
int xMapped = map(xAxis, 650, 1023, 0, 255);
if (UTurnAllowed == 1) { // Means Y is in neutral position
Speed_MotorLeftA = xMapped ;
Speed_MotorRightB = xMapped ;
// Set Motor Left A backward
digitalWrite(in2A_MotorLeftA, HIGH);
digitalWrite(in1A_MotorLeftA, LOW);
// Set Motor Right B forward
digitalWrite(in1B_MotorRightB, LOW);
digitalWrite(in2B_MotorRightB, HIGH);
}
else {
// if ySign>0 THEN Move to left forward - decrease left motor speed, increase right motor speed
// if ySign<0 THEN Move right backward - decrease right motor speed, increase left motor speed
Speed_MotorLeftA = Speed_MotorLeftA - xMapped*ySign;
Speed_MotorRightB = Speed_MotorRightB + xMapped*ySign;
}
}
if (xAxis < 370) { // Go to the right
// Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value
int xMapped = map(xAxis, 370, 0, 0, 255);
if (UTurnAllowed == 1) { // Means Y is in neutral position
Speed_MotorLeftA = xMapped;
Speed_MotorRightB = xMapped;
// Set Motor Left A forward
digitalWrite(in2A_MotorLeftA, LOW);
digitalWrite(in1A_MotorLeftA, HIGH);
// Set Motor Right B backward
digitalWrite(in1B_MotorRightB, HIGH); //////HIGH
digitalWrite(in2B_MotorRightB, LOW);
}
else {
// if ySign>0 THEN Move right forward - decrease right motor speed, increase left motor speed
// if ySign<0 THEN Move to left backward - decrease left motor speed, increase right motor speed
Speed_MotorLeftA = Speed_MotorLeftA + xMapped*ySign;
Speed_MotorRightB = Speed_MotorRightB - xMapped*ySign;
}
}
// Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70)
// Confine the range from Min 70 to Max 255
if (Speed_MotorLeftA > 200) {
Speed_MotorLeftA = 200;
}
if (Speed_MotorLeftA < 70) {
Speed_MotorLeftA = 0;
}
if (Speed_MotorRightB > 200) {
Speed_MotorRightB = 200;
}
if (Speed_MotorRightB < 70) {
Speed_MotorRightB = 0;
}
// Switch On LEDs
if ((Speed_MotorLeftA > 100) || (Speed_MotorRightB > 100)) {
TimerLED = 10000 ;
}
else {
TimerLED = TimerLED - 1;
}
if (TimerLED > 0) {
SwitchOnLED = LOW; // LOW = switch ON
}
else {
SwitchOnLED = HIGH; // HIGH = switch OFF
TimerLED = 0;
}
digitalWrite(PinSwitchLED, SwitchOnLED);
// Send PWM signals
analogWrite(enA_MotorLeftA, Speed_MotorLeftA); // Send PWM signal to Motor Left A
analogWrite(enB_MotorRightB, Speed_MotorRightB); // Send PWM signal to Motor Right B
}



