int motor1Pin1 = 27;
int motor1Pin2 = 26;
int enable1Pin = 14;
const int trigPin = 5;
const int echoPin = 18;
const int trigPin2 = 25;
const int echoPin2 = 33;
const int trigPin3 = 2;
const int echoPin3 = 4;
// Setting PWM properties
const int freq = 30000;
const int pwmChannel = 0;
const int resolution = 8;
int dutyCycle = 200;
//define sound speed in cm/uS
#define SOUND_SPEED 0.034
#define CM_TO_INCH 0.393701
long duration;
long duration1;
long duration2;
float distanceCm;
float distanceCm2;
float distanceCm3;
float distanceInch;
void setup() {
// sets the pins as outputs:
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(enable1Pin, OUTPUT);
Serial.begin(115200); // Starts the serial communication
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(trigPin2, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin2, INPUT); // Sets the echoPin as an Input
pinMode(trigPin3, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin3, INPUT); // Sets the echoPin as an Input
// configure LED PWM functionalitites
ledcSetup(pwmChannel, freq, resolution);
// attach the channel to the GPIO to be controlled
ledcAttachPin(enable1Pin, pwmChannel);
Serial.begin(115200);
// testing
Serial.print("Testing DC Motor...");
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculate the distance
distanceCm = duration * SOUND_SPEED/2;
// Convert to inches
distanceInch = distanceCm * CM_TO_INCH;
// Prints the distance in the Serial Monitor
// Serial.print("Distance (inch): ");
// Serial.println(distanceInch);
delay(1000);
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration1 = pulseIn(echoPin2, HIGH);
// Calculate the distance
distanceCm2 = duration1 * SOUND_SPEED/2;
// Convert to inches
distanceInch = distanceCm2 * CM_TO_INCH;
// Prints the distance in the Serial Monitor
// Serial.print("Distance (inch): ");
// Serial.println(distanceInch);
digitalWrite(trigPin3, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin3, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin3, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration2 = pulseIn(echoPin3, HIGH);
// Calculate the distance
distanceCm3 = duration2 * SOUND_SPEED/2;
// Convert to inches
distanceInch = distanceCm3* CM_TO_INCH;
// Prints the distance in the Serial Monitor
// Serial.print("Distance (inch): ");
// Serial.println(distanceInch);
Serial.print("Distance 1(cm): ");
Serial.println(distanceCm);
Serial.print("Distance2(cm): ");
Serial.println(distanceCm2);
Serial.print("Distance3(cm): ");
Serial.println(distanceCm3);
delay(1000);
// Move the DC motor forward at maximum speed
Serial.println("Moving Forward");
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
delay(2000);
// Stop the DC motor
Serial.println("Motor stopped");
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
delay(1000);
// Move DC motor backwards at maximum speed
Serial.println("Moving Backwards");
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
delay(2000);
// Stop the DC motor
Serial.println("Motor stopped");
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
delay(1000);
dutyCycle = 200;
}