3D
3D
h>
#define TRIGGER_PIN 10
#define ECHO_PIN 11
#define MAX_DISTANCE 200 // Maximum distance (in cm) to detect obstacles (2 meters)
#define LED_PIN 9
#define BUZZER_PIN 8
void setup() {
pinMode(ENA, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
// motors off
stopMotors();
// turn off the LED and buzzer initially
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
void loop() {
stopMotors();
digitalWrite(LED_PIN, HIGH);
digitalWrite(BUZZER_PIN, HIGH);
// delay to indicate the LED and Buzzer activation for a short duration
delay(500);
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
} else {
// If the obstacle is out of the 2-meter range, turn off the LED and Buzzer
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
int desiredSpeed = map(analogRead(A0), 0, 1023, 0, 255); // Adjust A0 to the analog input you are using.
// update motor speed
if (desiredSpeed != motorSpeed) {
motorSpeed = desiredSpeed;
analogWrite(ENA, motorSpeed);
if (digitalRead(4) == HIGH) { // Connect a button to digital pin 4 for reversing the motor
// toggle direction
isForward = !isForward;
if (isForward) {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
} else {
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
delay(100);
void stopMotors() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
analogWrite(ENA, 0);