C'est un projet assez compliqué, mais c'est possible! Une version simple peut utiliser deux moteurs (droit et gauche), avec un capteur infrarouge qui est monté sur un servomécanisme. Si vous voulez faire un robot très avancé, vous pourriez envisager de cartographie 2d! En revanche, il est très difficile...
Pour un robot qui se déplace au hasard, tout en évitant les obstacles, voilà! Des codes!
J'ai utilisé ce télémètre infrarouge:
https://www.sparkfun.com/products/8958?C'est pas trop chèr!
Aussi, je n'ai pas utilisé des moteurs - j'ai utilisé des servomécanismes modifiés pour se déplacer!
Bonne chance et désolé pour le mauvais Français!
[/*******************************************************************************************************************************\
Code for an obstacle avoiding robot. Uses two modified servos as wheels and a Sharp IR Rangefinder mounted on a scanning servo.
It also uses a simple LED bar graph to display distance readings graphically.
Left servo: pin 15 on the atmega328 (Arduino digital pin 9)
Right servo: pin 16 on the atmega328 (Arduino digital pin 10)
Scanning servo: pin 17 on the atmega328 (Arduino digital pin 11)
IR Rangefinder: pin 23 on the atmega328 (Arduino analog pin 0)
LED bar graph: 1: pin 18 on the atmega328 (Arduino digital pin 12)
2: pin 14 on the atmega328 (Arduino digital pin 8)
3: pin 13 on the atmega328 (Arduino digital pin 7)
4: pin 12 on the atmega328 (Arduino digital pin 6)
5: pin 11 on the atmega328 (Arduino digital pin 5)
6: pin 6 on the atmega328 (Arduino digital pin 4)
7: pin 5 on the atmega328 (Arduino digital pin 3)
8: pin 4 on the atmega328 (Arduino digital pin 2)
9: pin 3 on the atmega328 (Arduino digital pin 1)
10: pin 2 on the atmega328 (Arduino digital pin 0)
\*******************************************************************************************************************************/
#include <Servo.h>
const int left_wheel = 9;
const int right_wheel = 10;
const int scanner = 11;
const int rangefinder = 0;
const int num_of_leds = 10;
int led_pins[] =
{
12, 8, 7, 6, 5, 4, 3, 2, 1, 0
};
int left_forward = 850;
int left_stop = 1220;
int left_backward = 1450;
int right_forward = 1520;
int right_stop = 1375;
int right_backward = 1050;
int scan_left = 750;
int scan_right = 2250;
int scan_centre = 1500;
int sensor_value = 0;
int sensor_min = 1023;
int sensor_max = 0;
int led_sensor_value = 0;
Servo left_servo;
Servo right_servo;
Servo scan_servo;
void setup()
{
left_servo.attach (left_wheel);
right_servo.attach (right_wheel);
scan_servo.attach (scanner);
for (int this_led = 0; this_led < num_of_leds; this_led++)
{
pinMode (led_pins[this_led], OUTPUT);
}
left_servo.writeMicroseconds (left_stop);
right_servo.writeMicroseconds (right_stop);
scan_servo.writeMicroseconds (scan_centre);
while (millis() < 5000)
{
sensor_value = analogRead (rangefinder);
}
}
void loop()
{
sensor_value = analogRead (rangefinder);
sensor_value = map (sensor_value, sensor_min, sensor_max, 0, 255);
sensor_value = constrain (sensor_value, 0, 255);
left_servo.writeMicroseconds (left_forward);
right_servo.writeMicroseconds (right_forward);
if (sensor_value < 150)
{
left_servo.writeMicroseconds (left_stop);
right_servo.writeMicroseconds (right_stop);
scan_servo.writeMicroseconds (scan_left);
delay(500);
int left_reading = analogRead (rangefinder);
scan_servo.writeMicroseconds (scan_right);
delay(500);
int right_reading = analogRead (rangefinder);
scan_servo.writeMicroseconds (scan_centre);
if (left_reading < right_reading)
{
right_servo.writeMicroseconds (right_forward);
left_servo.writeMicroseconds (left_backward);
delay(400);
}
if (right_reading < left_reading)
{
left_servo.writeMicroseconds (left_forward);
right_servo.writeMicroseconds (right_backward);
delay(400);
}
}
int led_sensor_value = analogRead(rangefinder);
int led_level = map(led_sensor_value, 0, 400, 0, num_of_leds);
for (int this_led = 0; this_led < num_of_leds; this_led++)
{
if (this_led < led_level)
{
digitalWrite (led_pins[this_led], HIGH);
}
else
{
digitalWrite(led_pins[this_led], LOW);
}
}
}/code]