Question: Does this code meet these requirements? Your code must include the following: Decision Making. Looping. User - defined functions. The code must include three (

Does this code meet these requirements?
Your code must include the following:
Decision Making.
Looping.
User-defined functions. The code must include three (3) user-defined functionsno more, no less.
These functions must be used/called for main functionality.
At least one function must take at least one parameter. The function must use the parameter.
At least one function must return something. The return must be used by the receiving end.
At least one function that takes at least one parameter and returns something. Both the parameter and return must be used.
No function should be void/void. In other words, user-defined functions must meet 1.2 and/or 1.3 above.
Best-Practices. Your code must be written with best practices in mind. A few reminders below:
Variables.
Meaningful names and adequate data types.
If you have a value used in multiple places, put it into a variable (if it changes) or into a "DEFINE" or "CONST" (if it does not change), and use the variable/constant as you go through the code. For example, if your full speed is 150, instead of using the value 150 in multiple places, store the variable speed =150. This variable can then be used in different places of the code and as "speed/2" for half speed or similar variations. Ensure consistency with the use of the variables/constants.
White space. White space is encouraged for indentations of your code "in the right places" and to "break" the code into sections such as variables, function calls, printf/scanf statements, operations, etc.
Clean Code. Remove all lines of code that have no functionality (other than the required header/comments). This includes:
debugging lines
default Arduino-embedded comments
unused variables
code that is not meant to meet the provided requirements.
Please use in-program "comments by requirement" ONLY and a complete header. NOTE: Over-commented code and comments with functionality descriptions will not be allowed for demos.
#include
#define PIN_Motor_AIN_17
#define PIN_Motor_PWMB 6
#define PIN_Motor_PWMA 5
#define PIN_Motor_BIN_18
#define PIN_Motor_STBY 3
#define ECHO_PIN 12
#define TRIG_PIN 13
#define FULL_SPEED 150
#define CAUTION_SPEED 50
#define CLOSE_ENOUGH_DISTANCE 50
#define TOO_CLOSE_DISTANCE 10
#define ENOUGH_SPACE_DISTANCE 110
#define BACKUP_DISTANCE 20
Servo servo;
int angle =90;
struct Distances {
int left;
int right;
};
void setup(){
Serial.begin(9600);
pinMode(PIN_Motor_PWMA, OUTPUT);
pinMode(PIN_Motor_PWMB, OUTPUT);
pinMode(PIN_Motor_AIN_1, OUTPUT);
pinMode(PIN_Motor_BIN_1, OUTPUT);
pinMode(PIN_Motor_STBY, OUTPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
servo.attach(10);
servo.write(angle);
}
void loop(){
digitalWrite(PIN_Motor_STBY, HIGH);
float distance = readPing();
if (distance > ENOUGH_SPACE_DISTANCE){
moveForward(FULL_SPEED);
} else if (distance > CLOSE_ENOUGH_DISTANCE && distance <= ENOUGH_SPACE_DISTANCE){
moveForward(CAUTION_SPEED);
} else if (distance > TOO_CLOSE_DISTANCE && distance <= CLOSE_ENOUGH_DISTANCE){
Distances distances = Peer();
if (distances.right >= distances.left){
turnRight();
} else {
turnLeft();
}
delay(1500);
} else {
moveBackward();
delay(1000);
Stop();
delay(1000);
}
}
Distances Peer(){
Distances distances;
lookLeft();
delay(1000);
distances.left = readPing();
lookStraight();
delay(500);
lookRight();
delay(1000);
distances.right = readPing();
lookStraight();
delay(500);
return distances;
}
int readPing(){
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
unsigned long duration_us = pulseIn(ECHO_PIN, HIGH);
float distance_cm =0.017* duration_us;
Serial.print("distance: ");
Serial.print(distance_cm);
Serial.println(" cm");
return distance_cm;
}
void Stop(){
digitalWrite(PIN_Motor_AIN_1, HIGH);
analogWrite(PIN_Motor_PWMA, 0);
digitalWrite(PIN_Motor_BIN_1, HIGH);
analogWrite(PIN_Motor_PWMB,0);
}
void moveForward(int speed){
digitalWrite(PIN_Motor_AIN_1, HIGH);
analogWrite(PIN_Motor_PWMA, speed);
digitalWrite(PIN_Motor_BIN_1, HIGH);
analogWrite(PIN_Motor_PWMB, speed);
}
void moveBackward(){
digitalWrite(PIN_Motor_AIN_1, LOW);
analogWrite(PIN_Motor_PWMA, FULL_SPEED);
digitalWrite(PIN_Motor_BIN_1, LOW);
analogWrite(PIN_Motor_PWMB, FULL_SPEED);
}
void lookRight(){
Stop();
servo.write(150);
}
void lookStraight(){
Stop();
servo.write(90);
}
void lookLeft(){
Stop();
servo.write(40);
}
void turnLeft(){
digitalWrite(PIN_Motor_AIN_1, HIGH);
analogWrite(PIN_Motor_PWMA, 0);
digitalWrite(PIN_Motor_BIN_1, HIGH);
analogWrite(PIN_Motor_PWMB,100);
}
void turnRight(){
digitalWrite(PIN_Motor_AIN_1, HIGH);
analogWrite(PIN_Motor_PWMA, 100);
digitalWrite(PIN_Motor_BIN_1, HIGH);
analogWrite(PIN_Motor_PWMB,0);
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!