Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Car finds middle path between right and left obstacles / walls / track boundaries ... MrLunk.net march-2017(gnu)
- // Max track widt 2 meters.
- // Max Right steer = 90+25 degrees servo angle. (max 115)
- // Max Left steer = 90-25 degrees servo angle. (min 65)
- #include <Servo.h>
- // Define Sonar-Sensor Pinout numbers
- const int trigPinOne = 5;
- const int trigPinTwo = 6;
- const int trigPinThree = 7;
- const int echoPinOne = 8;
- const int echoPinTwo = 9;
- const int echoPinThree = 10;
- Servo SteeringServo;
- // define variables
- long duration;
- int Direction = 90;
- int Distance;
- int LeftDistance = 100;
- int RightDistance = 100;
- int MiddleDistance = 100;
- int pos = 90;
- int MinSideDistance = 100; // min Distance to side sensors before proportional steering kicks in.
- void setup() {
- Serial.begin(9600);
- pinMode(trigPinOne, OUTPUT);
- pinMode(echoPinOne, INPUT);
- pinMode(trigPinTwo, OUTPUT);
- pinMode(echoPinTwo, INPUT);
- pinMode(trigPinThree, OUTPUT);
- pinMode(echoPinThree, INPUT);
- digitalWrite(trigPinOne, LOW);
- digitalWrite(trigPinTwo, LOW);
- digitalWrite(trigPinThree, LOW);
- SteeringServo.attach(11);
- delay (10);
- SteeringServo.write(90);
- }
- // --------------Main Routine definitions------------------------------
- void loop() {
- // ---- Start Front Sensor Polling
- SonarSensor(trigPinOne, echoPinOne); // Poll Left Sonar-Sensor
- int LeftDistance = Distance;
- delay(10);
- SonarSensor(trigPinThree, echoPinThree); // Poll Right Sonar-Sensor
- int RightDistance = Distance;
- delay(10);
- SonarSensor(trigPinTwo, echoPinTwo); // Poll Middle Sonar-Sensor
- int MiddleDistance = Distance;
- delay(10);
- // ---- End Front Sensor Polling ----
- int LeftSteer = map(RightDistance,20,MinSideDistance,0,-25); // Proportional LeftSteer to Left object/wall distance
- if (LeftSteer <= -25){LeftSteer = -25;} // Max Right Sensor Error Protection
- int RightSteer = map(LeftDistance,20,MinSideDistance,0,25); // Proportional RightSteer to Left object/wall distance
- if (LeftSteer >= 25){LeftSteer = 25;} // Max Left Sensor Error Protection
- int Direction = (90+LeftSteer+RightSteer); // Final Direction calculation
- if (Direction <= 65){Direction = 65;} // Max SteerLeft protection
- if (Direction >= 115){Direction = 115;} // Max SteerRight Protection
- SteeringServo.write(Direction); // Send Final direction to Servo
- delay(50);
- }
- // --------------Sub-Routine definitions------------------------------
- void Forward(){
- }
- void Reverse(){
- }
- void SonarSensor(int trigPin,int echoPin){
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPin, LOW);
- duration = pulseIn(echoPin, HIGH);
- Distance = (duration/2) / 29.1;
- if (Distance > MinSideDistance) {
- Distance=MinSideDistance;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement