I Need help with my Invention

Hello Everyone, well I am stuck. I am trying to use a potentiometer with a 28byj-48 and a un2003 driver for a sub routine in this invention of mine. What I need this to do seems like it should be simple but I am just not getting it. Here is what I need this stepper to do:

I need the stepper to move to 4 pre determined locations using the potentiometer. So start with pot at full ccw motor stays put, then move pot 60 degrees in the cw direction and motor follows, then to 180 and motor follows, then 240 and motor follows then finally 300 and motor follows, then in reverse. However, I need the motor to follow wherever the pot goes. For an example, if the pot is at 240 and I turn it back to 60, i need it to follow.
Can someone give me a hand? I am really stuck. Here is the code I have:

#include <Stepper.h>  // Include the header file


#define STEPS 150
#define POTENTIOMETER_PIN A0

// create an instance of the stepper class using the steps and pins

Stepper stepper(STEPS, 8, 10, 9, 11);


int Pval = 0;

int potVal = 0;


void setup() {

  Serial.begin(9600);

  stepper.setSpeed(200);
}


void loop() {
  int data = analogRead(POTENTIOMETER_PIN);
  int percentage = map(data, 0, 1023, -90, 90);

  potVal = map(data, 0, 1023, 0, 800);  //(analogRead(A0),0,1024,0,500);

  if (potVal > Pval)

    stepper.step(-1500);

  if (potVal < Pval)

    stepper.step(+1500);

  Serial.print("Potentiometer at ");
  Serial.print(percentage);
  Serial.println("%");
  delay(500);

  Pval = potVal;


  Serial.println(Pval);  //for debugging
}

So the stepper just tracks the pot but only in 60 degree increments?
why not something like

potVal = map(data, 0, 1023, 0, 4);

or if you need the pot percentage, just

Hi,
You will need some sort of intialisation and home switch, this is so you can get the stepper synced with the pot.
At turn ON, how will the stepper know where it is, so it can sync with the pot position?

Tom.. :smiley: :+1: :coffee: :australia:

Sounds like a servo motor should be used, not a stepper motor!

Only 1023 would give 4 with that mapping so you would not get an interval for the max value but just a point and might be jaggy

Probably
potVal = map(data, 0, 1024, 0, 5);
Would do better as you never get 1024 you’ll never get 5 and you get 5 intervals in between 0-1023 leading to 0-4

I really don’t need percentage, just need it to follow the pot. Or, I thought of another idea, I could use 4 push buttons but I’m not sure how to code that either? I know how to wire them up but this unipolar stepper is kicking my but! Any ideas?

Richard Potter

I’ve never used a servo before. I need a decent amount of torque. Do they make one that could deliver? I’m up for a go at it.

Richard Potter

28BYJ-48 motor has 2048 steps per revolution so 300 degrees would be about 1707 steps (1706.667 or 1706 2/3) top speed without missing steps is 12 ~ 13 RPM unless acceleration is used (AccelStepper or MobaTools libraries).

Yep. Understood. Just hopped it up for testing.

Richard Potter

How does your stepper know the number of steps to move to get to those locations?

That seems to be my issue, I cant seem to get the unipolar stepper to work with accelstepper library to use the moveTo and other commands. Any ideas?

Ideas? Yes. ALL stepper uses need to have an initial part of the program to move the stepper to a known position that will become the zero position for counting stepper motor steps. You do not have that initial function. It needs a limit switch of some type to indicate when the stepper has reached the zero position.
Study some of the stepper motor projects on the forum. If they actually work, they will all have that beginning process.

I think getting a pot to a specific repeatable value is going to be a challenge. As others have stated the first step (pun intended :grin:) is to get your position to zero. Then use a small range from your pot to trigger your stepper to go there.

For example, if the pot reading is anywhere between 80 and 100 then you can move your stepper "X" number of steps to get there and just repeat for as many positions as you need.

