0% found this document useful (0 votes)
5 views

code

The document contains Arduino code for controlling a robot using infrared sensors and a motor driver. It defines pin assignments for the sensors and motors, sets up the pins in the setup function, and implements logic in the loop function to read sensor values and control the robot's movement. The robot can move forward, turn left, turn right, or stop based on the sensor readings.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

code

The document contains Arduino code for controlling a robot using infrared sensors and a motor driver. It defines pin assignments for the sensors and motors, sets up the pins in the setup function, and implements logic in the loop function to read sensor values and control the robot's movement. The robot can move forward, turn left, turn right, or stop based on the sensor readings.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

// Les pins capteur infrarouge

#define OUT1 A4
#define OUT2 A3
#define OUT3 A2
#define OUT4 A1
#define OUT5 A0

// Les pins motor driver


#define IN1 10
#define IN2 9
#define IN3 5
#define IN4 4
#define ENA 3
#define ENB 6

void setup() {
// Capteur infrarouge
pinMode(OUT1, INPUT);
pinMode(OUT2, INPUT);
pinMode(OUT3, INPUT);
pinMode(OUT4, INPUT);
pinMode(OUT5, INPUT);

// Motor Driver
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
}

void loop() {
// Lecture des capteurs
int r1 = digitalRead(OUT1);
int r2 = digitalRead(OUT2);
int r3 = digitalRead(OUT3);
int r4 = digitalRead(OUT4);
int r5 = digitalRead(OUT5);

if ((r1 == 1) && (r2 == 1) && (r3 == 0) && (r4 == 1) && (r5 == 1)) {
avant();
}
if ((r1 == 0) && (r2 == 1) && (r3 == 1) && (r4 == 1) && (r5 == 1)) {
tourner_a_gauche();
}
if ((r1 == 1) && (r2 == 0) && (r3 == 1) && (r4 == 1) && (r5 == 1)) {
tourner_a_gauche();
}
if ((r1 == 1) && (r2 == 1) && (r3 == 1) && (r4 == 0) && (r5 == 1)) {
tourner_a_droite();
}
if ((r1 == 1) && (r2 == 1) && (r3 == 1) && (r4 == 1) && (r5 == 0)) {
tourner_a_droite();
}
if ((r1 == 0) && (r2 == 0) && (r3 == 1) && (r4 == 1) && (r5 == 1)) {
tourner_a_gauche();
}
if ((r1 == 1) && (r2 == 0) && (r3 == 0) && (r4 == 1) && (r5 == 1)) {
tourner_a_gauche();
}
if ((r1 == 1) && (r2 == 1) && (r3 == 0) && (r4 == 0) && (r5 == 1)) {
tourner_a_droite();
}
if ((r1 == 1) && (r2 == 1) && (r3 == 0) && (r4 == 1) && (r5 == 0)) {
tourner_a_droite();
}
if ((r1 == 0) && (r2 == 0) && (r3 == 0) && (r4 == 0) && (r5 == 0)) {
arret();
}
}

// Création des fonctions


void avant() {
analogWrite(ENA, 100);
analogWrite(ENB, 100);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}

void tourner_a_gauche() {
analogWrite(ENA, 100);
analogWrite(ENB, 100);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}

void tourner_a_droite() {
analogWrite(ENA, 100);
analogWrite(ENB, 100);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}

void arret() {
analogWrite(ENA, 0);
analogWrite(ENB, 0);
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}

You might also like