51红外循迹小车代码
时间: 2023-12-20 08:29:12 浏览: 97
以下是基于51芯片的红外循迹小车代码,包括寻线和跟随功能:
```c
#include "reg52.h"
#include "delay.h"
sbit Finder_Left = P2^0;
sbit Finder_Right = P2^1;
sbit Flow_Left = P2^3;
sbit Flow_Right = P2^2;
extern char speed_Right;//0~40
extern char speed_Left;//0~40
//寻线功能
void checkWay(){
//左边触线,左转
if(Finder_Left && !Finder_Right){
speed_Right = 40;
speed_Left = 10;
}
//右边触线,左转
else if(Finder_Right && !Finder_Left){
speed_Right = 7;
speed_Left = 40;
}
//车悬空,停止移动
else if(Finder_Right && Finder_Left){
speed_Right = 0;
speed_Left = 0;
}
//直线,直行
else{
speed_Right = 36;
speed_Left = 36;
}
}
//跟随功能
void flow(){
//左边有人,左转
if(Flow_Right && !Flow_Left){
speed_Right = 40;
speed_Left = 5;
}
//右边有人,右转
else if(Flow_Left && !Flow_Right){
speed_Right = 5;
speed_Left = 40;
}
//车悬空,停止移动
else if(Flow_Right && Flow_Left){
speed_Right = 0;
speed_Left = 0;
}
//人在前方,直行
else{
speed_Right = 40;
speed_Left = 40;
}
}
```
阅读全文