Question
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started