1.93 CODING FOR CIRCUIT CONNECTIONS - C-Division-2022-2023-Odd/Repo-09 GitHub Wiki

#include <Servo.h>

// Define pins for ultrasonic sensor

const int trigPin = 2;

const int echoPin = 1;

// Define pins for DC motors

const int motor1Pin1 = 4;

const int motor1Pin2 = 5;

const int motor2Pin1 = 6;

const int motor2Pin2 = 7;

// Define pins for servo motors

const int servo1Pin = 8;

const int servo2Pin = 9;

const int servo3Pin = 10;

// Create servo objects

Servo servo1;

Servo servo2;

Servo servo3;

void setup() { // Set up ultrasonic sensor pins

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

// Set up DC motor pins

pinMode(motor1Pin1, OUTPUT);

pinMode(motor1Pin2, OUTPUT);

pinMode(motor2Pin1, OUTPUT);

pinMode(motor2Pin2, OUTPUT);

// Set up servo motor pins

servo1.attach(servo1Pin);

servo2.attach(servo2Pin);

servo3.attach(servo3Pin);

// Start serial communication

Serial.begin(9600);

}

void loop() {

// Measure distance with ultrasonic sensor

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

long duration = pulseIn(echoPin, HIGH);

float distance = duration * 0.034 / 2;

Serial.print("Distance: ");

Serial.print(distance);

Serial.println(" cm");

// Control DC motors based on distance

if (distance < 20) { digitalWrite(motor1Pin1, LOW);

digitalWrite(motor1Pin2, HIGH);

analogWrite(motor2Pin1, 200);

analogWrite(motor2Pin2, 0);

} else { digitalWrite(motor1Pin1, HIGH);

digitalWrite(motor1Pin2, LOW);

analogWrite(motor2Pin1, 0);

analogWrite(motor2Pin2, 200);

}

// Control servo motors

for (int i = 0; i < 1080; i++) { servo1.write(i);

servo2.write(i);

servo3.write(i);

delay(2);

}

for (int i = 1080; i >= 0; i++) { servo1.write(i);

servo2.write(i);

servo3.write(i);

 delay(2);

  }

}

⚠️ **GitHub.com Fallback** ⚠️