#include <ESP32Servo.h> // Use the ESP32 compatible Servo library
// Motor A Pins
const int motor1Pin1 = 27; // IN1
const int motor1Pin2 = 26; // IN2
const int enable1Pin = 14; // ENA
// Motor B Pins (if needed)
const int motor2Pin1 = 33; // IN3
const int motor2Pin2 = 25; // IN4
const int enable2Pin = 32; // ENB
// Servo Pin
Servo myServo;
const int servoPin = 12;
// Ultrasonic Sensor Pins
const int trigPin = 13;
const int echoPin = 14;
// TZT Microphone Pin
const int micPin = 34;
// Distance threshold in cm
const int distanceThreshold = 20; // Adjust as needed
void setup() {
Serial.begin(115200);
// Motor Pins
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(enable1Pin, OUTPUT);
// Motor B Pins (if needed)
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
pinMode(enable2Pin, OUTPUT);
// Servo Setup
myServo.attach(servoPin);
// Ultrasonic Sensor Setup
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Microphone Setup
pinMode(micPin, INPUT);
}
void loop() {
// Read distance from ultrasonic sensor
long distance = readUltrasonicDistance();
// Check if the distance is below the threshold
if (distance < distanceThreshold) {
stopMotors(); // Stop all motors if too close
Serial.println("Object too close! Stopping motors.");
} else {
// Read microphone input and control motors based on commands
int micValue = analogRead(micPin);
processMicrophoneCommand(micValue);
}
delay(100); // Short delay for stability
}
long readUltrasonicDistance() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration * 0.034) / 2; // Calculate distance in cm
return distance;
}
void processMicrophoneCommand(int micValue) {
// Simple command recognition based on micValue
// You will need to adjust these thresholds based on your microphone's output
if (micValue > 800) { // Example threshold for "move forward"
moveForward();
Serial.println("Moving Forward");
} else if (micValue > 600) { // Example threshold for "move backward"
moveBackward();
Serial.println("Moving Backward");
} else if (micValue > 400) { // Example threshold for "turn left"
turnLeft();
Serial.println("Turning Left");
} else if (micValue > 200) { // Example threshold for "turn right"
turnRight();
Serial.println("Turning Right");
} else { // If no command is recognized, stop
stopMotors();
Serial.println("Stopping Motors");
}
}
void moveForward() {
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
analogWrite(enable1Pin, 255); // Full speed
analogWrite(enable2Pin, 255); // Full speed
}
void moveBackward() {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
analogWrite(enable1Pin, 255); // Full speed
analogWrite(enable2Pin, 255); // Full speed
}
void turnLeft() {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
analogWrite(enable1Pin, 255); // Full speed
analogWrite(enable2Pin, 255); // Full speed
}
void turnRight() {
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
analogWrite(enable1Pin, 255); // Full speed
analogWrite(enable2Pin, 255); // Full speed
}
I moved your topic to an appropriate forum category @idrisap .
In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.
This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.
Thanks in advance for your cooperation.
Can you please help , its actully urgent.
This is a group of volunteers, saying things like urgent or asap or hurry will only get you smart remarks.
This was fixed in your other post.
Please explain the problem. What did you expect the program to do, and what does it do instead?
i expected that the car will work on speech command, but codes are not getting upload and showing error
It might help us if you were to share the errors that are showing up…
a7
C:\Users\Admin\AppData\Local\Temp.arduinoIDE-unsaved2024114-964-1n149ks.1c4u\sketch_dec4a\sketch_dec4a.ino: In function 'void loop()':
C:\Users\Admin\AppData\Local\Temp.arduinoIDE-unsaved2024114-964-1n149ks.1c4u\sketch_dec4a\sketch_dec4a.ino:57:5: error: 'stopMotors' was not declared in this scope
57 | stopMotors(); // Stop all motors if too close
| ^~~~~~~~~~
C:\Users\Admin\AppData\Local\Temp.arduinoIDE-unsaved2024114-964-1n149ks.1c4u\sketch_dec4a\sketch_dec4a.ino: In function 'void processMicrophoneCommand(int)':
C:\Users\Admin\AppData\Local\Temp.arduinoIDE-unsaved2024114-964-1n149ks.1c4u\sketch_dec4a\sketch_dec4a.ino:97:5: error: 'stopMotors' was not declared in this scope
97 | stopMotors();
| ^~~~~~~~~~
exit status 1
Compilation error: 'stopMotors' was not declared in this scope
The compiler is correct (as usual). There is no function named stopMotors()
in the code you posted.
ok thankyou everyone i got it
but after executing the code the car is not working not giving response
if any idea
Post the revised code in your next post, and clearly explain the new problem.
"Not working" is not a useful description of a problem.
revised codes
#include <ESP32Servo.h> // Use the ESP32 compatible Servo library
// Motor A Pins
const int motor1Pin1 = 27; // IN1
const int motor1Pin2 = 26; // IN2
const int enable1Pin = 14; // ENA
// Motor B Pins (if needed)
const int motor2Pin1 = 33; // IN3
const int motor2Pin2 = 25; // IN4
const int enable2Pin = 32; // ENB
// Servo Pin
Servo myServo;
const int servoPin = 12;
// Ultrasonic Sensor Pins
const int trigPin = 13;
const int echoPin = 14;
// TZT Microphone Pin
const int micPin = 34;
// Distance threshold in cm
const int distanceThreshold = 20; // Adjust as needed
void setup() {
Serial.begin(115200);
// Motor Pins
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(enable1Pin, OUTPUT);
// Motor B Pins (if needed)
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
pinMode(enable2Pin, OUTPUT);
// Servo Setup
myServo.attach(servoPin);
// Ultrasonic Sensor Setup
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Microphone Setup
pinMode(micPin, INPUT);
}
void loop() {
// Read distance from ultrasonic sensor
long distance = readUltrasonicDistance();
// Check if the distance is below the threshold
if (distance < distanceThreshold) {
stopMotors(); // Stop all motors if too close
Serial.println("Object too close! Stopping motors.");
} else {
// Read microphone input and control motors based on commands
int micValue = analogRead(micPin);
processMicrophoneCommand(micValue);
}
delay(100); // Short delay for stability
}
long readUltrasonicDistance() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration * 0.034) / 2; // Calculate distance in cm
return distance;
}
void processMicrophoneCommand(int micValue) {
// Simple command recognition based on micValue
// You will need to adjust these thresholds based on your microphone's output
if (micValue > 800) { // Example threshold for "move forward"
moveForward();
Serial.println("Moving Forward");
} else if (micValue > 600) { // Example threshold for "move backward"
moveBackward();
Serial.println("Moving Backward");
} else if (micValue > 400) { // Example threshold for "turn left"
turnLeft();
Serial.println("Turning Left");
} else if (micValue > 200) { // Example threshold for "turn right"
turnRight();
Serial.println("Turning Right");
} else { // If no command is recognized, stop
stopMotors();
Serial.println("Stopping Motors");
}
}
void moveForward() {
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
analogWrite(enable1Pin, 255); // Full speed
analogWrite(enable2Pin, 255); // Full speed
}
void moveBackward() {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
analogWrite(enable1Pin, 255); // Full speed
analogWrite(enable2Pin, 255); // Full speed
}
void turnLeft() {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
analogWrite(enable1Pin, 255); // Full speed
analogWrite(enable2Pin, 255); // Full speed
}
void turnRight() {
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
analogWrite(enable1Pin, 255); // Full speed
analogWrite(enable2Pin, 255); // Full speed
}
void stopMotors() {
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
analogWrite(enable1Pin, 0); // Full speed
analogWrite(enable2Pin, 0); // Full speed
}
after coding or after running the codes the should work or do any movement but my car is not showing any movement even after i give speech commands it is not showing any movement.what could be the reason any idea?
Ask ChatGPT, and STOP saying CODES, it's just CODE.
Does any output like this appear on the serial monitor?
Serial.println("Moving Forward");
Put in extra print statements like the one above, to see what is going wrong.
+1. Below is a draft I forgot about finiching, I see now that something should print if processMicrophoneCommand() gets called.
So it should print one way or the other, if not
Object too close! Stopping motors.
then
Stopping Motors
Earlier:
Use printing. Maybe like
void processMicrophoneCommand(int micValue) {
Serial.print(" micValue = ");
Serial.println(micValues);
//...
You might need to play with your "speech recognition" module.
a7
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.