Étape 6: dépannage
Google me dit que je peux utiliser la fonction « millis » ou « interruption » du pour faire, mais je ne comprends toujours pas après avoir lu plusieurs articles...: (l’exemple le plus courant est appelé Blink sans délai. Je ne sais toujours pas comment l’utiliser dans ma situation... Alors... J’ai essayer une autre façon qui est très lourd et le code est comme suit :
#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();}
J’ai séparé le balayage de servo en trois parties (sweepServo1 3). Chaque partie contrôle sous différents angles et toutes les pièces sont bouclés ensemble dans les ordres.