Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

i want help in arduino programming for graduation project i have 4 ultrasonic sensors , arduino mega and 4wd and l298n motor driver the idea

i want help in arduino programming for graduation project i have 4 ultrasonic sensors , arduino mega and 4wd and l298n motor driver the idea of the project is to park the car robot by it self in a suitable parking without hitting the other cars. like in real life. we have our program please combine it to make it work. // Motor A connections int enA = 6; int in1 = 5; int in2 = 7; // Motor B connections int enB = 9; int in3 = 10; int in4 = 11; //ultrasonic define int Echo1 = 35; int Trig1 = 34; int Echo2 = 43; int Trig2 = 42; int Echo3 = 45; int Trig3 = 44; int ABS = 200; int Left_Distance = 0, Right_Distance = 0, Middle_Distance = 0; void _mForward(){ digitalWrite(in1, HIGH); digitalWrite(in2, LOW); digitalWrite(in3, HIGH); digitalWrite(in4, LOW); analogWrite(enA, ABS); analogWrite(enB, ABS); Serial.print("ROBOT_MOVING_FARWARD"); } void _mBack(){ digitalWrite(in1, LOW); digitalWrite(in2, HIGH); digitalWrite(in3, LOW); digitalWrite(in4, HIGH); analogWrite(enA, ABS); analogWrite(enB, ABS); Serial.print("ROBOT_MOVING_BACKWARD"); } void _mleft(){ digitalWrite(in1, HIGH); digitalWrite(in2, LOW); digitalWrite(in3, LOW); digitalWrite(in4, LOW); analogWrite(enA, ABS); analogWrite(enB, ABS); Serial.print("ROBOT_MOVING_LEFT"); } void _mright(){ digitalWrite(in1, LOW); digitalWrite(in2, LOW); digitalWrite(in3, HIGH); digitalWrite(in4, LOW); analogWrite(enA, ABS); analogWrite(enB, ABS); Serial.print("ROBOT_MOVING_RIGHT"); } void _mStop(){ digitalWrite(in1, LOW); digitalWrite(in2, LOW); digitalWrite(in3, LOW); digitalWrite(in4, LOW); analogWrite(enA, LOW); analogWrite(enB, LOW); Serial.print("ROBOT_STOP"); } //Ultrasonic distance measurement: int Left_Distance_test(){ digitalWrite(Trig2, LOW); delayMicroseconds(2); digitalWrite(Trig2, HIGH); delayMicroseconds(20); digitalWrite(Trig2, LOW); float Fdistance = pulseIn(Echo2, HIGH); delay(10); Fdistance = Fdistance / 29 / 2; return (int)Fdistance; } int Middle_Distance_test(){ digitalWrite(Trig3, LOW); delayMicroseconds(2); digitalWrite(Trig3, HIGH); delayMicroseconds(20); digitalWrite(Trig3, LOW); float Fdistance = pulseIn(Echo3, HIGH); delay(10); Fdistance = Fdistance / 29 / 2; return (int)Fdistance; } int Right_Distance_test(){ digitalWrite(Trig1, LOW); delayMicroseconds(2); digitalWrite(Trig1, HIGH); delayMicroseconds(20); digitalWrite(Trig1, LOW); float Fdistance = pulseIn(Echo1, HIGH); delay(10); Fdistance = Fdistance / 29 / 2; return (int)Fdistance; } void setup() { // Set all the motor control pins to outputs pinMode(enA, OUTPUT); pinMode(enB, OUTPUT); pinMode(in1, OUTPUT); pinMode(in2, OUTPUT); pinMode(in3, OUTPUT); pinMode(in4, OUTPUT); Serial.begin(9600); pinMode(Echo1, INPUT); pinMode(Trig1, OUTPUT); pinMode(Echo2, INPUT); pinMode(Trig2, OUTPUT); pinMode(Echo3, INPUT); pinMode(Trig3, OUTPUT); _mStop(); // Turn off motors - Initial state digitalWrite(in1, LOW); digitalWrite(in2, LOW); digitalWrite(in3, LOW); digitalWrite(in4, LOW); } void loop() { directionControl(); delay(1000); speedControl(); delay(1000); Left_Distance = Left_Distance_test(); delay(10); Middle_Distance = Middle_Distance_test(); delay(10); Right_Distance = Right_Distance_test(); delay(10); if(Middle_Distance <= 20){ if(Right_Distance > Left_Distance){ if((Right_Distance <= 20) && (Left_Distance <= 20)){ _mStop(); delay(200); _mBack(); delay(1000); } else{ _mright(); delay(500); } } else if(Right_Distance < Left_Distance){ if((Right_Distance <= 20) && (Left_Distance <= 20)){ _mStop(); delay(200); _mBack(); delay(1000); } else{ _mleft(); delay(500); } } } else if(Right_Distance <= 15){ _mleft(); delay(500); } else if(Left_Distance <= 15){ _mright(); delay(500); } else{ _mForward(); } } // This function lets you control spinning direction of motors void directionControl() { // Set motors to maximum speed // For PWM maximum possible values are 0 to 255 analogWrite(enA, 255); analogWrite(enB, 255); // Turn on motor A & B digitalWrite(in1, HIGH); digitalWrite(in2, LOW); digitalWrite(in3, HIGH); digitalWrite(in4, LOW); delay(2000); // Now change motor directions digitalWrite(in1, LOW); digitalWrite(in2, HIGH); digitalWrite(in3, LOW); digitalWrite(in4, HIGH); delay(2000); // Turn off motors digitalWrite(in1, LOW); digitalWrite(in2, LOW); digitalWrite(in3, LOW); digitalWrite(in4, LOW); } // This function lets you control speed of the motors void speedControl() { // Turn on motors digitalWrite(in1, LOW); digitalWrite(in2, HIGH); digitalWrite(in3, LOW); digitalWrite(in4, HIGH); // Accelerate from zero to maximum speed for (int i = 0; i < 256; i++) { analogWrite(enA, i); analogWrite(enB, i); delay(20); } // Decelerate from maximum speed to zero for (int i = 255; i >= 0; --i) { analogWrite(enA, i); analogWrite(enB, i); delay(20); } // Now turn off motors digitalWrite(in1, LOW); digitalWrite(in2, LOW); digitalWrite(in3, LOW); digitalWrite(in4, LOW); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image
Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_step_2

Step: 3

blur-text-image_step3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students explore these related Databases questions