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

Arduino Project 030 - Line Following Robot

RonWang1年前 (2024-11-21)电子编程1392

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.donstudio.cn/?id=285

标签: Arduino

相关文章

Arduino Programming Basic - Variables

Arduino Programming Basic - Variables

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

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 032 - BMP280 Pressure Sensor LCD Display

Arduino Project 032 - BMP280 Pressure Sensor LCD Display

For this project we will use Arduino Uno and BMP280 along with LCD 16x2 display module to display te...

Arduino Project 034 - TM1637 4Digital 7Segment Display Module

Arduino Project 034 - TM1637 4Digital 7Segment Display Module

A standard 4-digit 7-segment display is needed for clock, timer and counter projects, but it usually...

Arduino Project 021 -  LED Dot Matrix Display - Scrolling Message

Arduino Project 021 - LED Dot Matrix Display - Scrolling Message

There are many different ways to drive LEDs. Using shift registers is one way and they have their a...

 Arduino Project 037 - 18B20 Temperature Sensor

Arduino Project 037 - 18B20 Temperature Sensor

The DS18B20 Temperature Sensor is a digital temperature sensor capable of measuring temperatures wit...