0% found this document useful (0 votes)
21 views4 pages

Project Coding

Uploaded by

talha 8byt Talha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views4 pages

Project Coding

Uploaded by

talha 8byt Talha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

i am making a robot, i want to programme it and make circuit of it, i have three dc motors with

two l298n motor driver to control it, a servo motor and 5 limit switches, when i starts it the first
& second dc motor start moving, then the first limit switch gets pressed and both dc motors starts
moving in opposite direction, after that the first and second dc motor reaches at the point where
the second limit switch gets pressed, both first and second dc motor stops moving, the servo
motor rotates to 45 degree and after a 1 sec delay the third dc motor gets starts and after 2 sec
delay, the servo motor gets back to its original position and at a specific point, the third limit
switch gets pressed and the third dc motor gets stopped and after a 2 sec delay, both first and
second dc motors starts moving again in forward direction, after a while, the fourth limit switch
gets pressed and both first and second dc motors starts moving in reverse direction and then after
sometime, the second limit switch gets pressed again and both first and second dc motors stops,
and the servo motor rotates to 45 degree and after a 1 sec delay, the third dc motor starts moving
in reverse direction and after 2 sec the servo gets back to its original position, and after that the
fifth limit switch gets pressed and the third dc motor stops moving and a 2 sec delay to gets start
both first and second dc motors in forward direction

#include <Servo.h>

// Motor Pins
const int M1_IN1 = 2;
const int M1_IN2 = 3;
const int M2_IN1 = 4;
const int M2_IN2 = 5;
const int M3_IN1 = 6;
const int M3_IN2 = 7;

// Servo
const int SERVO_PIN = 13;
Servo myServo;

// Limit Switches
const int LIMIT1 = 8;
const int LIMIT2 = 9;
const int LIMIT3 = 10;
const int LIMIT4 = 11;
const int LIMIT5 = 12;

void setup() {
// Motor pins
pinMode(M1_IN1, OUTPUT);
pinMode(M1_IN2, OUTPUT);
pinMode(M2_IN1, OUTPUT);
pinMode(M2_IN2, OUTPUT);
pinMode(M3_IN1, OUTPUT);
pinMode(M3_IN2, OUTPUT);

// Limit switch inputs


pinMode(LIMIT1, INPUT_PULLUP);
pinMode(LIMIT2, INPUT_PULLUP);
pinMode(LIMIT3, INPUT_PULLUP);
pinMode(LIMIT4, INPUT_PULLUP);
pinMode(LIMIT5, INPUT_PULLUP);

// Servo setup
myServo.attach(SERVO_PIN);
myServo.write(0); // initial position

// Start Serial for debugging (optional)


Serial.begin(9600);
}

// Motor control helpers


void motor1Forward() { digitalWrite(M1_IN1, HIGH);
digitalWrite(M1_IN2, LOW); }
void motor1Backward() { digitalWrite(M1_IN1, LOW);
digitalWrite(M1_IN2, HIGH); }
void motor1Stop() { digitalWrite(M1_IN1, LOW);
digitalWrite(M1_IN2, LOW); }

void motor2Forward() { digitalWrite(M2_IN1, HIGH);


digitalWrite(M2_IN2, LOW); }
void motor2Backward() { digitalWrite(M2_IN1, LOW);
digitalWrite(M2_IN2, HIGH); }
void motor2Stop() { digitalWrite(M2_IN1, LOW);
digitalWrite(M2_IN2, LOW); }

void motor3Forward() { digitalWrite(M3_IN1, HIGH);


digitalWrite(M3_IN2, LOW); }
void motor3Backward() { digitalWrite(M3_IN1, LOW);
digitalWrite(M3_IN2, HIGH); }
void motor3Stop() { digitalWrite(M3_IN1, LOW);
digitalWrite(M3_IN2, LOW); }

void loop() {
// Step 1: Start motor 1 and 2 forward
motor1Forward();
motor2Forward();

// Step 2: Wait for limit switch 1


while (digitalRead(LIMIT1) == HIGH);

// Step 3: Reverse motor 1 and 2


motor1Backward();
motor2Backward();

// Step 4: Wait for limit switch 2


while (digitalRead(LIMIT2) == HIGH);

// Step 5: Stop motors 1 and 2


motor1Stop();
motor2Stop();

// Step 6: Move servo to 45 degrees


myServo.write(45);
delay(1000);

// Step 7: Start motor 3


motor3Forward();
delay(2000);

// Step 8: Return servo to original position


myServo.write(0);

// Step 9: Wait for limit switch 3


while (digitalRead(LIMIT3) == HIGH);
motor3Stop();
delay(2000);

// Step 10: Start motor 1 and 2 forward


motor1Forward();
motor2Forward();

// Step 11: Wait for limit switch 4


while (digitalRead(LIMIT4) == HIGH);

// Step 12: Reverse motors 1 and 2


motor1Backward();
motor2Backward();

// Step 13: Wait for limit switch 2 again


while (digitalRead(LIMIT2) == HIGH);
motor1Stop();
motor2Stop();
// Step 14: Move servo to 45 degrees
myServo.write(45);
delay(1000);

// Step 15: Start motor 3 in reverse


motor3Backward();
delay(2000);

// Step 16: Return servo


myServo.write(0);

// Step 17: Wait for limit switch 5


while (digitalRead(LIMIT5) == HIGH);
motor3Stop();
delay(2000);

// Step 18: Start motor 1 and 2 forward again


motor1Forward();
motor2Forward();

// Loop restarts or stops


while (1); // or remove this to repeat continuously
}

You might also like