1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| //Broche d'entrées analogique
int ind_g = A0; // inductance gauche
int ind_d = A1; // inductance droite
int sig_g; // signal gauche out ampli
int sig_d; // signal droite out ampli
//Broches alim. moteur
int EN_A = 11; //Broche d'activation moteur gauche
int IN1 = 9; //Broche de contrôle moteur gauche
int IN2 = 8; //Broche de contrôle moteur gauche
int IN3 = 7; //Broche de contrôle moteur droit
int IN4 = 6; //Broche de contrôle moteur droit
int EN_B = 10; //Broche d'activation moteur droit
//Initialisation de variables pour stocker des données
int moteur_g; //sortie analogique moteur gauche
int moteur_d; //sortie analogique moteur droit
int vit_max; //vitesse maximum de déplacement
int vit_max2; //vitesse maximum de déplacement - 1
void setup ( ) {
Serial.begin (9600); //Démarrage de la communication série à un débit de 9600 bauds
//Initialisation des broches du moteur en sortie
pinMode(EN_A, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(EN_B, OUTPUT);
//Initialisation des broches d'entrées analogique
pinMode (ind_g, INPUT) ;
pinMode (ind_d, INPUT) ;
}
void loop () {
sig_g = analogRead (ind_g) ; //Entrée capteur gauche
sig_d = analogRead (ind_d) ; //Entrée capteur droit
vit_max = 150; //valeur de la vitesse maximum de déplacement
vit_max2 = (vit_max-1); //valeur de la vitesse de déplacement maximale - 1
// CONTRÔLE MOTEUR GAUCHE
if (sig_g > 400) { // marche pleine puissance moteur gauche
delay(10);
moteur_g = (vit_max);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
analogWrite(EN_A, moteur_g); //activation moteur gauche
}
if (sig_g >= 50 and sig_g <= 380) {
moteur_g = map(sig_g, 50, 390, 0, vit_max2); //Mapper les valeurs sur 0 a vitesse maximale pour le moteur gauche
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
analogWrite(EN_A, moteur_g); //activation moteur gauche
}
if (sig_g < 50) { // arrêt complet du moteur gauche
delay(10);
moteur_g = (0);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
analogWrite(EN_A, moteur_g); //activation moteur gauche
}
// CONTRÔLE MOTEUR DROIT
if (sig_d > 400){ //marche pleine puissance moteur droit
delay(10);
moteur_d = (vit_max);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
analogWrite(EN_B, moteur_d); //activation moteur droit
}
if (sig_d >= 50 and sig_d <= 380) {
moteur_d = map(sig_d, 50, 390, 0, vit_max2); //Mapper les valeurs sur 0 a vitesse maximale pour le moteur droit
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
analogWrite(EN_B, moteur_d); //activation moteur droit
Serial.println(vit_max2);
}
if (sig_d < 50){ //arrêt complet moteur droit
delay(10);
moteur_d = (0);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
analogWrite(EN_B, moteur_d); //activation moteur droit
}
} |
Partager