Bonjour,
Voilà, mon projet est simple, un vehicule qui se déplace en évitant les obstacles, c'est je pense un bon exercice pour un début.
Alors mon premier projet était simple, j'ai même reussi à faire le code sans pomper ailleurs, j'ai passé du temps mais je suis satisfait.
Lorsque que le véhicule "rencontrait" un obstacle il l'évitait en tournant à gauche.
Mais maintenant j'ai eu l'idée de le faire évoluer, j'ai mis le detecteur de distance sur un servo, le servo à 3 positions de mesure, en fonction de la mesure (distance obstacle inferieur à 10cm) il change de trajectoire en fonction de la position du cerveau, malheureusement ça ne marche pas, je vous poste mon code (qui doit pas être optimisé)...
Donc si quelqu'un pouvait me filer un conseil... ça fait depuis 15h00 que je suis dessus:
#include "Ultrasonic.h"
#include <Servo.h>
const int Trig = 7;
const int Echo = 6;
long cm;
Ultrasonic HCSR04(Trig,Echo);
int motor1Pin1 = 8; // MOTEUR GAUCHE
int motor1Pin2 = 9; // MOTEUR DROIT
int enablePin1 = 2; // COMMANDE GAUCHE
int motor2Pin4 = 10; // MOTEUR DROIT
int motor2Pin3 = 11; // MOTEUR DROIT
int enablePin2 = 3; // COMMANDE DROITE
Servo servoexplo;
void setup() {
Serial.begin(9600);
// déclaration des sorties
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(enablePin1, OUTPUT);
pinMode(motor2Pin4, OUTPUT);
pinMode(motor2Pin3, OUTPUT);
pinMode(enablePin2, OUTPUT);
servoexplo.attach(5);
digitalWrite(enablePin1, HIGH);
digitalWrite(enablePin2, HIGH);
}
void loop() {
Serial.println(cm);
servoexplo.write(90);
cm = HCSR04.convert(HCSR04.timing(), 1);
delay(2500);
if (cm < 10) {
digitalWrite(motor1Pin1, LOW); //
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin4, LOW); //
digitalWrite(motor2Pin3, HIGH); //
delay (1800);
}
else {
// Le moteur gauche AVANT
digitalWrite(motor1Pin1, HIGH); //
digitalWrite(motor1Pin2, LOW); //
// Le moteur droit AVANT
digitalWrite(motor2Pin4, LOW); //
digitalWrite(motor2Pin3, HIGH); //
}
servoexplo.write(140);
cm = HCSR04.convert(HCSR04.timing(), 1);
delay(2500);
if (cm < 10) {
digitalWrite(motor1Pin1, HIGH); //
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin4, HIGH); //
digitalWrite(motor2Pin3, LOW); //
delay (1800);
}
else {
// Le moteur gauche AVANT
digitalWrite(motor1Pin1, HIGH); //
digitalWrite(motor1Pin2, LOW); //
// Le moteur droit AVANT
digitalWrite(motor2Pin4, LOW); //
digitalWrite(motor2Pin3, HIGH); //
}
servoexplo.write(30);
cm = HCSR04.convert(HCSR04.timing(), 1);
delay(2500);
if (cm < 10) {
digitalWrite(motor1Pin1, HIGH); //
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin4, HIGH); //
digitalWrite(motor2Pin3, LOW); //
delay (1800);
}
else {
// Le moteur gauche AVANT
digitalWrite(motor1Pin1, HIGH); //
digitalWrite(motor1Pin2, LOW); //
// Le moteur droit AVANT
digitalWrite(motor2Pin4, LOW); //
digitalWrite(motor2Pin3, HIGH); //
}
}