Paso 6: solución de problemas
Google me dice que puedo usar la función "millis" o "interrupción" para hacerlo, pero todavía no entiendo después de leer varios artículos...: (el ejemplo más común se llama Blink sin demora. Todavía no sabemos cómo usarlo en mi situación... So... Trato de otra manera es muy engorroso y el código es como sigue:
#include <Servo.h> #define trigPin 5#define echoPin 6Servo myservo; // create servo object to control a servo int pos = 0; // variable to store the servo position int servoDirection = 100;int servoDelay = 20;int motorPin = 8; //right side to IB - forwardint motorPin2 = 9; //left side to IA - forwardint motorPin3 = 10; //right side to IA - backwardint motorPin4 = 11; //left side to IB - backwardvoid setup() {Serial.begin (9600); myservo.attach(7); // attaches the servo on pin 7 to the servo object myservo.write(pos);pinMode(motorPin, OUTPUT);pinMode(motorPin2, OUTPUT);pinMode(motorPin3, OUTPUT);pinMode(motorPin4, OUTPUT);pinMode(trigPin, OUTPUT);pinMode(echoPin, INPUT);}void forward(){ digitalWrite(motorPin, HIGH);digitalWrite(motorPin2, HIGH);digitalWrite(motorPin3, LOW); digitalWrite(motorPin4, LOW); }void backward() { digitalWrite(motorPin, LOW); digitalWrite(motorPin2, LOW); digitalWrite(motorPin3, HIGH); digitalWrite(motorPin4, HIGH); } void turnLeft() { digitalWrite(motorPin, HIGH); digitalWrite(motorPin2, LOW); digitalWrite(motorPin3, LOW); digitalWrite(motorPin4, HIGH); } void turnRight() { digitalWrite(motorPin, LOW); digitalWrite(motorPin2, HIGH);digitalWrite(motorPin3, HIGH); digitalWrite(motorPin4, LOW); }int CheckDistance(){ long duration, distance; digitalWrite(trigPin, LOW); // Added this line delayMicroseconds(2); // Added this line digitalWrite(trigPin, HIGH); delayMicroseconds(10); // Added this line digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = (duration/2) / 29.1; return distance;}void sweepServo1(){ for(pos = 10; pos < 40; pos += 1) // goes from 0 degrees to 100 degrees { // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(10);} int testDistance = CheckDistance(); /// get object distance using ping /// if object is more than 30 cm away it is out of range if (testDistance >= 30 || testDistance <= 0) /// if object is more than 30 cm away it is out of range { forward(); } else /// object is closer than 30cm, prit distance { turnRight(); Serial.print(testDistance); Serial.println("cm"); }}void sweepServo2(){ for(pos = 40; pos < 80; pos += 1) // goes from 0 degrees to 100 degrees { // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(10);} int testDistance = CheckDistance(); // get object distance using ping if (testDistance >= 30 || testDistance <= 0) // if object is more than 30 cm away it is out of range { forward(); } else // object is closer than 30cm, prit distance { turnRight(); Serial.print(testDistance); Serial.println("cm"); }}void sweepServo3(){ for(pos = 80; pos < 120; pos += 1) // goes from 0 degrees to 100 degrees { // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(10);} int testDistance = CheckDistance(); // get object distance using pingif (testDistance >= 30 || testDistance <= 0) // if object is more than 30 cm away it is out of range { forward(); } else // object is closer than 30cm, prit distance { turnRight(); Serial.print(testDistance); Serial.println("cm"); }}void loop() { sweepServo1(); sweepServo2(); sweepServo3();}
Separar el barrido servo en tres partes (sweepServo1 3). Cada parte controla diferentes ángulos y todas las piezas se coloca juntos en órdenes.