Merci du coup de main
La partie indiquant que l'on est très loin ne m'intéresse pas
J'ai modifie les lignes 42 à 44 pour ajouter la variable 'cm' dont je me sers mais j'ai un doute
distance = duration / 58.2;
cm = distance;
#include <Servo.h>
//https://www.youtube.com/watch?v=eKcKdMb_nkQ&feature=youtu.be
// Pins
#define TRIG_PIN A0
#define ECHO_PIN A1
//Define all the connections maps to the L298N pour faire le pont en H pour inversion des rotations
#define enA 11 // ou 13 avant la mise à jour partie 4
#define in1 12
#define in2 13 // ou 11 avant la mise à jour partie 4
#define in3 7
#define in4 6
#define enB 5
#define servoPin 2
/*
Si vous voulez contrôler les vitesses du moteur, plaire l'échange épingle(coince) 11 et épingle(coince) 13
rapports(connexions), aussi bien que met à jour la section se définissant dans le code.
L'épingle 13 n'est pas PWM l'épingle mais l'épingle 11 est. Désolé de l'erreur.
*/
class Motor {
int enablePin;
int directionPin1; // sens de rotation du moteur
int directionPin2; // sens de rotation inverse du moteur
public:
//Method to define the motor pins
Motor(int ENPin, int dPin1, int dPin2) {
enablePin = ENPin;
directionPin1 = dPin1;
directionPin2 = dPin2;
};
//Method to drive the motor 0~255 driving forward. -1~-255 driving backward
void Drive(int speed) {
if (speed >= 0) {
digitalWrite(directionPin1, LOW);
digitalWrite(directionPin2, HIGH); // vitesse positive, on avance
}
else {
digitalWrite(directionPin1, HIGH); // vitesse négtive, on recule
digitalWrite(directionPin2, LOW);
speed = - speed;
}
analogWrite(enablePin, speed);
}
};
Motor leftMotor = Motor(enA, in1, in2); // les pin du pont en H pour le moteur gauche
Motor rightMotor = Motor(enB, in3, in4);// les pin du pont en H pour le moteur droit
Servo myservo; // create servo object to control a servo
void motorInitiation() {
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
// Set initial direction and speed
digitalWrite(enA, LOW);
digitalWrite(enB, LOW);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
}
// Anything over 400 cm (23200 us pulse) is "out of range"
const unsigned int MAX_DIST = 23200;
bool ObsticalAhead = false;
int servoPos = 90;
enum Directions { Forward, TurnLeft, TurnRight, TurnAround, Brake};
Directions nextStep = Forward;
unsigned long t1;
unsigned long t2;
unsigned long pulse_width;
float cm;
float inches;
void setup() {
// The Trigger pin will tell the sensor to range find
pinMode(TRIG_PIN, OUTPUT);
digitalWrite(TRIG_PIN, LOW);
// We'll use the serial monitor to view the sensor output
Serial.begin(9600);
myservo.attach(servoPin);
motorInitiation();
Directions nextStep = Forward;
}
void loop() {
// on regarde la distance, puis on choisit la direction et on roule
checkDistance()
if (tempsEcoule < 600000) checkDirection(); // 10 mn * 60 * 1 000
drive();
else if (tempsEcoule < 1200000) checkDirectionVersBordure(); // 20 mn * 60 * 1 000
drive();
else if (tempsEcouleAvantPause > 1800000) pause(); // 30 mn * 60 * 1 000 après 30 mn de fonctionnement mise en pause de 10 mn
else startMillis = millis();
delay (100);
}
void pause () { // utilisation du second chrono pour arrété au bout de 30 mn
delay (600000); // 10 mn soit 10 *60*1000(milli secondes)
startMillisPause = millis(); // <-- second chrono ici
startMillis = millis(); // <-- remise à 0 du premier chrono
}
void checkDistance() {
// Hold the trigger pin high for at least 10 us
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Wait for pulse on echo pin
while ( digitalRead(ECHO_PIN) == 0 );
// Measure how long the echo pin was held high (pulse width)
// Note: the micros() counter will overflow after ~70 min
t1 = micros();
while ( digitalRead(ECHO_PIN) == 1);
t2 = micros();
pulse_width = t2 - t1;
// Calculate distance in centimeters and inches. The constants
// are found in the datasheet, and calculated from the assumed speed
//of sound in air at sea level (~340 m/s).
cm = pulse_width / 58.0;
inches = pulse_width / 148.0;
// Print out results
if ( pulse_width > MAX_DIST ) {
//Serial.println("Out of range");
} else {
//Serial.print(cm);
//Serial.print(" cm \t");
//Serial.print(inches);
//Serial.println(" in");
}
// Wait at least 60ms before next measurement
delay(60);
if (cm <= 20) { // booléenne
ObsticalAhead = true;
Serial.println("Problem Ahead");
}
else {
ObsticalAhead = false;
}
}
void checkDirection() {
Serial.println("checking direction");
if (ObsticalAhead == true) { // on a un obstacle devant, on coupe les moteurs et on regarde à gauche en premier
nextStep = Brake;
drive();
myservo.write(180); // servo va à 180° pour la gauche
delay(400); // attente 15ms pour que le servo aille à la position
checkDistance();
// là on regarde la gauche
if (ObsticalAhead == false) { // si ObstacleDevant est faux donc si le coté gauche est libre, on y va en tournant à gauche
nextStep = TurnLeft;
Serial.println("Next step is TurnLeft");
myservo.write(90);//RAZ de la position du servo
delay(400);
}
else { // à gauche, c'est bloqué. On doit regarder à droite
myservo.write(0); // servo va à 0° pour la droite
delay(800); // attente 15ms pour que le servo aille à la position
checkDistance();
// là on regarde la droite
if (ObsticalAhead == false) { // si ObstacleDevant est faux donc si le coté droit est libre, on y va en tournant à droite
nextStep = TurnRight;
Serial.println("Next step is TurnRight");
myservo.write(90);//RAZ de la position du servo
delay(400);
}
else { //Alors ObstacleDevant est vrai, on doit tourner autour
nextStep = TurnAround;
myservo.write(90);//RAZ de la position du servo
delay(300);
Serial.println("Next step is TurnAround");
}
}
}
else {
nextStep = Forward; //Pas d'obstacle devant , on avance
}
}
void checkDirectionVersBordure () {
Serial.println("checking direction");
if (ObsticalAhead == true) { // on a un bord devant, on s'arrête et on regarde à gauche
nextStep = Brake;
drive();
myservo.write(180); // servo va à 180° pour la gauche
delay(400); // attente 15ms pour que le servo aille à la position
checkDistance();
// là on regarde la gauche
if (ObsticalAhead == true) { // si ObstacleDevant est vrai donc si le coté gauche détecté, on s'y dirige
nextStep = TurnLeft;
Serial.println("Next step is TurnLeft");
myservo.write(90);//RAZ de la position du servo
delay(400);
}
else { // à gauche, pas de bord. On doit regarder à droite
myservo.write(0); // servo va à 0° pour la droite
delay(800); // attente 15ms pour que le servo aille à la position
checkDistance();
// là on regarde la droite
if (ObsticalAhead == true) { // si ObstacleDevant est vrai donc si le coté gauche détecté, on s'y dirige
nextStep = TurnRight;
Serial.println("Next step is TurnRight");
myservo.write(90);//RAZ de la position du servo
delay(400);
}
else { //Alors ObstacleDevant est vrai, on doit tourner autour, bouée dans la piscine par exemple
nextStep = TurnAround;
myservo.write(90);//RAZ de la position du servo
delay(300);
Serial.println("Next step is on evite");
}
}
}
else {
nextStep = Forward; //Pas d'obstacle devant , on avance
}
}
void drive() {
switch (nextStep) {
case Forward:
leftMotor.Drive(255);
rightMotor.Drive(255);
Serial.println("Forward");
break;
case TurnLeft:
leftMotor.Drive(-255);
rightMotor.Drive(255);
Serial.println(" TurnLeft");
delay(400);
break;
case TurnRight:
leftMotor.Drive(255);
rightMotor.Drive(-255);
Serial.println(" TurnRight");
delay(400);
break;
case TurnAround:
leftMotor.Drive(255);
rightMotor.Drive(-255);
Serial.println(" TurnAround");
delay(600);
break;
case Brake:
leftMotor.Drive(0);
rightMotor.Drive(0);
Serial.println(" stopped");
}
}