0% found this document useful (0 votes)
13 views2 pages

Configuration Capteur Infrarouge Arduino

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)
13 views2 pages

Configuration Capteur Infrarouge Arduino

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

// 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);
}

Common questions

Powered by AI

Defining custom functions like avant(), tourner_a_gauche(), and arret() facilitates modularity and readability in the code, simplifying complex commands to single calls. This abstraction allows for reusing code blocks without rewriting them, making maintenance and debugging more efficient. It also improves code clarity, with each function encapsulating specific behavior—forward movement, left turn, or stopping—streamlining the process of altering or extending functionality. Such practices are integral in developing scalable and accessible robotic applications .

Setting pins as INPUT or OUTPUT in the setup function is crucial for defining how each pin will interact with the circuit. INPUT pins read data from sensors or external signals, crucial for making informed decisions within the program based on sensor readings. OUTPUT pins send data to actuators like LEDs or motors to execute commands. Incorrectly setting a pin can lead to failed operations or unpredictable behavior. In this program, the appropriate configuration ensures the robot correctly interprets sensor inputs and sends accurate control signals to the motors .

Incorrect speed settings using analogWrite can lead to several risks. Setting the PWM signal too high can overload the motor, causing overheating or damage over time. If set too low, the robot might not have sufficient power to move, leading to failure in operation. In precise movement scenarios, like line-following, incorrect speeds can result in unstable movement or difficulty in following lines accurately, potentially causing the robot to deviate from its path, collide with obstacles, or fail to stop promptly, increasing wear on mechanical components .

Adding more sensors would enhance the robot's ability to gather environmental data, increasing its navigation accuracy and reliability. Additional sensors can provide finer granularity in detecting path deviations, enabling more precise adjustments and smoother turns. Increased sensor coverage can help detect obstacles earlier, improving safety and preventing collisions. Moreover, more data points for the program might allow implementing more advanced algorithms, like a PID controller, to refine movements further and handle diverse environments robustly .

Reversing the roles of motor driver pins in the loop function could lead to opposite behavior for each command, such as moving backward instead of forward or turning in the incorrect direction. For instance, if IN1 and IN2, traditionally responsible for a specific motor direction, are reversed, the corresponding motor would operate in reverse direction while the code attempts forward movement. Such misconfiguration can disrupt expected robot navigation, potentially leading to crashes or navigation errors, thereby highlighting the importance of accurate pin assignments .

The program defines four functions—avant(), tourner_a_gauche(), tourner_a_droite(), and arret(). The avant() function makes the robot move forward by setting both motors to drive forward. The tourner_a_gauche() function makes the robot turn left by setting the left motor to reverse and the right motor to drive forward. The tourner_a_droite() function makes the robot turn right by setting the right motor to reverse and the left motor to drive forward. The arret() function stops the robot by deactivating both motors. Each function uses analogWrite to control motor speed and digitalWrite to control motor direction via the motor driver pins .

Infrared sensors are used to detect the presence of objects or changes in light to control the robot's movements. In the given program, the infrared sensor pins (OUT1 to OUT5) are set as INPUT, which means they read signals from the environment to determine the robot's path. The motor driver pins (IN1, IN2, IN3, IN4, ENA, and ENB) are set as OUTPUT, allowing the program to control the direction and speed of the robot's motors. The program uses readings from the sensors to execute movement commands like moving forward, turning left or right, and stopping by manipulating the motor driver outputs .

The condition-based logic in the loop function reads inputs from the infrared sensors to determine the robot's position relative to a line. Each set of conditions specifies which sensors detect the line (or its absence), triggering a corresponding movement function. For example, if sensors read (1,1,0,1,1), meaning OUT3 does not detect the line, the robot moves forward. Other combinations like (0,1,1,1,1) or (1,1,1,0,1) instruct the robot to turn left or right respectively. This logic allows the robot to adapt its movements based on sensory input, ensuring it follows a predefined path reliably .

The function analogWrite is used to control the speed of the motors by sending a PWM (Pulse Width Modulation) signal to the motor driver pins ENA and ENB. In the program, analogWrite sets the PWM signal to 100, which regulates the amount of power delivered to the motors. A higher value would increase motor speed, while a lower value would decrease it. This control is essential for managing the robot's movement dynamics—allowing for smoother acceleration and deceleration during operations like turning, moving forward, or stopping .

digitalWrite operates at two states, HIGH or LOW, making it suitable for simple ON/OFF operations without intermediate states, which can make it less ideal for motor speed control needing precision. analogWrite, however, provides PWM signals for variable speed control, enabling finer control over motor power to achieve smoother acceleration or deceleration. While analogWrite is more versatile, it also requires careful management of PWM to avoid motor wear and energy inefficiency. These trade-offs are pivotal when optimizing for both operational simplicity and performance efficiency in robotics .

You might also like