Arduino Mega, Accelstepper & 28BYJ48 issue

Hi everyone. I’m new to the Arduino (but have programmed lots before) and I have an issue that I can’t seem to fix. I think the answer’s obvious, but I’m not seeing it! The documentation for the Accelstepper.h library isn’t helping me.

I’m using a standard Radio Control transmitter wired to the Arduino. The X/Y positions of one of the sticks are being read by the Arduino and needs to control a stepper motor.

The stepper motor starts at position 0. (Stick X=0, Y=lots, angle = 0). I can get the stepper to track clockwise or anticlockwise to follow the angle of the stick, no problem. The stepper moves from -2048 steps (-180 degrees) to +2048 steps (+180 degrees).

If I move the stick beyond these end points (Stick X=0, Y=-lots) I want the stepper to follow. At the moment, it takes “the long way around”. Which isn’t the result I’m after.

To calculate the “shortest direction” I’m using the following code:

if(buttonState==1){
    // read stick x and y positions
    int xValue = 510 - analogRead(A4);
    int yValue = analogRead(A5) - 513;
    // value in radians
    float stickAngle = atan2(xValue, yValue);
    // value in step units
    float stickTarget = stickAngle*2048/3.14159;
    // calculate best direction to turn
    float stepperCurrentPosition = stepper2.currentPosition();
    float difference = stepperCurrentPosition - stickTarget;
    if(difference<-2048){
      difference = difference + 4096;
    }
    if(difference>2048){
      difference = difference - 4096;
    }
    if(difference<0){
      // needs to move clockwise
      stepper2.moveTo(stickTarget);
    }else{
      // needs to move anticlockwise
      stepper2.moveTo(stickTarget);
    }
    stepper2.run();
  } else {
    stepper2.moveTo(0);
    stepper2.run();
  }

The question:

What commands do I need to use to ensure the step movement to stickTarget is clockwise or anticlockwise?

Many thanks for any help with this!

Andy

I don't think I understand your question sufficiently well to offer advice.

You say the motors move (say) CW when you push the stick to the right and (I assume) the distance moved is proportional to how far the stick is pushed (and CCW when you push the stick to the left). From stick full left to stick full right, the motor move through a full 360 degrees.

If I have that right, then I don't follow the rest of it.

...R

Hi Robin,

The joystick can move around in an X/Y plane. I’m measuring the angle of the stick relative to the Y-axis: it’s between +/-180 degrees. This angle is converted to steps for the stepper motor’s 360 degree rotation (+/-2048 steps).

Within limits, the stepper motor is matching the stick angle - it’s moving to follow the stick. Result!

However, there’s a point at which “common sense” would move the stepper across the -Y axis, but the stepper takes the long route, all the way back through the +Y axis to reach the stick angle.

This bit of my code …

    if(difference<0){
      // move clockwise
      stepper2.moveTo(stickTarget);
    }else{
      // move anticlockwise
      stepper2.moveTo(stickTarget);
    }

… is the solution to force the stepper to take the correct route, but I don’t see how, using accelStepper (and the moveTo command) I can force a clockwise or anticlockwise direction of movement.

Andy

You need to measure the resistance or voltage from the joysticks and post that with your code. Without knowing that information the code is not going to be much help. If you provide reliable resistance or voltage information for the "stick" , then debugging the code becomes more straightforward. We can't be sure you wired it correctly. I'm sure you understand we need more that a discription of how "think" it is connected. If you post the information requested we don't need you to tell us how it is connected.
Why are there no debug serial print statements in your code ? You should already be doing that before you post here. If you haven't done that, then you need to do it. That will tell what is going on.

Ah! Solved!

Example: suppose the stepper motor is located at -1800 steps. It’s doing the sensible thing to track to the stick target at +1600 steps by going through step 0. What I needed to do was make the stepper think it’s “a turn ahead” at +2296 steps in order for it to cross the -Y axis. And ensure that “fixing the stepper’s location” only occurs for movements across the -Y axis.

    if(abs(stickAngle)>1.57){
      if(stepperCurrentPosition<0 && stickTarget>0){
        stepper2.setCurrentPosition(4096+stepperCurrentPosition);
      }
      if(stepperCurrentPosition>0 && stickTarget<0){
        stepper2.setCurrentPosition(stepperCurrentPosition-4096);
      }
    }
    stepper2.moveTo(stickTarget);
    stepper2.run();

Thanks, Robin, for making me re-evaluate what’s really going on here. I think a cup of tea helped, too.

What’s it for? Well, I want full turret control and automatic tracking (courtesy of a MPU-6050) for this brute:

… a 1/72nd scale model of HMS Dreadnought. ]:slight_smile:

Andy

Glad you have figured it out.

Impressive model. You will need a big pond.

...R

Impressive model.

Ditto that. If you ever get it in the water send us a photo.