Add POT to the below code

Hi All,

I quickly need to add a pot to the code to adjust the speed of motion in the void loop. The initial speed to home can remain constant but not critical if easier to change. I basically need to change the backward and forward by the pot. I placed it 5v and gnd and A03 but lack of coding knowledge. If you could update the code it would be fantastic as got time constraints. Thank you S

#include <AccelStepper.h>

AccelStepper stepper1(1, 9, 8);

const int homeButton = 2;
const int ledPin = 13;
byte hBval;


void setup(){
  stepper1.setMaxSpeed(1000000); //nice and slow for testing
  stepper1.moveTo(-3200);
  stepper1.setAcceleration(400000);
  pinMode(homeButton, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  stepperHome(); //runs routine to home motor
}
void loop(){
  stepper1.moveTo(2000); // random position to end for testing
  stepper1.runToPosition();
  delay(10);
  stepper1.moveTo(0);
  stepper1.runToPosition();
  delay(10);
}
//contributed by Runaway Pancake 9/2/13
void stepperHome(){ //this routine should run the motor
  hBval = digitalRead(homeButton);
  while (hBval == HIGH)
  {
    //backwards slowly till it hits the switch and stops
    stepper1.moveTo(-5000);
    stepper1.run();
    digitalWrite(ledPin, LOW); //indicates it's doing something
    hBval = digitalRead(homeButton);
  }
  digitalWrite(ledPin, HIGH); //indicates it's doing something
  stepper1.setCurrentPosition(-2000); //should set motor position to zero and go back to main routine
}

Read this

http://forum.arduino.cc/index.php?topic=148850.0

start at step 7

For speed control:

#define MY_MAX_SPEED ??? // whatever step rate is your max

int readSpeed ()
{
  int input = analogRead (A3) ;
  return map (input, 0, 1023, 0, MY_MAX_SPEED) ;
}

#define RUN_MS 400   // milliseconds to run each direction, large enough to notice

void loop ()
{
  stepper1.setSpeed (readSpeed ()) ;
  unsigned long now = millis () ;
  while (millis () - now < RUN_MS)
    stepper1.runSpeed();
  stepper1.setSpeed (- readSpeed ()) ;
  now = millis () ;
  while (millis () - now < RUN_MS)
    stepper1.runSpeed();
}

Or position control with max speed:

void loop()
{
  stepper1.setMaxSpeed (readSpeed ()) ;
  stepper1.moveTo(2000); // random position to end for testing
  stepper1.runToPosition();
  delay(RUN_MS);
  stepper1.moveTo(0);
  stepper1.runToPosition();
  delay(RUN_MS);
}

Your 10ms delays in the original code would make any motion difficult to
detect, BTW.

Hi Mark,

Thank you so much for that, just tested and looks good. My only issue now is the line below doesn't work.

stepper1.setCurrentPosition(-2000); //should set motor position to zero and go back to main routine

After the motor reversing and hitting the end switch it then moves forward a distance before starting the forward and backward motion, this no longer happens. It may be the way I have implemented your suggestion or other. I will take a look unless you can see any obvious reason why?

Regards
Simon

You're attempting to home, but never read the current position in order to set your
home point for the rest of the code. You need a variable to store it and then use
code like

  stepper1.moveTo (2000 - home) ;

Tried for a few hours to get what you suggest to work but to no avail, may need to abandon. Just a lack of experience and cannot click on how I store the motors current position to use it as a variable. Thank for your help so far anyway.

Regards
Simon

sgilkes:
Tried for a few hours to get what you suggest to work but to no avail,

Regards
Simon

It's very simple - you go to the post you need to modify, click on "modify", then highlight the code and then click on the # symbol on the editor's toolbar.

That was a response to Marks suggestion not your 'formatting' assistance.

sgilkes:
That was a response to Marks suggestion not your 'formatting' assistance.

I know that, but I thought you might take the hint.
Seems not.
Ho-hum.

That's me officially given up.

Admin, fixed the layout so you will be happy.

Thanks Mark, if you ever get some spare time lets me know how crap I am.

S.

Admin, fixed the layout so you will be happy

You've got the wrong guy - I'm just a moderator.
(Hopefully, not for much longer)

I would like to add a POT to this code so I can vary its resultant back and forth motion. At present the code makes the stepper reverse to an end stop switch, it then move forward to a postion and then starts a forward and back motion. I would like to include the POT to vary the speed of his motion. I'm new to Arduino coding and so far have failed to make this work. If someone could add what is required to this code it would be a great help.

Regards
S

#include <AccelStepper.h>

AccelStepper stepper1(1, 9, 8);

const int homeButton = 2;
const int ledPin = 13;
byte hBval;


void setup(){
  stepper1.setMaxSpeed(1000000); //nice and slow for testing
  stepper1.moveTo(-3200);
  stepper1.setAcceleration(400000);
  pinMode(homeButton, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  stepperHome(); //runs routine to home motor
}
void loop(){
  stepper1.moveTo(2000); // random position to end for testing
  stepper1.runToPosition();
  delay(10);
  stepper1.moveTo(0);
  stepper1.runToPosition();
  delay(10);
}
//contributed by Runaway Pancake 9/2/13
void stepperHome(){ //this routine should run the motor
  hBval = digitalRead(homeButton);
  while (hBval == HIGH)
  {
    //backwards slowly till it hits the switch and stops
    stepper1.moveTo(-5000);
    stepper1.run();
    digitalWrite(ledPin, LOW); //indicates it's doing something
    hBval = digitalRead(homeButton);
  }
  digitalWrite(ledPin, HIGH); //indicates it's doing something
  stepper1.setCurrentPosition(-2000); //should set motor position to zero and go back to main routine
}

and so far have failed to make this work.

What have you tried? What problems are you having?

Reading from the pin that a properly wired potentiometer is connected to is trivial.

Using that reading to control the speed is trivial.

  stepper1.setMaxSpeed(1000000); //nice and slow for testing

Yeah, right.