当前位置:首页 > 科学研究 > 电子编程 > 正文内容

Arduino Project 030 - Line Following Robot

RonWang9个月前 (11-21)电子编程752

Project 30 Line Following Robot

Use L298P Driver Line Following Robot

Arduino Line Trace Robot Circuit

 Line or Trace Following Robot Circuit

Arduino Line Trace Robot Schematic

Line or Trace Following Robot Schematic

/* Coding Ron Wang
   Nov.21th 2024
   Autaba support for coding Hardware
   Project 30  Tracking Line Robot
 */

int mr1=8;  //motor right 1
int mr2=9;  //motor right 2
int ml1=10; //motor left 1
int ml2=11; //motor left 2
int sr=6;   //sensor right
int sl=7;   //sensor left
int svr=0;
int svl=0;
int led=13;
int enr=3; 
int enl=5;
int vspeed=100;    
int tspeed=255;
int tdelay=20;

void  setup()
{
 pinMode(mr1,OUTPUT);
 pinMode(mr2,OUTPUT);
 pinMode(ml1,OUTPUT);
  pinMode(ml2,OUTPUT);
 pinMode(led,OUTPUT);
 pinMode(sr,INPUT);
 pinMode(sl,INPUT);
  
 delay(5000);
}

void loop()
{
 svr=digitalRead(sr);
 svl=digitalRead(sl);
  
  if(svl==LOW && svr==LOW)
  {
  forward();   // Forward
  }
  if(svl==HIGH  && svr==LOW)
  {
  left();     //Turn left
  }
 
  if(svl==LOW && svr==HIGH)
  { 
  right();     //Turn Right
  }
  
  if(svl==HIGH && svr==HIGH)
  {
  stop();   // Stop
  }
}
void forward()
 {
  digitalWrite(mr1,HIGH);
  digitalWrite(mr2,LOW);
  digitalWrite(ml1,HIGH);
  digitalWrite(ml2,LOW);
  analogWrite (enr,vspeed);
  analogWrite (enl,vspeed);
 } 
void backward()
  {
  digitalWrite(mr1,LOW);
  digitalWrite(mr2,HIGH);
  digitalWrite(ml1,LOW);
  digitalWrite(ml2,HIGH);
  analogWrite (enr,vspeed);
  analogWrite (enl,vspeed);
  }
void right()
 {
  digitalWrite(mr1,LOW);
  digitalWrite(mr2,HIGH);
  digitalWrite(ml1,HIGH);
  digitalWrite(ml2,LOW);
  analogWrite (enr,tspeed);
  analogWrite (enl,tspeed);
  delay(tdelay);
 } 
void left()
 {
  digitalWrite(mr1,HIGH);
  digitalWrite(mr2,LOW);
  digitalWrite(ml1,LOW);
  digitalWrite(ml2,HIGH);
  analogWrite (enr,tspeed);
  analogWrite (enl,tspeed);
  delay(tdelay);
}  
void stop()
 {
  analogWrite (enr,0);
  analogWrite  (enl,0);
 }

3 Way or 5Way Trace Following Robot

/* Coding Ron Wang
   Nov.21th 2024
   Autaba support for coding Hardware
   Project 30   3way or 5 ways Trace Following Robot
 */

#define lights 9

int LDR1, LDR2, LDR3; // sensor values

// calibration offsets
int leftOffset = 0, rightOffset = 0, centre = 0;

// pins for motor speed and direction

int speed1 = 3, speed2 = 11, direction1 = 12, direction2 = 13;

// starting speed and rotation offset
int startSpeed = 70, rotate = 30;

// sensor threshold
int threshhold = 5;

// initial speeds of left and right motors
int left = startSpeed, right = startSpeed;

