|
/*
* 走黑线
* 链接方法:L298N:ENA,IN1,IN2,IN3,IN4,ENB
引脚12-IN1;引脚13-IN2;引脚10-IN3;引脚11-IN4;
引脚9-ENA;引脚6-ENB;
引脚A0-线左传感器;引脚A1-线右传感器;引脚A2-外左传感器;引脚A3-外右传感器;遇到黑线
经过试验:遇到黑线:指示灯不亮,io=1
遇到地板:指示灯亮, io=0*/
#define left_motor_go digitalWrite(10,1),digitalWrite(11,0) //左轮前进
#define left_motor_back digitalWrite(10,0),digitalWrite(11,1) //左轮后退
#define right_motor_go digitalWrite(12,0),digitalWrite(13,1) //右轮前进
#define right_motor_back digitalWrite(12,1),digitalWrite(13,0)//右轮后退
#define line_left digitalRead(A0) //线左传感器
#define line_right digitalRead(A1) //线右传感器
#define out_left digitalRead(A2) //外左传感器
#define out_right digitalRead(A3) //外右传感器
#define Stop_left digitalWrite(9,0)
#define Stop_right digitalWrite(6,0)
#define n0 (out_left==1)&&(out_right==1) // 外传感器全部检测到黑线
#define n1 (out_left==1)&&(out_right==0) //外左传感器检测到黑线
#define n2 (out_left==0)&&(out_right==1) // 外右传感器检测到黑线
#define n3 ((out_left==1)&&(out_right==1)&&(line_right==1)&&(line_left==1)) //传感器全部检测到黑
#define n4 ((out_left==0)&&(out_right==0)&&(line_right==0)&&(line_left==0)) //传感器全部检测到bai
//////////////////////////////////////////////////////////////////////
void zitai(boolean f1,byte s1,boolean f2,byte s2)//(左轮方向:1前进,0后退。速度:0~255;右轮方向:1前进,0后退。速度:0~255.)
{
if(f1)
left_motor_go; //左电机前进
else
left_motor_back; //左电机后退
if(f2)
right_motor_go;//右电机前进
else
right_motor_back;//右电机后退
analogWrite(9,s1);//0~255,值越大电压越高
analogWrite(6,s2);
}
//跟着黑线走
void zhixing(){
if((line_left == 1)&& (line_right == 1)) //go strite
{
zitai(1,200,1,200);
}
else if((out_left==0)&&(out_right==0)&&(line_left == 0)&& (line_right ==1)) //turn right
{
zitai(1,190,1,190);
while((out_left==0)&&(out_right==0)&&(line_right==0)&&(line_left==0)) //line_right == 1,line_left==1,线右,线左探测到地板,一直左转
{
zitai(1,200,0,200); //慢慢原地右转
}
}
else if((out_left==0)&&(out_right==0)&&(line_left == 1)&& (line_right == 0)) //turn left
{
zitai(1,190,1,190);
while((out_left==0)&&(out_right==0)&&(line_right==0)&&(line_left==0)) //line_right == 1,line_left==1,线右,线左探测到地板,一直左转
{
zitai(0,200,1,200); //慢慢原地左转
}
}
}
void wangline(void) //跟着直线走
{
if((line_left == 1)&& (line_right == 1)) //go strite
{
zitai(1,230,1,230);
}
while((out_left==0)&&(out_right==0)&&(line_left == 0)&& (line_right ==1)) //turn right
{
zitai(1,230,0,200);
zitai(1,230,0,200);
zitai(1,220,1,220);
while((out_left==0)&&(out_right==0)&&(line_right==0)&&(line_left==0)) //line_right == 1,line_left==1,线右,线左探测到地板,一直左转
{
zitai(1,230,0,230); //慢慢原地右转
}
}
while((out_left==0)&&(out_right==0)&&(line_left == 1)&& (line_right == 0)) //turn left
{
zitai(0,200,1,230);
zitai(0,200,1,230);
zitai(1,220,1,220);
while((out_left==0)&&(out_right==0)&&(line_right==0)&&(line_left==0)) //line_right == 1,line_left==1,线右,线左探测到地板,一直左转
{
zitai(0,230,1,230); //慢慢原地左转
}
}
/*else //stop
{
zitai(0,100,0,100);
}*/
}
|
-
-
_3.zip
1.82 KB, 下载次数: 14, 下载积分: 黑币 -5
|