Project Mania

 Robotics hand

#include <Servo.h>
Servo s1;  
Servo s2; 
Servo s3; 

int pos = 0;    // variable to store the servo position

void setup() {
s1.attach(3);
s2.attach(9); 
s3.attach(6);   
}

void loop() {
  delay(50);
  for (pos = 0; pos <= 60; pos += 1) 
  {                                     // s2 downwards
   s2.write(pos);              
    delay(50);                       
  }
  for (pos = 20; pos >= 0; pos -= 1)  
  {                                         //s1 close
    s3.write(pos);              
    delay(50);                      
  }
  
  for (pos = 60; pos >= 0; pos -= 1)  
  {                                         //s2 up
    s2.write(pos);              
    delay(50);                      
  }
  delay(50);
  for (pos = 0; pos <= 60; pos += 1)  
  {                                         //s1 left
    s1.write(pos);              
    delay(50); 
  }
  for (pos = 0; pos <= 60; pos += 1) 
  {                                     // s2 downwards
   s2.write(pos);              
    delay(50);                       
  }
  delay(50);
  for (pos = 0; pos <= 20; pos += 1) 
  {                                     // s1 open
   s3.write(pos);              
    delay(50);                       
  }
  for (pos = 60; pos >= 0; pos -= 1)  
  {                                         //s2 up
    s2.write(pos);              
    delay(50); 
  delay(50);
}
 for (pos = 60; pos >= 0; pos -= 1)  
  {                                         //s1 Right
    s1.write(pos);              
    delay(50); 
 
}
  }



Obstacles avoiding car



// Define pins for motor control
const int motor1Pin1 = 5;  // Motor A forward
const int motor1Pin2 = 4;  // Motor A backward
const int motor2Pin1 = 7;  // Motor B forward
const int motor2Pin2 = 6;  // Motor B backward

// Define pins for ultrasonic sensor
const int trigPin = 9;
const int echoPin = 10;

long duration;
int distance;

void setup() {
  // Initialize motor pins as output
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(motor2Pin1, OUTPUT);
  pinMode(motor2Pin2, OUTPUT);

  // Initialize ultrasonic sensor pins
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  // Start serial communication for debugging
  Serial.begin(9600);
}

void loop() {
  // Measure distance using the ultrasonic sensor
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.0344 / 2;

  // Print the distance for debugging
  Serial.print("Distance: ");
  Serial.println(distance);

  if (distance < 10) {  // If an obstacle is within 10 cm
    // Stop the motors
    stopMotors();
    delay(500); // Wait for a moment
    reverseMotors(); // Move backwards
    delay(1000); // Reverse for 1 second
    stopMotors(); // Stop the car
    turnRight();   // Turn right to avoid obstacle
    delay(500); // Turn for half a second
  } else {
    // Move forward if no obstacle is detected
    moveForward();
  }
}

// Function to move the car forward
void moveForward() {
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin1, HIGH);
  digitalWrite(motor2Pin2, LOW);
}

// Function to stop the car
void stopMotors() {
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin1, LOW);
  digitalWrite(motor2Pin2, LOW);
}

// Function to reverse the car
void reverseMotors() {
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, HIGH);
  digitalWrite(motor2Pin1, LOW);
  digitalWrite(motor2Pin2, HIGH);
}

// Function to turn the car right
void turnRight() {
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin1, LOW);
  digitalWrite(motor2Pin2, LOW);
  delay(500);  // Rotate the car by running motor 1 for a while
  stopMotors();
}

Comments

Post a Comment

Popular posts from this blog

Class 6

Class 8

Class 5