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

Arduino Project 030 - Line Following Robot

RonWang2个月前 (11-21)电子编程129

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://parentscn.com/?id=285

标签: Arduino

相关文章

Arduino Project 027 - Joystick Servo Control

Arduino Project 027 - Joystick Servo Control

In this tutorial, we are going to learn how to use Arduino and a joystick to control two servo motor...

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 Project 018 - Dual Shift Register 8-Bit Binary Counter

Arduino Project 018 - Dual Shift Register 8-Bit Binary Counter

In Project 18, you will daisy chain (or cascade) another 74HC595 IC onto the one used in Project 17...

Arduino Project 017 - Shift Register 8-Bit Binary Counter

Arduino Project 017 - Shift Register 8-Bit Binary Counter

In this project, you’re going to use additional ICs (Integrated Circuits) in the form of shift regis...

Arduino Project 026 - Dual Servo Control

Arduino Project 026 - Dual Servo Control

This project you’ll create another simple project, but this time you’ll control two servos using com...

Arduino Project 005 - LED Chase Lights

Arduino Project 005 - LED Chase Lights

You’re going to use a string of LEDs (10 in total) to make an LED chase effect, similar to that used...

评论列表

单片机之名
单片机之名
2个月前 (12-04)

Line fellow trance 双电机控制原理

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。