Publicat la data

Robot 2WD Roboromania autonom cu radar ocolire obstacole

Robot 2WD Roboromania autonom cu radar ocolire obstacole

robot

Unul dintre cele mai simple coduri pentru Robot 2WD autonom cu radar ocolire obstacole.

Avem nevoie de :
o placă de dezvoltare tip Arduino UNO (oricare)
un modul driver motoare L298N
un senzor cu ultrasunete
șasiu cu 2 motoare 2WD
servomotor
cabluri Dupont

robot-2wd-roboromania

Cel mai simplu cod :

//––––––––––––––

// roboromania.ro Arduino Object Avoiding Robot with Ultrasonic Sensor
#include <Servo.h>
#include „Ultrasonic.h”

const int buzzer = 8;
const int motorA1= 2;
const int motorA2= 6;
const int enableA = 3;
const int motorB1= 10;
const int motorB2= 9;
const int enableB = 5;

Ultrasonic ultrasonic(A2 ,A3); //(trig pin,echo pin)
Servo myservo;

int distance;
int checkRight;
int checkLeft;
int pos=100;
int speedPWM = 150; //Change speed (PWM max 255)

void setup()
{
myservo.attach(7); //Servo pin connected to pin 7
myservo.write(pos);
pinMode(buzzer, OUTPUT);
pinMode(motorA1,OUTPUT);
pinMode(motorA2,OUTPUT);
pinMode(motorB1,OUTPUT);
pinMode(motorB2,OUTPUT);
pinMode(enableA, OUTPUT);
pinMode(enableB, OUTPUT);
delay(1000);
}

void loop(){
analogWrite(enableA, speedPWM);
analogWrite(enableB, speedPWM+50);
//Read distance…
distance = ultrasonic.Ranging(CM); // CM= centimeters INC= inches
delay(20);
//Check for objects…
if (distance > 15){
forward(); //All clear, move forward!
noTone(buzzer);
}
else if (distance <=15){
stop(); //Object detected! Stop the robot and check left and right for the better way out!
tone(buzzer,500); // play a tone
//Start scanning…
for(pos = 30; pos < 170; pos += 1){
myservo.write(pos);
delay(10);
}

checkLeft = ultrasonic.Ranging(CM);

for(pos = 170; pos>=30; pos-=1){
myservo.write(pos);
delay(10);
}

checkRight= ultrasonic.Ranging(CM);

myservo.write(100); // Sensor „look” forward again

// decision turn left or right?
if (checkLeft < checkRight){
left();
delay(500);
}
else if (checkLeft > checkRight){
right();
delay(500);
}
else if (checkLeft <=10 && checkRight <=10){
backward();
left();
}
}
delay(150);
}
void forward(){
digitalWrite(motorA1, HIGH);
digitalWrite(motorA2, LOW);
digitalWrite(motorB1, HIGH);
digitalWrite(motorB2, LOW);
}

void backward(){
digitalWrite(motorA1, LOW);
digitalWrite(motorA2, HIGH);
digitalWrite(motorB1, LOW);
digitalWrite(motorB2, HIGH);
}

void left(){
digitalWrite(motorA1, HIGH);
digitalWrite(motorA2, LOW);
digitalWrite(motorB1, LOW);
digitalWrite(motorB2, HIGH);
}

void right(){
digitalWrite(motorA1, LOW);
digitalWrite(motorA2, HIGH);
digitalWrite(motorB1, HIGH);
digitalWrite(motorB2, LOW);
}

void stop(){
digitalWrite(motorA1, LOW);
digitalWrite(motorA2, LOW);
digitalWrite(motorB1, LOW);
digitalWrite(motorB2, LOW);
}

//––––––––––––––

Atenție la declararea pinilor. Dacă copiați codul atenție la fonturi.

Succes !

Lasă un răspuns

Adresa ta de email nu va fi publicată. Câmpurile necesare sunt marcate *