|
本帖最后由 qaz63547 于 2021-5-18 16:50 编辑
int spd1 = 80;
int spd2 = 91;
int isObstaclePin = 12;
int isObstacle = HIGH;
int System=0;
const int IN1 = 10;
const int IN2 = 9;
const int IN3 = 6;
const int IN4 = 5;
int SensorL=7;
int SensorR=8;
int SensorM=4;
int SL;
int SR;
int SM;
void setup()
{
Serial.begin(9600);
pinMode(10,OUTPUT);
pinMode(9,OUTPUT);
pinMode(6,OUTPUT);
pinMode(5,OUTPUT);
pinMode(isObstaclePin, INPUT);
pinMode(SensorL,INPUT);
pinMode(SensorR,INPUT);
pinMode(SensorM,INPUT);
delay(500);
}
void loop() {
if(digitalRead(isObstaclePin)==0)
{
System=1;
}
if(System==0)
{
Serial.println("Now is 0");
Stop();
}
if(System==1)
{
SL = digitalRead(SensorL);
SR = digitalRead(SensorR);
SM= digitalRead(SensorM);
if(SL==1 && SM==0 && SR==1)
{
Serial.println("F");
Forward ();
}
if(SL==1 && SM==1 && SR==0)
{
Serial.println("R");
Right ();
}
if(SL==0 && SM==1 && SR==1)
{
Serial.println("L");
Left ();
}
if(SL==0 && SM==0 && SR==1)
{
spd1=60;
Serial.println("LL");
Forward ();
}
if(SL==1 && SM==0 && SR==0)
{
spd2=60;
Serial.println("LL");
Forward ();
}
if(SL==1 && SM==1 && SR==1)
{
Serial.println("S");
Back ();
}
if(SL==0 && SM==0 && SR==0)
{
Serial.println("S");
Stop ();
}
if(SL==0 && SM==1 && SR==0)
{
Serial.println("N");
Stop ();
}
if(digitalRead(isObstaclePin)==1)
{
delay(1000);
System=2;
}
}
if(System==2)
{
Back();
delay(3000);
System=0;
}
}
void Forward()
{
analogWrite(IN1, spd1);
analogWrite(IN2, 0);
analogWrite(IN3, spd2);
analogWrite(IN4, 0);
}
void Back()
{
analogWrite(IN1, 0);
analogWrite(IN2, 50);
analogWrite(IN3,0 );
analogWrite(IN4, 50);
}
void Stop()
{
analogWrite(IN1, 0);
analogWrite(IN2, 0);
analogWrite(IN3, 0);
analogWrite(IN4, 0);
}
void Left()
{
analogWrite(IN1, spd1);
analogWrite(IN2, 0);
analogWrite(IN3, 0);
analogWrite(IN4, 0);
}
void Right()
{
analogWrite(IN1, 0);
analogWrite(IN2, 0);
analogWrite(IN3, spd2);
analogWrite(IN4, 0);
}
設備:
uno板
l298n
tcrt5000紅外線循跡
紅外線避障模組
市售一般自走車套件
想問問
第一次啟動電源時以手部去遮罩動作是正常的但是第二次用手做遮罩時馬達就不轉動或是動作異常只動一輪,直到電源重啟才會正常,但第二次一樣又會有問題是什麼原因呢?
初次使用兩個循跡模組時的動作是正常的用了三個就有上述問題了
希望各位高手可以解決小弟的困惑
謝謝
|
|