硬件:uno板,L298N马达驱动板,9伏电池,马达,红外线传感器TCR T5000 5个(或5合1一个)
将以下代码复制并粘贴到您的 Arduino 编辑器中。
测试代码
使用此代码测试电机和红外传感器。
/*
This is the Test code for the line follower.
For instructions, please click the link below.
*/
// Motor A - Left Motor
#define MCenA 13
#define MCin1 11
#define MCin2 12
// Motor B - Right Motor
#define MCenB 8
#define MCin3 9
#define MCin4 10
// Set inital speed for Motors
int motorSpeedA = 0;
int motorSpeedB = 0;
// Initialise light sensors
int in1 = 3;
int in2 = 4;
int in3 = 5;
int in4 = 6;
int in5 = 7;
int s1 = 0;
int s2 = 0;
int s3 = 0;
int s4 = 0;
int s5 = 0;
void setup() {
// Open serial connection to display values in Monitor if connected
Serial.begin(9600);
Serial.println("Light sensor test");
// Initialise sensor inputs:
pinMode(in1,INPUT);
pinMode(in2,INPUT);
pinMode(in3,INPUT);
pinMode(in4,INPUT);
pinMode(in5,INPUT);
// Initialize the Arduino ports for the Motor Controller
pinMode(MCenA, OUTPUT);
pinMode(MCenB, OUTPUT);
pinMode(MCin1, OUTPUT);
pinMode(MCin2, OUTPUT);
pinMode(MCin3, OUTPUT);
pinMode(MCin4, OUTPUT);
}
void loop() {
// Read each light sensor
s1 = digitalRead(in1);
s2 = digitalRead(in2);
s3 = digitalRead(in3);
s4 = digitalRead(in4);
s5 = digitalRead(in5);
// Print ligth sensor reading to Serial Monitor (Ctrl + Shift + M to view)
Serial.print("Sensors");
Serial.print("\t");
Serial.print(s1);
Serial.print("\t");
Serial.print(s2);
Serial.print("\t");
Serial.print(s3);
Serial.print("\t");
Serial.print(s4);
Serial.print("\t");
Serial.print(s5);
Serial.print("\t");
// Set Motor A forward by default
digitalWrite(MCin1, LOW);
digitalWrite(MCin2, HIGH);
// Set Motor B forward by default
digitalWrite(MCin3, LOW);
digitalWrite(MCin4, HIGH);
TurnRight();
delay(1000);
Stop();
delay(1000);
TurnLeft();
delay(1000);
Stop();
delay(1000);
DriveStraight();
delay(1000);
Stop();
delay(1000);
}
void DriveStraight() {
Serial.print("Drive straight (IN LOOP.)");
// Set motor speeds
motorSpeedA = 150;
motorSpeedB = 150;
// Print motor speed to monitor.
Serial.print("Motor Speeds.");
Serial.print("\t");
Serial.print(motorSpeedA);
Serial.print("\t");
Serial.println(motorSpeedB);
// Send motor speeds to motor
analogWrite(MCenA, motorSpeedA); // Send PWM signal to motor A
analogWrite(MCenB, motorSpeedB); // Send PWM signal to motor B
}
void TurnRight() {
Serial.print("Turn Right (IN LOOP.)");
// Set motor speeds
motorSpeedA = 0;
motorSpeedB = 150;
// Print motor speed to montor.
Serial.print("Motor Speeds.");
Serial.print("\t");
Serial.print(motorSpeedA);
Serial.print("\t");
Serial.println(motorSpeedB);
// Send motor speeds to motor
analogWrite(MCenA, motorSpeedA); // Send PWM signal to motor A
analogWrite(MCenB, motorSpeedB); // Send PWM signal to motor B
}
void TurnLeft() {
Serial.print("Turn Left (IN LOOP.)");
// Set motor speeds
motorSpeedA = 150;
motorSpeedB = 0;
// Print motor speed to montor.
Serial.print("Motor Speeds.");
Serial.print("\t");
Serial.print(motorSpeedA);
Serial.print("\t");
Serial.println(motorSpeedB);
// Send motor speeds to motor
analogWrite(MCenA, motorSpeedA); // Send PWM signal to motor A
analogWrite(MCenB, motorSpeedB); // Send PWM signal to motor B
}
void Stop() {
Serial.print("Stop (IN LOOP.)");
// Set motor speeds
motorSpeedA = 0;
motorSpeedB = 0;
// Print motor speed to montor.
Serial.print("Motor Speeds.");
Serial.print("\t");
Serial.print(motorSpeedA);
Serial.print("\t");
Serial.println(motorSpeedB);
// Send motor speeds to motor
analogWrite(MCenA, motorSpeedA); // Send PWM signal to motor A
analogWrite(MCenB, motorSpeedB); // Send PWM signal to motor B
}</code></span></span>
巡线代码
使用此代码来运行巡线机器人(车)。
/*
This is the run code for the line follower.
For instructions, please click the link below.
*/
// Motor A - Left Motor
#define MCenA 11
#define MCin1 13
#define MCin2 12
// Motor B - Right Motor
#define MCenB 9
#define MCin3 8
#define MCin4 10
// Set inital speed for Motors
int motorSpeedA = 0;
int motorSpeedB = 0;
// Initialise light sensors
int in1 = 3;
int in2 = 4;
int in3 = 5;
int in4 = 6;
int in5 = 7;
int s1 = 0;
int s2 = 0;
int s3 = 0;
int s4 = 0;
int s5 = 0;
void setup() {
// Open serial connection to display values in Monitor if connected
Serial.begin(9600);
Serial.println("Light sensor test");
// Initialize the Arduino ports for the Motor Controller
pinMode(MCenA, OUTPUT);
pinMode(MCenB, OUTPUT);
pinMode(MCin1, OUTPUT);
pinMode(MCin2, OUTPUT);
pinMode(MCin3, OUTPUT);
pinMode(MCin4, OUTPUT);
// Initialise sensor inputs:
pinMode(in1,INPUT);
pinMode(in2,INPUT);
pinMode(in3,INPUT);
pinMode(in4,INPUT);
pinMode(in5,INPUT);
}
void loop() {
// 读取传感器数据
s1 = digitalRead(in1);
s2 = digitalRead(in2);
s3 = digitalRead(in3);
s4 = digitalRead(in4);
s5 = digitalRead(in5);
Serial.print("Sensors");
Serial.print("\t");
Serial.print(s1);
Serial.print("\t");
Serial.print(s2);
Serial.print("\t");
Serial.print(s3);
Serial.print("\t");
Serial.print(s4);
Serial.print("\t");
Serial.print(s5);
Serial.print("\t");
// 设置左右轮向前运动
digitalWrite(MCin1, LOW);
digitalWrite(MCin2, HIGH);
digitalWrite(MCin3, LOW);
digitalWrite(MCin4, HIGH);
if ((s2 == 1) && (s4 == 1)) {
// Serial.print("Drive straight.");
DriveStraight();
}
else if ((s2 == 0) && (s4 == 1)) {
// Serial.print("Turn Right.");
TurnRight();
}
else if ((s2 == 1) && (s4 == 0)) {
// Serial.print("Turn Left.");
TurnLeft();
}
else if ((s1 == 0) && (s2 == 0) && (s3 == 0) && (s4 == 0) && (s5 == 0)) {
// Serial.print("Drive straight.");
DriveStraight();
}
else {
// Serial.print("STOP.");
Stop();
}
delay(10);
motorSpeedA = 0;
motorSpeedB = 0;
// 设置马达速度
analogWrite(MCenA, motorSpeedA); // Send PWM signal to motor A
analogWrite(MCenB, motorSpeedB); // Send PWM signal to motor B
delay(10);
}
void DriveStraight() {
Serial.print("Drive straight (IN LOOP.)");
// Set motor speeds
motorSpeedA = 150;
motorSpeedB = 150;
// Print motor speed to montor.
Serial.print("Motor Speeds.");
Serial.print("\t");
Serial.print(motorSpeedA);
Serial.print("\t");
Serial.println(motorSpeedB);
// Send motor speeds to motor
analogWrite(MCenA, motorSpeedA); // Send PWM signal to motor A
analogWrite(MCenB, motorSpeedB); // Send PWM signal to motor B
}
void TurnRight() {
Serial.print("Turn Right (IN LOOP.)");
//小车向右转,左轮停止,右轮速度150
// Set motor speeds
motorSpeedA = 0;
motorSpeedB = 150;
// Print motor speed to montor.
Serial.print("Motor Speeds.");
Serial.print("\t");
Serial.print(motorSpeedA);
Serial.print("\t");
Serial.println(motorSpeedB);
// 传送信号,开始运动
analogWrite(MCenA, motorSpeedA); // Send PWM signal to motor A
analogWrite(MCenB, motorSpeedB); // Send PWM signal to motor B
}
void TurnLeft() {
Serial.print("Turn Left (IN LOOP.)");
// 右轮速度0,左轮速度150向右转。
motorSpeedA = 150;
motorSpeedB = 0;
// Print motor speed to montor.
Serial.print("Motor Speeds.");
Serial.print("\t");
Serial.print(motorSpeedA);
Serial.print("\t");
Serial.println(motorSpeedB);
// 传送信号,开始运动
analogWrite(MCenA, motorSpeedA); // Send PWM signal to motor A
analogWrite(MCenB, motorSpeedB); // Send PWM signal to motor B
}
void Stop() {
Serial.print("Stop (IN LOOP.)");
// Set motor speeds
motorSpeedA = 0;
motorSpeedB = 0;
// Print motor speed to montor.
Serial.print("Motor Speeds.");
Serial.print("\t");
Serial.print(motorSpeedA);
Serial.print("\t");
Serial.println(motorSpeedB);
// Send motor speeds to motor
analogWrite(MCenA, motorSpeedA); // Send PWM signal to motor A
analogWrite(MCenB, motorSpeedB); // Send PWM signal to motor B
}
在您的机器人上运行代码
安装光传感器。
光传感器需要安装在正确的高度才能正常工作。
现在您应该已将线路传感器连接到 Arduino。
如果已连接,您可以使用 USB 电缆将 Arduino 连接到计算机。它将为您的 Arduino 提供电源,并且也足以为线路传感器供电。
如果线路传感器通电,您应该看到线路传感器上至少有一个红色 LED 亮起。
如果您没有看到任何 LED 亮起,请检查接线是否松动且连接正确。
现在获取一块轨道,看看线传感器是否可以检测到该线。
通过在不同高度移动传感器穿过线路进行实验。
警告:线传感器检测红外光。太阳是红外线光源,会干扰您的传感器并导致错误读数。
使用传感器时,请尝试遮挡所有阳光和其他红外光源。
最好在夜间没有阳光的情况下操作。
当线路传感器穿过线路时,您应该看到每个传感器 LED 打开和关闭。
继续尝试离地面不同的高度。
我们的传感器电路板距离瓷砖大约 20 毫米。
当您对适合您的传感器高度感到满意时,请使用支架来固定高度。
我们使用小型和中型支架来达到所需的高度。