Influencer Home Forums Arduino 4 Motor motor shield code.

Viewing 1 post (of 1 total)
  • Author
    Posts
  • hroc65
    Participant
    Post count: 1

    I built a 4 Dc motor Robot. No Servos just DC Motor only.  I have this code that runs all 4 motors But Motor  M2 is having issues…..I’m using a Ps2x Controller to run it. Motors M1,M3 And M4 Work Just fine. But M2 Runs forward But Won’t run backward when I press the ps2 controller button.    Please someone .Take a look at this code to see what the issue could be…I’ don’t see one.    The motor shield works just fine when i test the M1-M4, Connections Separately. SEE CODE:

    #include <Wire.h>
    #include “PS2X_lib.h”
    #include “Adafruit_MotorShield.h”
    #include “Adafruit_MS_PWMServoDriver.h”

    #define TRIGGER_PIN A0
    #define ECHO_PIN A1
    #define SPEED 255

    Adafruit_MotorShield AFMS = Adafruit_MotorShield();
    PS2X ps2x;
    //Ultrasonic ultrasonic(TRIGGER_PIN, ECHO_PIN);

    long ARM_MIN[] = {10, 10, 40, 10};
    long ARM_MAX[] = {170, 140, 170, 102};

    Adafruit_Servo *Servo1 = AFMS.getServo(1);
    Adafruit_Servo *Servo2 = AFMS.getServo(2);
    Adafruit_Servo *Servo3 = AFMS.getServo(3);
    Adafruit_Servo *Servo4 = AFMS.getServo(4);
    Adafruit_DCMotor *DCMotor_1 = AFMS.getMotor(1);
    Adafruit_DCMotor *DCMotor_3 = AFMS.getMotor(3);
    Adafruit_DCMotor *DCMotor_4 = AFMS.getMotor(4);
    Adafruit_DCMotor *DCMotor_2 = AFMS.getMotor(2);

     

    //Grab something
    void openGripper() {
    Servo4->writeServo(ARM_MIN[3]);
    delay(300);
    }

    void closeGripper() {
    Servo4->writeServo(ARM_MAX[3]);
    delay(300);
    }

    void setup() {
    AFMS.begin(50);
    int error = 0;
    do {
    error = ps2x.config_gamepad(13, 11, 10, 12, true, true);
    if (error == 0) {
    break;
    } else {
    delay(100);
    }
    } while (1);
    Servo1->writeServo(90);
    Servo2->writeServo(90);
    Servo3->writeServo(90);
    Servo4->writeServo(60);
    }

     

    void loop() {
    ps2x.read_gamepad(false, 0);
    if (ps2x.Button(PSB_PAD_UP)) {
    DCMotor_3->setSpeed(SPEED);
    DCMotor_4->setSpeed(SPEED);
    if (ps2x.Button(PSB_L1)) {
    DCMotor_3->setSpeed(0);
    }
    if (ps2x.Button(PSB_R1)) {
    DCMotor_4->setSpeed(0);
    }
    DCMotor_3->run(FORWARD);
    DCMotor_4->run(FORWARD);
    } else if (ps2x.Button(PSB_PAD_DOWN)) {
    DCMotor_3->setSpeed(SPEED);
    DCMotor_4->setSpeed(SPEED);
    if (ps2x.Button(PSB_L1)) {
    DCMotor_3->setSpeed(0);
    }
    if (ps2x.Button(PSB_R1)) {
    DCMotor_4->setSpeed(0);
    }
    DCMotor_3->run(BACKWARD);
    DCMotor_4->run(BACKWARD);
    } else if (ps2x.Button(PSB_PAD_LEFT)) {
    DCMotor_3->setSpeed(SPEED);
    DCMotor_3->run(BACKWARD);
    DCMotor_4->setSpeed(SPEED);
    DCMotor_4->run(FORWARD);
    } else if (ps2x.Button(PSB_PAD_RIGHT)) {
    DCMotor_3->setSpeed(SPEED);
    DCMotor_3->run(FORWARD);
    DCMotor_4->setSpeed(SPEED);
    DCMotor_4->run(BACKWARD);
    } else {
    DCMotor_3->setSpeed(0);
    DCMotor_3->run(RELEASE);
    DCMotor_4->setSpeed(0);
    DCMotor_4->run(RELEASE);
    }

    if (ps2x.Button(PSB_CROSS)) {
    ps2x.read_gamepad(true, 200);
    delay(300);
    ps2x.read_gamepad(false, 0);
    }
    if (ps2x.Button(PSB_R2)) {
    DCMotor_1->setSpeed(SPEED);
    DCMotor_1->run(BACKWARD);
    } else if (ps2x.ButtonReleased(PSB_R2)) {
    DCMotor_1->setSpeed(0);
    DCMotor_1->run(RELEASE);
    }
    if (ps2x.Button(PSB_L2)) {
    DCMotor_1->setSpeed(SPEED);
    DCMotor_1->run(FORWARD);
    } else if (ps2x.ButtonReleased(PSB_L2)) {
    DCMotor_1->setSpeed(0);
    DCMotor_1->run(RELEASE);
    }

    if (ps2x.Button(PSB_CROSS)) {
    ps2x.read_gamepad(true, 200);
    delay(300);
    ps2x.read_gamepad(false, 0);

    if (ps2x.Button(PSB_CROSS))
    DCMotor_2->setSpeed(SPEED);
    DCMotor_2->run(BACKWARD);
    } else if (ps2x.ButtonReleased(PSB_CROSS))
    DCMotor_2->setSpeed(0);
    DCMotor_2->run(RELEASE);

    if (ps2x.Button(PSB_TRIANGLE)){
    DCMotor_2->setSpeed(SPEED);
    DCMotor_2->run(FORWARD);
    } else if (ps2x.ButtonReleased(PSB_TRIANGLE))
    DCMotor_2->setSpeed(0);
    DCMotor_2->run(RELEASE);
    }

Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.