Question
I need flowchart for this code : #include UltraSonicDistanceSensor ultrasonic(A0,A1); float distance; // left motor int leftMotorSpeedPin = 3; int leftMotorForwardPin = 4; int leftMotorBackwardPin
I need flowchart for this code :
#include
UltraSonicDistanceSensor ultrasonic(A0,A1);
float distance;
// left motor
int leftMotorSpeedPin = 3;
int leftMotorForwardPin = 4;
int leftMotorBackwardPin = 5;
// right motor
int rightMotorSpeedPin = 11;
int rightMotorForwardPin = 12;
int rightMotorBackwardPin = 13;
void setup() {// Motor Control pins as outputs
pinMode(leftMotorSpeedPin, OUTPUT);
pinMode(leftMotorForwardPin, OUTPUT);
pinMode(leftMotorBackwardPin, OUTPUT);
pinMode(rightMotorSpeedPin, OUTPUT);
pinMode(rightMotorForwardPin, OUTPUT);
pinMode(rightMotorBackwardPin, OUTPUT);
Serial.begin(9600);// Initialize serial communication for debugging
digitalWrite(leftMotorSpeedPin, HIGH);
digitalWrite(rightMotorSpeedPin, HIGH);
}
void loop() {
distance = ultrasonic.measureDistanceCm();
//Use 'CM' for centimeters or 'INC' for inches
Serial.println(distance);// Print distance to serial monitor
if (distance > -1 && distance < 25)
{
stop();
delay(1000);
goBackward();
delay(300);
stop();
delay(1000);
if(random(0, 2) == 0)
{
goLeft();
}
else
{
goRight();
}
delay(500);
stop();
delay(700);
}
else {
goForward();
}
}
// Function to move the vehicle forward
void goForward() {
digitalWrite(leftMotorForwardPin, HIGH);
digitalWrite(leftMotorBackwardPin, LOW);
digitalWrite(rightMotorForwardPin, HIGH);
digitalWrite(rightMotorBackwardPin, LOW);
}
void goBackward() {
digitalWrite(leftMotorForwardPin, LOW);
digitalWrite(leftMotorBackwardPin, HIGH);
digitalWrite(rightMotorForwardPin, LOW);
digitalWrite(rightMotorBackwardPin, HIGH);
}
void stop() { // Function to stop the motors
digitalWrite(leftMotorForwardPin, LOW);
digitalWrite(leftMotorBackwardPin, LOW);
digitalWrite(rightMotorForwardPin, LOW);
digitalWrite(rightMotorBackwardPin, LOW);
}
void goRight() {
digitalWrite(leftMotorForwardPin, HIGH);
digitalWrite(leftMotorBackwardPin, LOW);
digitalWrite(rightMotorForwardPin, LOW);
digitalWrite(rightMotorBackwardPin, LOW);
}
void goLeft() {
digitalWrite(leftMotorForwardPin, LOW);
digitalWrite(leftMotorBackwardPin, LOW);
digitalWrite(rightMotorForwardPin, HIGH);
digitalWrite(rightMotorBackwardPin, LOW);
}
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