// Sensor calibration routine
void calibrate() {
 for (int x=0; x<10; x++) { // run this 10 times to obtain average
 digitalWrite(lights, HIGH); // lights on
 delay(100);
 LDR1 = analogRead(0); // read the 3 sensors
 LDR2 = analogRead(1);
 LDR3 = analogRead(2);
 leftOffset = leftOffset + LDR1; // add value of left sensor to total
 centre = centre + LDR2; // add value of centre sensor to total
 rightOffset = rightOffset + LDR3; // add value of right sensor to total
 delay(100);
 digitalWrite(lights, LOW); // lights off
 delay(100);
 }
 
 // obtain average for each sensor
 leftOffset = leftOffset / 10;
 rightOffset = rightOffset / 10;
 centre = centre /10;
 // calculate offsets for left and right sensors
 leftOffset = centre - leftOffset;
 rightOffset = centre - rightOffset;
}

void setup()
{
 // set the motor pins to outputs
 pinMode(lights, OUTPUT); // lights
 pinMode(speed1, OUTPUT);
 pinMode(speed2, OUTPUT);
 pinMode(direction1, OUTPUT);
 pinMode(direction2, OUTPUT);
 // calibrate the sensors
 calibrate();
 delay(3000);
 digitalWrite(lights, HIGH); // lights on
 delay(100);
 // set motor direction to forward
 digitalWrite(direction1, HIGH);
 digitalWrite(direction2, HIGH);
 // set speed of both motors
 analogWrite(speed1,left);
 analogWrite(speed2,right);
}

void loop() {
 
 // make both motors same speed
 left = startSpeed;
 right = startSpeed;
 // read the sensors and add the offsets
 LDR1 = analogRead(0) + leftOffset;
 LDR2 = analogRead(1);
 LDR3 = analogRead(2) + rightOffset;
 
 // if LDR1 is greater than the centre sensor + threshold turn right
 if (LDR1 > (LDR2+threshhold)) {
 left = startSpeed + rotate;
 right = startSpeed - rotate;
 }
 
 // if LDR3 is greater than the centre sensor + threshold turn left
 if (LDR3 > (LDR2+threshhold)) {
 left = startSpeed - rotate;
 right = startSpeed + rotate;
 }
 
 // send the speed values to the motors
 analogWrite(speed1,left);
 analogWrite(speed2,right);
}

版权声明:本文为原创文章,版权归donstudio所有,欢迎分享本文,转载请保留出处!

本文链接:http://www.parentscn.com/?id=285

标签: Arduino

相关文章

 Arduino Project 033 - 4 Digital 7 Segment Display

Arduino Project 033 - 4 Digital 7 Segment Display

Showing Heep and number 0-9 on a Common Anode 7-segment LED display. Displays the numbers 0-9 on the...

Arduino Project 006 - LED Interactive Chase Effect

Arduino Project 006 - LED Interactive Chase Effect

Leave your circuit board intact from Project 5. You’re just going to add a potentiometer to this cir...

Arduino Programming Basic - Data Type

Arduino Programming Basic - Data Type

Arduino 程序基础,介绍Arduino程序的基本组成,第一部分编写了10个例子,关于变量及变量名称,串口监视器,if循环,for循环,while循环等。第二部分介绍了函数,全局变量,局部变量和静...

Arduino Project 028B - Basic Stepper Control (Unipolar)

Arduino Project 028B - Basic Stepper Control (Unipolar)

In this very simple project, you will connect up a stepper motor and then get the Arduino to control...

Arduino Project 012 - Piezo Sounder Melody Player

Arduino Project 012 - Piezo Sounder Melody Player

Rather than using the piezo to make annoying alarm sounds, why not use it to play a melody? You are...

Arduino UNO Mp3音乐播放代码

Arduino UNO Mp3音乐播放代码

Arduino UNO Mp3音乐播放代码今天我们将使用Arduino UNO 和SD卡制作音乐播放器。这个播放器不需要添加多余的模块,只需要SD读卡器和Arduino UNO开发板就可以播放音频文件...