Bonjour gilbertlevoyant
J'ai essayé ton programme, dans le fond il va très bien.
Pour ce qui est de l'arrêt des moteurs, il n'y avait rien pour, je t'ai ajouté motorsStop()
Je t'ai ajouté plein de Serial.println("
afin que tu "voies" fonctionner ton programme, mets ta console à 115200 afin de voir les textes:
Moteur B ARRIERE 113
Moteurs STOP
Moteur A AVANT 36
Moteur B ARRIERE 115
Moteurs STOP
Moteur A AVANT 36
Moteur B ARRIERE 115
Moteurs STOP
Moteur A AVANT 36
Moteur B ARRIERE 117
Moteurs STOP
Moteur A AVANT 36
Moteur B ARRIERE 117
Moteurs STOP
Moteur A AVANT 36
Moteur B ARRIERE 117
Moteurs STOP
Pour ce qui est du sens de rotation de tes moteurs, 2 méthodes:
Jouer avec les valeurs HIGH et LOW en les croisant:
if (motorSpeedA >= 0) {
digitalWrite(motorPinA1, HIGH);
digitalWrite(motorPinA2, LOW);
} else {
digitalWrite(motorPinA1, LOW);
digitalWrite(motorPinA2, HIGH);
motorSpeedA = -motorSpeedA;
}
ou croiser les fils de tes moteurs.
Le programme:
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX, TX
int xAxis, yAxis;
int motorPinA1 = 2; // Broche de contrôle du moteur A1
int motorPinA2 = 3; // Broche de contrôle du moteur A2
int motorPinB1 = 4; // Broche de contrôle du moteur B1
int motorPinB2 = 5; // Broche de contrôle du moteur B2
int enA = 6; // Broche de PWM pour le moteur A
int enB = 7; // Broche de PWM pour le moteur B
boolean motorsRunning = false; // Moteurs pas en marche
void setup() {
pinMode(motorPinA1, OUTPUT);
pinMode(motorPinA2, OUTPUT);
pinMode(motorPinB1, OUTPUT);
pinMode(motorPinB2, OUTPUT);
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
// Initialiser la communication série avec le module Bluetooth
Serial.begin(115200);
BTSerial.begin(9600);
}
void loop() {
// Lire les valeurs du joystick
if (BTSerial.available() >= 2) {
xAxis = BTSerial.read();
yAxis = BTSerial.read();
// Convertir les valeurs du joystick en valeurs de moteur
int motorSpeedA = map(yAxis, 0, 254, -255, 255);
int motorSpeedB = map(xAxis, 0, 254, -255, 255);
// Contrôler la direction et la vitesse du moteur A
Serial.print("Moteur A");
if (motorSpeedA >= 0) {
digitalWrite(motorPinA1, HIGH);
digitalWrite(motorPinA2, LOW);
Serial.print(" AVANT ");
} else {
digitalWrite(motorPinA1, LOW);
digitalWrite(motorPinA2, HIGH);
Serial.print(" ARRIERE ");
motorSpeedA = -motorSpeedA;
}
// Contrôler la vitesse du moteur A avec PWM
Serial.println(motorSpeedA);
analogWrite(enA, motorSpeedA);
// Contrôler la direction et la vitesse du moteur B
Serial.print("Moteur B");
if (motorSpeedB >= 0) {
digitalWrite(motorPinB1, HIGH);
digitalWrite(motorPinB2, LOW);
Serial.print(" AVANT ");
} else {
digitalWrite(motorPinB1, LOW);
digitalWrite(motorPinB2, HIGH);
Serial.print(" ARRIERE ");
motorSpeedB = -motorSpeedB;
}
// Contrôler la vitesse du moteur B avec PWM
Serial.println(motorSpeedB);
analogWrite(enB, motorSpeedB);
motorsRunning = true; // Moteurs en marche
}
else // Si pas de communications arrêt des moteurs
{
if (motorsRunning) // Si moteurs en marche
{
motorsStop();
}
}
}
void motorsStop()
{
Serial.println("Moteurs STOP");
digitalWrite(motorPinA1, LOW);
digitalWrite(motorPinA2, LOW);
analogWrite(enA, 0);
digitalWrite(motorPinB1, LOW);
digitalWrite(motorPinB2, LOW);
analogWrite(enB, 0);
motorsRunning = false; // Moteurs pas en marche
}
Ne te gêne pas pour poser des questions 
A+
Cordialement
jpbbricole