Stap 6: problemen oplossen
Google vertelt me ik kan de functie "millis" of "interrupt" gebruiken om dit te doen, maar ik begrijp nog steeds niet na het lezen van diverse artikelen...: (het meest voorkomende voorbeeld heet knipperen zonder vertraging. Ik weet nog niet hoe het te gebruiken in mijn situatie... So... Ik probeer een andere manier dat is erg lastig en de code is als volgt:
#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();}
Ik de servo sweep scheiden in drie delen (sweepServo1 3). Elk onderdeel controleert verschillende hoeken en alle onderdelen zijn samen lus in orders.