Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

create an activity diagram for this code: int trig_front = 9; int trig_rear = 8; int echo_front = 10; int echo_rear = 7; int motorpin_left1

create an activity diagram for this code:

int trig_front = 9;
int trig_rear = 8;
int echo_front = 10;
int echo_rear = 7;
int motorpin_left1 = 5;
int motorpin_left2 = 6;
int motorpin_right1 = 12;
int motorpin_right2 = 11;
float distance_front;
float distance_rear;

void setup() {
pinMode(trig_front, OUTPUT);
pinMode(trig_rear, OUTPUT);
pinMode(echo_front, INPUT);
pinMode(echo_rear, INPUT);
pinMode(motorpin_left1, OUTPUT);
pinMode(motorpin_left2, OUTPUT);
pinMode(motorpin_right1, OUTPUT);
pinMode(motorpin_right2, OUTPUT);
Serial.begin(9600);
}

void loop()
{
distance_front = measureDistance(trig_front,echo_front);
Serial.print("Front Distance");
Serial.println(distance_front);

if (distance_front > 3)
{ forward();
delay(200);
}
else{
distance_rear = measureDistance(trig_rear,echo_rear);
Serial.print("Rear Distance");
Serial.println(distance_rear);
}
transmitLocationData();
delay(1000);

String userInput = Serial.readString();{
processUserInput(userInput);
}
}
float measureDistance(int trigPin, int echoPin) {
digitalWrite(trig_front, LOW);
delayMicroseconds(2);
digitalWrite(trig_rear, HIGH);
delayMicroseconds(10);
digitalWrite(trig_front, LOW);
return pulseIn(echoPin, HIGH) / 58;
}

void processUserInput(String userInput) {
userInput.trim();
if (userInput.equalsIgnoreCase("forward")) {
forward();
} else if (userInput.equalsIgnoreCase("rear")) {
rear();
} else if (userInput.equalsIgnoreCase("left")) {
turnLeft();
} else if (userInput.equalsIgnoreCase("right")) {
turnRight();
} else if (userInput.equalsIgnoreCase("stop")) {
stopMotors();
} else {
Serial.println("Available commands: forward, backward, left, right, stop");
}
}

void forward() {
digitalWrite(motorpin_left1, HIGH);
digitalWrite(motorpin_left2, LOW);
digitalWrite(motorpin_right1, HIGH);
digitalWrite(motorpin_right2, LOW);
}

void rear() {
digitalWrite(motorpin_left1, HIGH);
digitalWrite(motorpin_left2, LOW);
digitalWrite(motorpin_right1, HIGH);
digitalWrite(motorpin_right2, LOW);
}

void turnLeft() {
digitalWrite(motorpin_left1, HIGH);
digitalWrite(motorpin_left2, LOW);
digitalWrite(motorpin_right1, HIGH);
digitalWrite(motorpin_right2, LOW);
}

void turnRight() {
digitalWrite(motorpin_left1, HIGH);
digitalWrite(motorpin_left2, LOW);
digitalWrite(motorpin_right1, HIGH);
digitalWrite(motorpin_right2, LOW);
}

void stopMotors() {
digitalWrite(motorpin_left1, HIGH);
digitalWrite(motorpin_left2, LOW);
digitalWrite(motorpin_right1, HIGH);
digitalWrite(motorpin_right2, LOW);
}

void transmitLocationData() {
Serial.println("Location data transmitted to main hub.");
}

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

Income Tax Fundamentals 2013

Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill

31st Edition

1111972516, 978-1285586618, 1285586611, 978-1285613109, 978-1111972516

More Books

Students also viewed these Programming questions