Code:
int frontEchoPin = 12;
int frontTriggerPin = 11;
int leftEchoPin = 30;
int leftTriggerPin = 31;
int rightEchoPin = 33;
int rightTriggerPin = 32;
int enA = 2;
int motorL1 = 5;
int motorL2 = 6;
int motorR1 = 3;
int motorR2 = 4;
int enB = 7;
volatile float maxFrontDistance = 30.00;
volatile float frontDuration, frontDistanceCm, leftDuration, leftDistanceCm, rightDuration, rightDistanceCm;
volatile float maxLeftDistance, maxRightDistance = 30.00;
void setup() {
// serial
Serial.begin(9600);
// ultrasonic
pinMode(frontTriggerPin, OUTPUT);
pinMode(frontEchoPin, INPUT);
pinMode(leftTriggerPin, OUTPUT);
pinMode(leftEchoPin, INPUT);
pinMode(rightTriggerPin, OUTPUT);
pinMode(rightEchoPin, INPUT);
// motors
pinMode(motorL1, OUTPUT);
pinMode(motorL2, OUTPUT);
pinMode(motorR1, OUTPUT);
pinMode(motorR2, OUTPUT);
}
void loop() {
// front distance check
checkFrontDistance();
if (frontDistanceCm < maxFrontDistance) {
Serial.println("Too close");
checkLeftDistance();
delay(100);
checkRightDistance();
delay(100);
if (leftDistanceCm < rightDistanceCm)
{
moveBackward;
delay(100);
moveRight();
}
else if (leftDistanceCm > rightDistanceCm)
{
moveBackward;
delay(100);
moveLeft();
}
}
else {
Serial.println("OK");
moveForward();
}
// left distance check
checkLeftDistance();
if (leftDistanceCm < maxLeftDistance) {
Serial.println("Left too close");
delay(100);
checkFrontDistance();
delay(100);
checkRightDistance();
delay(100);
if (frontDistanceCm > rightDistanceCm)
{
moveForward();
}
else if (frontDistanceCm < rightDistanceCm)
{
moveBackward;
delay(100);
moveRight();
}
}
// right distance check
checkRightDistance();
if (rightDistanceCm < maxRightDistance) {
Serial.println("Right too close");
delay(100);
checkFrontDistance();
delay(100);
checkLeftDistance();
delay(100);
if (frontDistanceCm > leftDistanceCm)
moveForward();
else if (frontDistanceCm < leftDistanceCm)
{
moveBackward;
delay(100);
moveLeft();
}
}
}
void checkFrontDistance() {
digitalWrite(frontTriggerPin, LOW); //para generar un pulso limpio ponemos a LOW 4us
delayMicroseconds(4);
digitalWrite(frontTriggerPin, HIGH); //generamos Trigger (disparo) de 10us
delayMicroseconds(10);
digitalWrite(frontTriggerPin, LOW);
frontDuration = pulseIn(frontEchoPin, HIGH); //medimos el tiempo entre pulsos, en microsegundos
frontDistanceCm = frontDuration * 10 / 292 / 2; //convertimos a distancia, en cm
Serial.print("Distance: ");
Serial.print(frontDistanceCm);
Serial.println(" cm");
}
void checkLeftDistance() {
digitalWrite(leftTriggerPin, LOW); //para generar un pulso limpio ponemos a LOW 4us
delayMicroseconds(4);
digitalWrite(leftTriggerPin, HIGH); //generamos Trigger (disparo) de 10us
delayMicroseconds(10);
digitalWrite(leftTriggerPin, LOW);
leftDuration = pulseIn(leftEchoPin, HIGH); //medimos el tiempo entre pulsos, en microsegundos
leftDistanceCm = leftDuration * 10 / 292 / 2; //convertimos a distancia, en cm
Serial.print("Left distance: ");
Serial.print(leftDistanceCm);
Serial.println(" cm");
}
void checkRightDistance() {
digitalWrite(rightTriggerPin, LOW); //para generar un pulso limpio ponemos a LOW 4us
delayMicroseconds(4);
digitalWrite(rightTriggerPin, HIGH); //generamos Trigger (disparo) de 10us
delayMicroseconds(10);
digitalWrite(rightTriggerPin, LOW);
rightDuration = pulseIn(rightEchoPin, HIGH); //medimos el tiempo entre pulsos, en microsegundos
rightDistanceCm = rightDuration * 10 / 292 / 2; //convertimos a distancia, en cm
Serial.print("Right distance: ");
Serial.print(rightDistanceCm);
Serial.println(" cm");
}
void moveBackward() {
Serial.println("Backward.");
digitalWrite(motorL1, LOW);
digitalWrite(motorL2, HIGH);
digitalWrite(motorR1, LOW);
digitalWrite(motorR2, HIGH);
analogWrite(enA, 100);
analogWrite(enB, 100);
}
void moveForward() {
Serial.println("Forward.");
digitalWrite(motorL1, HIGH);
digitalWrite(motorL2, LOW);
digitalWrite(motorR1, HIGH);
digitalWrite(motorR2, LOW);
analogWrite(enA, 130);
analogWrite(enB, 130);
}
void moveLeft() {
Serial.println("Left.");
digitalWrite(motorL1, HIGH);
digitalWrite(motorL2, LOW);
digitalWrite(motorR1, LOW);
digitalWrite(motorR2, HIGH);
analogWrite(enA, 90);
analogWrite(enB, 100);
}
void moveRight() {
Serial.println("Right.");
digitalWrite(motorL1, LOW);
digitalWrite(motorL2, HIGH);
digitalWrite(motorR1, HIGH);
digitalWrite(motorR2, LOW);
analogWrite(enA, 90);
analogWrite(enB, 100);
}
void moveStop()
{
digitalWrite(4,LOW);
digitalWrite(3,LOW);
digitalWrite(6,LOW);
digitalWrite(5,LOW);
analogWrite(enA, 0);
analogWrite(enB, 0);
}
Mein Problem ist jetzt das der "leftDistance" rum Zickt.
Lesezeichen