Please need help with this code that i was traying make to work .
The problem is that I want the servo move when two of the three senses censors, the servo does not move where I want.
when it senses the right, then moves 180, when senses the left, then moves 0 degrees, 90 degrees but sensation moves, but I want to move to a position intermediate senses when two sensors, for example to 135 degrees
Only moves where you say when senses only to a sensor
Thanks
#include <Servo.h> // Include servo library, you can get it from Arduino Playground - Servo
Servo myservo; // Create a servo object
int pos = 0; // Variable to store the servo position in degrees
int inicial = 90;
int trigDer = 13;
int echoDer = 12;
int trigIzq = 8;
int echoIzq = 7;
int trigCen = 4;
int echoCen = 2;
void setup() {
Serial.begin (9600);
pinMode(trigDer, OUTPUT);
pinMode(echoDer, INPUT);
pinMode(trigIzq, OUTPUT);
pinMode(echoIzq, INPUT);
pinMode(trigCen, OUTPUT);
pinMode(echoCen, INPUT);
myservo.attach(9); // set the servo to digital pin 9
myservo.write(inicial);
}
void loop() {
long durationDer, distanceDer;
digitalWrite(trigDer, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigDer, HIGH);
// delayMicroseconds(1000); - Removed this line
delayMicroseconds(10); // Added this line
digitalWrite(trigDer, LOW);
durationDer = pulseIn(echoDer, HIGH);
distanceDer = (durationDer/2) / 29.1;
Serial.print(distanceDer);
Serial.println(" cm der");
delay(500);
long durationIzq, distanceIzq;
digitalWrite(trigIzq, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigIzq, HIGH);
// delayMicroseconds(1000); - Removed this line
delayMicroseconds(10); // Added this line
digitalWrite(trigIzq, LOW);
durationIzq = pulseIn(echoIzq, HIGH);
distanceIzq = (durationIzq/2) / 29.1;
Serial.print(distanceIzq);
Serial.println(" cm izq");
delay(500);
long durationCen, distanceCen;
digitalWrite(trigCen, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigCen, HIGH);
// delayMicroseconds(1000); - Removed this line
delayMicroseconds(10); // Added this line
digitalWrite(trigCen, LOW);
durationCen = pulseIn(echoCen, HIGH);
distanceCen = (durationCen/2) / 29.1;
Serial.print(distanceCen);
Serial.println(" cm cen");
delay(500);
if (distanceDer > 10 && distanceDer < 20 || distanceIzq > 10 && distanceIzq < 20 || distanceCen > 10 && distanceCen < 20) {
if (distanceDer < distanceIzq) { // mover el servo a la derecha cuando la distancia sea menor a la distancia izquierda
myservo.write(160);
delay(200);
}
else if (distanceCen < 20) { //aca quisiera hacer esto if (distanceCen < 20 && distanceDer < 20) , pero esta manera no funciona.
myservo.write(130);
delay(200);
}
// tambien alrreves if (distanceCen < 20 && distanceIzq < 20 ) ocea que cuando sensen ambos sensores el servo se mueva a una posicion entre los dos servos.
// myservo.write(50);
else if (distanceIzq < distanceDer) {
myservo.write(20);
delay(200);
}
}
else {
myservo.write(90);
delay(200);
}
delay(100);
}