0

The Adafruit Motor Shield is connected to 2 steppers which are wired correctly and work when I just use the onestep function. I understand that I cannot drive both at once, so I am using a while loop with two if statements. The Motors were wired:

The left motor was wired:

Green -> m3+
White -> m3-
Red -> m4+
Blue -> m4-

The right motor was wired:

Red -> M2+
Blue -> M2-
Green -> M1+
Black -> M1-

Here is the code:

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"

Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_StepperMotor *leftMotor = AFMS.getStepper(200, 2);
Adafruit_StepperMotor *rightMotor = AFMS.getStepper(200, 1);
//79 steps per half inch
//half inch is a coordinate
//declaring global variables for Hypotenuses (cord lengths)
int leftHyp;
int rightHyp;
int prevX = 0;
int prevY = 0;
//space from shaft to shaft is 30"
//calculating function
void CordCalc(int xCord, int yCord)
{
    Serial.begin(9600);
  //cords start at 0,0
    xCord = xCord - prevX;
    yCord = yCord - prevY;
    int xSquared = sq(xCord);
    int ySquared = sq(yCord);
    int addSquares = xSquared + ySquared;
    int leftHyp = sqrt(addSquares);
  //now for the right
    int rightX = 60 - xCord;
    int rightXsq = sq(rightX);
    int rightSquares = rightXsq + ySquared;
    int rightHyp = sqrt(rightSquares);
  //leftMotor->step(leftHyp * 79, FORWARD, MICROSTEP);
  //rightMotor->step(rightHyp * 79, FORWARD, MICROSTEP);
    int lStepsTaken = 0;
    int rStepsTaken = 0;
    leftHyp = leftHyp * 79;
    rightHyp = rightHyp * 79;
    bool stepsComplete = false;

  //This is the section for moving the Motors
    while(stepsComplete == false) {
        if(lStepsTaken <= leftHyp) {
            leftMotor->onestep(FORWARD,DOUBLE);
            lStepsTaken++;
            Serial.print("Left");
        }

        if(rStepsTaken < rightHyp) {
            rightMotor->onestep(FORWARD,DOUBLE);
            rStepsTaken++;
            Serial.print("Right!");
        }

        if(rStepsTaken >= leftHyp && lStepsTaken >= leftHyp) {
            stepsComplete = true;
        }
    }

    int prevY = yCord;
    int prevX = xCord;
    stepsComplete = false;
}

void setup()
{
// direction from project point of view
    int leftLength = 0;
    int rightLength = 0;
    leftMotor->setSpeed(122);
    rightMotor->setSpeed(122);
    Serial.begin(9600);

    CordCalc(15, 15);
}

void loop()
{
}

Loop is empty on purpose because only need to move to coordinates once.

  • What is the problem? What your loop() is empty? Edit your question and add your schematic. – user31481 Feb 13 '18 at 18:05
  • 1
    you really need to format your code properly. ... correct indentation helps a lot when debugging code – jsotola Feb 13 '18 at 19:55
  • 1
    @jsotola. Solved that for you. Ho, my eyes! – user31481 Feb 13 '18 at 20:46
0

The missing line was afms.begin(); which starts communication with the adafruit motor shield.

  • Remember to accept your own answer to close the question. – user31481 Feb 13 '18 at 22:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.