Using a POT to manage speed

Hello, i have a rotary POT that is currently writing the analog input to a LED to control the voltage however instead of controlling the voltage from the pot, I want to rather control a cycle time (between 8 < 30 movements a minute) and then add another pot to control the length of the stoke for the same cycle time (so arms would move faster or slower to match the cycle time). I dont see much time function on google. What is the best function to do this?

Thanks, I have read the link but dont see how to apply it. Please bear with me as i am very new and learning all i can. i have attached a basic sketch for concept. i am able to control teh speed without issues using the pot, its just the time function im baffled. At the moment, im just using physical values.

<// Define the Pins used

#define step_pin 2    // Pin 2 connected to Steps pin on Ardunio
#define dir_pin 3     // Pin 3 connected to Direction pin Ardunio
#define home_switch 9 // Pin 9 connected to Ardunio (MicroSwitch)


#include <Stepper.h>
int direction;    // Variable to set Rotation (CW-CCW) of the motor
int steps;        // Used to set HOME position after Homing is completed
const int stepsPerRevolution = 3200; 
int speed = 100; // rpm of motor 
int strokeLength = 2800; // length of stroke movement on paddles
Stepper myStepper(stepsPerRevolution, 2,3);

void setup() {
   pinMode(dir_pin, OUTPUT);
   pinMode(step_pin, OUTPUT);
   pinMode(home_switch, INPUT_PULLUP);
// Start Homing procedure of Stepper Motor at startup

  while (digitalRead(home_switch)) {  // Do this until the switch is activated   
    digitalWrite(dir_pin, HIGH);      // (HIGH = anti-clockwise / LOW = clockwise)
    digitalWrite(step_pin, HIGH);
    delay(10);                       // Delay to slow down speed of Stepper
    digitalWrite(step_pin, LOW);

  while (!digitalRead(home_switch)) { // Do this until the switch is not activated
    digitalWrite(dir_pin, LOW); 
    digitalWrite(step_pin, HIGH);
    delay(10);                       // More delay to slow even more while moving away from switch
    digitalWrite(step_pin, LOW);

  steps=0;  // Reset position variable to zero

void loop() {

  //Serial.begin(9600);               //remove these to start serial writing 
  myStepper.setSpeed(speed); // RPM
  myStepper.step(strokeLength);       // SETS THE NUMBER OF STEPS
  delay(500);                         // Plat time 
  myStepper.setSpeed(speed); // RPM
  myStepper.step(-strokeLength);      // SETS THE NUMBER OF STEPS

OPs diagram.

Put the Serial.begin(9600);
in the setup() part of your code.
Tom... :slight_smile:

I am confused. In your previous post you were using a stepper driver MKS TB67S109 with step/dir control. Now you are using Stepper.h along with step/dir control. Stepper.h should not be used with that driver. You will directly manage the steps like you do in the homing routine.

See Robin2's stepper motor basics

To use the pots to control the motor you are going to have to do some math to determine the step delay.

You want to use one one pot to set the stroke length. You will need to map function to convert the pot readings 0-1023 to some stroke length.

Then you use the other pot to set the cycle time. Again, you will map the pot reading to a cycles/minute value.

Say you want to move 2000 steps out and back 10 times per minute. Thats 40K steps over 60 seconds or 667 steps/second. That works out to 1500 microseconds between steps.

You will need to manage the stepper using the timing you calculate.

Thanks, I managed to work out how to change the analog reading and map it to give something i could use.

The problem is now that the POT I am using is not very accurate so it changes values by 1 or 2 readings which then changes the position on the actual arms. Over a period, the run out is variable.

I think I need to use a type of clicker (or fixed point position) type pot that will give accurate readings to the ensure the arm positions stay in their desired stroke.

Any views on that?

Sounds like you want a rotary encoder then instead of a pot. Or even just 2 push buttons, one up & one down. Use those to count up or down as low or as high as needed, or as your program limits the count.

You could use a control band on the pot changes so that the value needs to be larger or smaller by some amount(say 5-10 counts)than the last value. That will eliminate the 1-2-3 count variation. The downside is that you may loose some precision on the available stroke lengths.

You could also only read the pot values after a run is complete to set new values. Do you need to change things during a cycle?

As @ Crossroads suggests, a digital solution might be best.

If you can be hooked to a computer or laptop you could enter the values by Serial. You could possible use a membrane keypad.

User interface is often the most complex part of an Arduino program.

The more pot you use, everything seems to go slower.

@CrossRoads, I thought of a button for up and down, but then i would need to have 6 buttons for this as i have 3 x pots on the control interface and possibly a forth.

@Cattledog, what code would you suggest i use? Sorry im very new to Arduino and finding may way around. It would have to only change once the forward and back stroke has completed.

Can you add one button? It could be used to trigger a run routine after parameter setup is complete. When the run is over the program can return to the setup block. You could either make changes, or just push the button to run the same again.

Can you please post the code as you now have it.

POT is not an acronym like LED (light emitting diode). It is an abbreviation for "potentiometer". It should not be capitalized.