Code:
#include <NewPing.h>
int trigPinF = 11;
int echoPinF = 12;
int trigPinR = 32;
int echoPinR = 33;
int enA = 2;
int revright = 3;
int fwdright= 4;
int revleft= 5;
int fwdleft = 6;
int enB = 7;
int c = 0;
void setup() {
//Serial.begin(9600);
pinMode(enA, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(trigPinF, OUTPUT);
pinMode(echoPinF, INPUT);
pinMode(trigPinR, OUTPUT);
pinMode(echoPinR, INPUT);
}
void loop() {
long duration,distanceF,distanceR;
digitalWrite(trigPinF,HIGH);
digitalWrite(trigPinR,HIGH);
delayMicroseconds(1000);
digitalWrite(trigPinF,LOW);
digitalWrite(trigPinR,LOW);
duration=pulseIn(echoPinF,HIGH);
duration=pulseIn(echoPinR,HIGH);
distanceF,distanceR=(duration/2)/29.1;
//Serial.print(distance);
//Serial.println("CM");
delay(10);
if((distanceF>=20&distanceR>=20))
{
moveforward();
}
if(distanceF<20&distanceR>=20)
{
movestop();
delay(400);
movereverse();
delay(3000);
movestop();
}
else if(distanceF>=20&distanceR<20)
{
movestop();
delay(400);
movereverse();
delay(3000);
movestop();
delay(400);
moveleft();
}
}
void moveforward()
{
digitalWrite(4,LOW);
digitalWrite(3,HIGH);
digitalWrite(6,LOW);
digitalWrite(5,HIGH);
analogWrite(enA, 160);
analogWrite(enB, 160);//
}
void moveleft()
{
digitalWrite(4,HIGH);
digitalWrite(3,LOW);
digitalWrite(6,LOW);
digitalWrite(5,HIGH);
analogWrite(enA, 140);
analogWrite(enB, 180);
}
void moveright()
{
digitalWrite(4,LOW);
digitalWrite(3,HIGH);
digitalWrite(6,HIGH);
digitalWrite(5,LOW);
analogWrite(enA, 180);
analogWrite(enB, 140);
}
void movereverse()
{
digitalWrite(4,HIGH);
digitalWrite(3,LOW);
digitalWrite(6,HIGH);
digitalWrite(5,LOW);
analogWrite(enA, 120);
analogWrite(enB, 120);
}
void movestop()
{
digitalWrite(4,LOW);
digitalWrite(3,LOW);
digitalWrite(6,LOW);
digitalWrite(5,LOW);
analogWrite(enA, 0);
analogWrite(enB, 0);
}
Vielleicht kann jemand mal drüber schauen und mir sagen wo der Fehler liegt.
Lesezeichen