Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I want to design a wall following robot with only one ultrasonic sensor by using Arduino. Several parts of the code have already been written

I want to design a wall following robot with only one ultrasonic sensor by using Arduino. Several parts of the code have already been written by me and I do not what logic should I use with only one sensor, especially under the scenario where the robot meets the corner. What logic should I use and how will the code look like? Code attached is what I have written: #include //servo library Servo my servo; // create servo object to control servo int Echo = A4; int Trig = A5; #define ENB 5 #define IN1 7 #define IN2 8 #define IN3 9 #define IN4 11 #define ENA 6 #define car Speed 200 int right Distance = 0, left Distance = 0, middle Distance = 0; void forward(){ analogy Write(ENA, car Speed); analogWrite(ENB, car Speed); digital Write(IN1, HIGH); digital Write(IN2, LOW); digital Write(IN3, LOW); digital Write(IN4, HIGH); Serial.println("Forward"); } void back() { analogWrite(ENA, car Speed); analogWrite(ENB, car Speed); digital Write(IN1, LOW); digital Write(IN2, HIGH); digital Write(IN3, HIGH); digital Write(IN4, LOW); Serial.println("Back"); } void left() { analogy Write(ENA, car Speed); analogWrite(ENB, car Speed); digital Write(IN1, LOW); digital Write(IN2, HIGH); digital Write(IN3, LOW); digital Write(IN4, HIGH); Serial.println("Left"); } void right() { analogWrite(ENA, car Speed); analogWrite(ENB, car Speed); digital Write(IN1, HIGH); digital Write(IN2, LOW); digital Write(IN3, HIGH); digital Write(IN4, LOW); Serial.println("Right"); } void stop() { digital Write(ENA, LOW); digital Write(ENB, LOW); Serial.println("Stop!"); } //Ultrasonic distance measurement Sub function int get Distance() { digital Write(Trig, LOW); delay Microseconds(2); digital Write(Trig, HIGH); delay Microseconds(10); digital Write(Trig, LOW); return (int)pulse In(Echo, HIGH) / 58; } void setup() { myservo.attach(3); // attach servo on pin 3 to servo object Serial. Begin(9600); pin Mode(Echo, INPUT); pin Mode(Trig, OUTPUT); pin Mode(IN1, OUTPUT); pin Mode(IN2, OUTPUT); pin Mode(IN3, OUTPUT); pin Mode(IN4, OUTPUT); pin Mode(ENA, OUTPUT); pin Mode(ENB, OUTPUT); stop(); }

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: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

Design Operation And Evaluation Of Mobile Communications

Authors: Gavriel Salvendy ,June Wei

1st Edition

3030770249, 978-3030770242

More Books

Students also viewed these Programming questions

Question

600 lb 20 0.5 ft 30 30 5 ft

Answered: 1 week ago