Go Down

Topic: Arduino Mega interfacing DC motor and Servo motor (Read 78 times) previous topic - next topic

Rajatsardana

Mar 17, 2018, 07:18 pm Last Edit: Mar 21, 2018, 04:05 pm by Rajatsardana
Hello sir/ma'am.. i am making an Arduino based project but am stuck a little with interfacing. As in, I can control the vehicle (DC motors) seperately and arms (Servo motors) seperately. But when I interface everything altogether servo starts malfunctioning when I send command for DC motors (via Bluetooth). It'll be great if you would help me in troubleshooting it.
P.S. I've connected 4 DC motors using Motor Driver IC L293D and Servo motors (SG 90) with a 5V DC power supply.

Code: [Select]
#include<Servo.h>

Servo myservo1; // pin 10  ARM_1 UP/DOWN
Servo myservo2; // pin 5   CLAW
int motor1Pin1 = 3; // pin 2 on L293D IC   (Forward Left) (3)
int motor1Pin2 = 4; // pin 7 on L293D IC   (Back) (6)
int motor2Pin1 = 11; // pin 10 on L293D IC (Back) (11)
int motor2Pin2 = 10; // pin 15 on L293D IC (Forward Right) (14)
int state;
int flag=0;        //makes sure that the serial only prints once the state
int stateStop=0;
int pos = 0;
int pos1 = 35;

void setup() {
    // sets the pins as outputs:
    pinMode(motor1Pin1, OUTPUT);
    pinMode(motor1Pin2, OUTPUT);
    pinMode(motor2Pin1, OUTPUT);
    pinMode(motor2Pin2, OUTPUT);
    myservo1.attach(5);
    myservo2.attach(9);
    // initialize serial communication at 9600 bits per second:
    Serial.begin(9600);
   digitalWrite(motor1Pin1, LOW);
    digitalWrite(motor1Pin2, LOW);
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, LOW);
   myservo2.write(pos);
   myservo1.write(pos1);
 
}

void loop() {
    //if some date is sent, reads it and saves in state
    if(Serial.available() > 0){     
      state = Serial.read();   
      flag=0;
    }   
    // if the state is '1' the DC motor will go forward
    if (state == '1') {
        digitalWrite(motor1Pin1, HIGH);
        digitalWrite(motor1Pin2, LOW);
        digitalWrite(motor2Pin1, LOW);
        digitalWrite(motor2Pin2, HIGH);
        Serial.println(state);
        if(flag == 0){
          Serial.println("Go Forward!");
          flag=1;
        }
      delay(100);
        state=3;
        stateStop=1;
    }
    // if the state is '2' the motor will Reverse
    else if (state == '2') {
        digitalWrite(motor1Pin1, LOW);
        digitalWrite(motor1Pin2, HIGH);
        digitalWrite(motor2Pin1, HIGH);
        digitalWrite(motor2Pin2, LOW);
        Serial.println(state);
        if(flag == 0){
          Serial.println("Reverse!");
          flag=1;
        }
      delay(100);
        state=3;
        stateStop=1;
    }
    // if the state is '3' the motor will turn right
    else if (state == '3') {
        digitalWrite(motor1Pin1, LOW);
        digitalWrite(motor1Pin2, LOW);
        digitalWrite(motor2Pin1, LOW);
        digitalWrite(motor2Pin2, HIGH);
        Serial.println(state);
        if(flag == 0){
          Serial.println("Turn RIGHT");
          flag=1;
        }
        delay(100);
        state=3;
        stateStop=1;
    }
    // if the state is '4' the motor will turn left
    else if (state == '4') {
        digitalWrite(motor1Pin1, HIGH);
        digitalWrite(motor1Pin2, LOW);
        digitalWrite(motor2Pin1, LOW);
        digitalWrite(motor2Pin2, LOW);
        Serial.println(state);
        if(flag == 0){
          Serial.println("Turn LEFT");
          flag=1;
        }
        delay(100);
        state=3;
        stateStop=1;
    }
    // if state is '5' the motor will Stop
    else if (state == '5') {
       digitalWrite(motor1Pin1, LOW);
       digitalWrite(motor1Pin2, LOW);
       digitalWrite(motor2Pin1, LOW);
       digitalWrite(motor2Pin2, LOW);
       Serial.println(state);
       if(flag == 0){
         Serial.println("STOP!");
         flag=1;
        }
      delay(100);
        state=3;
        stateStop=1;
    }
    // if state is '6' Arm will move up
    else if (state == '6') {
      myservo1.write(pos1 -= 10);
      Serial.println(state);
      Serial.println(pos1);
      if(flag == 0){
          Serial.println("Arm UP");
          flag=1;
        }
        delay(100);
        state=3;
        stateStop=1;
    }
    // if state is '7' Arm will move down
    else if (state == '7') {
      myservo1.write(pos1  += 10);
      Serial.println(state);
      Serial.println(pos1);
      if(flag == 0){
          Serial.println("Arm DOWN");
          flag=1;
        }
        delay(100);
        state=3;
        stateStop=1;
    }
    // if state is '8' Claw will grab
    else if (state == '8') {
      myservo2.write(pos -= 15);
      Serial.println(state);
      Serial.println(pos);
      if(flag == 0){
          Serial.println("Claw GRAB");
          flag=1;
        }
        delay(100);
        state=3;
        stateStop=1;
    }
    // if state is '9' Claw will drop
    else if (state == '9') {
      myservo2.write(pos += 15);
      Serial.println(state);
      Serial.println(pos1);
      if(flag == 0){
          Serial.println("Claw Drop");
          flag=1;
        }
        delay(100);
        state=3;
        stateStop=1;
    }
     stateStop=0;
}


I tried simulation for same and didn't find any issue with that. You can see the circuit below (I know it's difficult to understand with the wiring I made).

PaulRB

Hello sir..
The person who helps you is not guaranteed to be male...

Please read the forum guidelines in the sticky post. There, you will learn what things to include in your post so that we can help you.

Go Up