et la première partie du code
#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
/*
Update: If you want to control the motor speeds, please swap pin 11 and pin 13 connections,
as well as update the define section in the code. Pin 13 is not PWM pin but pin 11 is.
sorry for the mistake.
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);
}
//Variables--------------------------------------------------------------------------
// 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;
//SETUP--------------------------------------------------------------------------
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() {
// Variable globale pour faire le décompte du temps
unsigned long startMillisTempsEcoule; // à 0 par défaut pour le chrono de checkDirection()( MVT normal) suivi de checkDirectionVersBordure()( mvt vers les bords)
unsigned long startMillisTempsEcouleAvantPause ; // à 0 par défaut pour le chrono des 30 mn
// on regarde le temps, puis on choisit la direction et on roule
if (startMillisTempsEcoule < 600000) {
checkDirection(); // 10 mn * 60 * 1 000
drive();
}
else if (startMillisTempsEcoule < 1200000) {
checkDirectionVersBordure(); // 20 mn * 60 * 1 000
drive();
}
else if (startMillisTempsEcouleAvantPause > 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");
}
}
}
}