#include <AFMotor.h> #define MaxRange 23 // #define MaxTries 3 // int srfPin = 16; // Pin for SRF05 int ledPin = 2; // AF_DCMotor motorL(2); AF_DCMotor motorR(1); boolean isInRange(int value,int leftBorder,int rightBorder){ return (value>leftBorder && value<rightBorder); } // int getDistance(){ delay(50); int duration=0; // pinMode(srfPin, OUTPUT); digitalWrite(srfPin, LOW); // , delayMicroseconds(2); digitalWrite(srfPin, HIGH); // 10 delayMicroseconds(10); digitalWrite(srfPin, LOW); // pinMode(srfPin, INPUT); duration = pulseIn(srfPin, HIGH); // return duration/58; // } // void goForward(){ motorL.setSpeed(255); motorR.setSpeed(255); motorL.run(FORWARD); motorR.run(FORWARD); } void goBackward(){ motorL.setSpeed(255); motorR.setSpeed(255); motorL.run(BACKWARD); motorR.run(BACKWARD); } void turnLeft(){ motorL.setSpeed(255); motorR.setSpeed(255); motorL.run(BACKWARD); motorR.run(FORWARD); } void turnRight(){ motorL.setSpeed(255); motorR.setSpeed(255); motorL.run(FORWARD); motorR.run(BACKWARD); } void stopMovement(){ motorL.run(RELEASE); motorR.run(RELEASE); } // int getMaxDistanseAround(){ unsigned long timeStart=millis(); int maxFound=0; // if(random(100)<50){turnLeft();}else{turnRight();} // while(millis()-timeStart<3000){ int curDistance=getDistance(); if( curDistance > maxFound ){ maxFound=curDistance; } } return maxFound; } // boolean turnToDistance(int distance){ unsigned long timeStart=millis(); // if(random(100)<50){turnLeft();}else{turnRight();} // while(millis()-timeStart<3000){ // // , if( isInRange(getDistance(),distance-5,distance+5)){ // usb Serial.print("Turned to "); Serial.println(getDistance()); // ( ) goBackward(); delay(200); stopMovement(); blinkLed(); return true;// 1, .. } } Serial.println("distance not found"); return false;// } // , float getMySpeed(){ int secToMeas=2; float firstDistance=getDistance(); goForward(); delay(secToMeas*1000); stopMovement(); float newDistance=getDistance(); return (firstDistance-getDistance())/secToMeas/100; } // void blinkLed(){ digitalWrite(ledPin, HIGH); delay(1000); digitalWrite(ledPin, LOW); } float mySpeedSm=0;// /c float mySpeedM=0;// /c void setup() { Serial.begin(9600); // set up Serial library at 9600 bps // .. // randomSeed(analogRead(0)); // , pinMode(ledPin, OUTPUT); Serial.println("Testing systems"); blinkLed(); // getDistance(); if(getDistance()>0){ Serial.println("Ultrasonic sensor ready"); } delay(1000/4); // motorL.setSpeed(255); motorR.setSpeed(255); stopMovement(); blinkLed(); delay(1000/3); Serial.println("Getting averange speed"); blinkLed(); mySpeedSm=getMySpeed(); mySpeedM=mySpeedSm/100; blinkLed(); blinkLed(); blinkLed(); } int tries=MaxTries; // void loop(){ // int maxDist=getMaxDistanseAround(); Serial.print("Found max distance"); Serial.println(maxDist); delay(500); // if(turnToDistance(maxDist)){ tries=MaxTries;// int lastDistance=getDistance(); // while(lastDistance>MaxRange){ goForward(); delay(500); // int newDist=getDistance(); if(isInRange(newDist,lastDistance-2,lastDistance+2)){ goBackward(); delay(1000); lastDistance=0; } else{ lastDistance=newDist; } } }else{ // // tries--; if(tries==0){// - if(getDistance()>MaxRange*2){ goForward(); delay(1000); } else{ goBackward(); delay(1000); } } } stopMovement(); delay(500); }
Source: https://habr.com/ru/post/135529/
All Articles