Understood, however, I have seen projects that are not using limit switches and I really hope I can find a way without them simply because I just don’t have anywhere to put one…………….But thank you for the information I really do appreciate all the help.

Thank you essejcds, I am using a 10k 1 turn pot so the idea was to use the whole range of the one turn pot to get to the positions I need, having said that, they do not have to be exact positions as long as the motor rotates with the pot in the range of full ccw to full cw. I hope I am explaining myself clearly, I know it is hard on the recieving end to picture what is going on. Thank you again.

I must have miss read that. I thought that you wanted the stepper to go to a set of predefined positions based on the pot. If you need full range to mimic the position of the pot, then you'll need to map your pot reading to steps from zero while recording your position in steps.

OK, I just heard the same thing from another so I will give that a try. I will be learning on the fly with this, its my first time using a pot like this.

still not clear what you want to do
do you need just 4 positions ?
How much deviation in degrees is allowed if you drive to any of he 4 positions a second a third a fourth time?

You will have a very hard time to put a potentiometer at the exact same position again and again except for the min-position and the max-position of the potentiometer because you can move any farther than the min/max-position

The stepper-libraris AccelStepper and MobaTools have functions to drive to a certain absolute position.

This means using four buttons boils down to
as pseudo-code

if(button1 is pressed) {
  moveTo(Pos1);
}

if(button2 is pressed) {
  moveTo(Pos2);
}

if(button3 is pressed) {
  moveTo(Pos3);
}

if(button4 is pressed) {
  moveTo(Pos4);
}

job done

Depending on your real project driving a stepper-motor without limit-switch in most cases means to fiddling by hand to get your stepper-motor into a defined position each time you switch power on.

This means you created a heavy design-flaw by not knowing enough about stepper-motors

best regards Stefan

Can you say what the not working 'invention' is ?

Hello everyone, thank you all for your help. I figured out what I needed and it works well.

#include <AccelStepper.h>
#include <MultiStepper.h>



// Define some steppers and the pins the will use
//AccelStepper stepper1; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
AccelStepper stepper1(AccelStepper::FULL4WIRE, 8, 10, 9, 11);
//AccelStepper stepper3(AccelStepper::FULL2WIRE, 10, 11);

int Pval = 0;
int potVal = 0;

void setup() {
  Serial.begin(9600);
  
}

void loop() {
  
  int currentPosition(0);
  potVal = map(analogRead(A0), 0, 1024, 0, 100);

  if (potVal >1 && potVal <12) {
    stepper1.setMaxSpeed(400.0);
    stepper1.setAcceleration(400);
    stepper1.moveTo(1000);
  }
if (potVal >13 && potVal <25) {
    stepper1.setMaxSpeed(400.0);
    stepper1.setAcceleration(400);
    stepper1.moveTo(2000);
  }

if (potVal >26 && potVal <38) {
    stepper1.setMaxSpeed(400.0);
    stepper1.setAcceleration(400);
    stepper1.moveTo(3000);
  }
if (potVal >39 && potVal <50) {
    stepper1.setMaxSpeed(400.0);
    stepper1.setAcceleration(400);
    stepper1.moveTo(4000);
  }
if (potVal >51 && potVal <63) {
    stepper1.setMaxSpeed(400.0);
    stepper1.setAcceleration(400);
    stepper1.moveTo(5000);
  }
if (potVal >64 && potVal <75) {
    stepper1.setMaxSpeed(400.0);
    stepper1.setAcceleration(400);
    stepper1.moveTo(6000);
  }
if (potVal >76 && potVal <88) {
    stepper1.setMaxSpeed(400.0);
    stepper1.setAcceleration(300);
    stepper1.moveTo(7000);
  }
if (potVal >89 && potVal <100) {
    stepper1.setMaxSpeed(400.0);
    stepper1.setAcceleration(400);
    stepper1.moveTo(8000);
  }

  
  Pval = potVal;
  Serial.print(potVal);

  stepper1.run();
}