Potentiometer with reverse button for stepper

Hi Im am looking for a code and trying to put one together from others I have found (badly) as I am a total beginner in Arduino code.

I have made a camera slider and have a Nema 17 with both a4899 and tmc2208 drivers at my disposal with a nano which I have successfully got working with 2 different codes, 1 that controls the speed (allowing me to film slow and fast shots etc) and the second code to reverse the direction (so when my camera dolly gets to the end I can reverse it with a microswitch)

I would like to do both things at once but cant find a way to join the 2 code together (as I have no real knowledge on coding. I have searched the net and yet cant find code that will do it. I would be very grateful if anyone could help me with this.

Thanks in advance

Here are the 2 codes I have:-

Button code

// Stepper motor run code with a4988 driver
// by Superb

const int stepPin = 3;
int dirPin = 4;
int dirButton = 2;
int state = HIGH;
int reading;
int previous = LOW;
int stepDelay=1000;
int customDelay,customDelayMapped; // Defines variables

long time = 0;
long debounce = 200;

void setup() {
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(dirButton, INPUT_PULLUP);
customDelayMapped = speedUp(); // Gets custom delay values from the custom speedUp function
// Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
digitalWrite(stepPin, HIGH);
delayMicroseconds(customDelayMapped);
digitalWrite(stepPin, LOW);
delayMicroseconds(customDelayMapped);
}
// Function for reading the Potentiometer
int speedUp() {
int customDelay = analogRead(A0); // Reads the potentiometer
int newCustom = map(customDelay, 0, 1023, 300,8000); // Convrests the read values of the potentiometer from 0 to 1023 into desireded delay values (300 to 4000)
return newCustom;
}

void loop() {
reading = digitalRead(dirButton);

// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;

time = millis();    

}

digitalWrite(dirPin, state);

previous = reading;

digitalWrite(stepPin, HIGH);
delayMicroseconds(stepDelay);
digitalWrite(stepPin, LOW);
delayMicroseconds(stepDelay);
}

  2 nd code 

Pot code

// Stepper motor run code with a4988 driver
// by Superb

const int stepPin = 3;
const int dirPin = 4;
int customDelay,customDelayMapped; // Defines variables

void setup() {
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);

digitalWrite(dirPin,HIGH); //change the rotation direction HIGH for clockwise and LOW for anticlockwise
}
void loop() {

customDelayMapped = speedUp(); // Gets custom delay values from the custom speedUp function
// Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
digitalWrite(stepPin, HIGH);
delayMicroseconds(customDelayMapped);
digitalWrite(stepPin, LOW);
delayMicroseconds(customDelayMapped);
}
// Function for reading the Potentiometer
int speedUp() {
int customDelay = analogRead(A0); // Reads the potentiometer
int newCustom = map(customDelay, 0, 1023, 300,4000); // Convrests the read values of the potentiometer from 0 to 1023 into desireded delay values (300 to 4000)
return newCustom;
}

That's the beauty of code. The variations are enormous and the code You want has to be made by You.
There are topics showing "how to merge codes".

No. That's not code. It's text. Use the advice given here: How to get the best out of this forum - Using Arduino / IDE 1.x - Arduino Forum

It tells how to post code. Look for "autoformat", and for "code tags".

2 Likes

Would you draw a picture of these, along with the potentiometer and buttons, in what you imagine to be a complete (combined) system?

Write functions for the components you described:

BUTTONS:

  • read the buttons >> digitalRead(buttonPin1) and digitalRead(buttonPin2)

POTENTIOMETER:

  • read the potentiometer >> analogRead(potPin)
  • map the potentiometer to valid stepper values >> map(analogRead(potPin, 0, 1023, ?, ??)

MOTOR:

  • set the motor direction according to the button >> digitalWrite(dirPin, button)
  • start stepping motor with high pulse >> digitalWrite(stepPin, HIGH)
  • control motor speed with a delay >> delayMicroseconds(analogRead(potPin))
  • finish stepping the motor with a low pulse >> digitalWrite(stepPin, LOW)
    *** make the motor STOP

Write each of these on its own. When each function works, move it (and the parameters) to individual functions, and call the function from void loop()

Good luck.

Haha. Same project here >>

Just for that, here's my code dump.

#define potPin A0
#define dirPin 3
#define stepPin 4

int dir, step, deadband = 100;

void setup() {
  Serial.begin(115200);
  pinMode(dirPin, OUTPUT);
  pinMode(stepPin, OUTPUT);

  welcome();
  while (analogRead(potPin) < 490 || analogRead(potPin) > 534) {} // wait for potentiometer near zero
  instructions();
}

void loop() {
  int speed = map(analogRead(A0), 0, 1024, -2000, 2000);
  if (speed <= -deadband) {
    dir = 0; // counterclockwise
    motorStep(dir, speed);
  }
  if (speed >= deadband) {
    dir = 1; // clockwise
    motorStep(dir, speed);
  }
  if (abs(speed) < deadband) // deadband - stop motor
    motorStop(speed);
}

void motorStep(int dir, int speed) {
  digitalWrite(dirPin, dir);
  digitalWrite(stepPin, HIGH);
  delayMicroseconds(2000 - abs(speed));
  digitalWrite(stepPin, LOW);
  // delayMicroseconds(2000 - abs(speed)); // only the HIGH pulse duration is needed
}

void motorStop(int speed) {
  digitalWrite(stepPin, LOW);
  delayMicroseconds(2000);
  digitalWrite(stepPin, LOW);
  delayMicroseconds(2000);
}

void motorDisplay(int dir, int speed) {
  Serial.print("DIRECTION: ");
  Serial.print((dir == 1) ? " CW" : "CCW");
  Serial.print(" SPEED: ");
  Serial.println(abs(speed));
}

void welcome() {
  Serial.print("Motor will start when potentiometer is at ZERO.  ");
}

void instructions() {
  Serial.println("Motor is started.");
  Serial.println("<< LEFT to increase CCW  | CENTER to STOP | RIGHT to increase CW >>");
}
1 Like

I totally get that but it is incredibly hard to learn and find out the smallest details towards the tasks have literally taken days of searching! I thought this forum here to give advice and encouragement.

I have managed to find a way to merge both codes the only thing missing from my code now is the ability to stop/pause the motor! ie stop it dead and if poss hold the motor as well.searching

Maybe set the speed to zero if the potentiometer is below some threshold?

What code do you have?

The code i dumped has a stop/hold/speed. Try it in the simulation.

In post #3 I listed a way for you to write small parts of your code and then make it into one sketch.

And you have others doing the same thing on a separate thread.

Pick one and flag to merge the two.

Here is the code I have that works for reversing the motor and potentiometer speed control

// Stepper motor run code with a4988 driver
// by Superb

const int stepPin = 3;
 int dirPin = 4; 
 int dirButton = 2;
int state = HIGH;
int reading;
int previous = LOW;
int stepDelay=1000;

long time = 0;
long debounce = 200;

int customDelay,customDelayMapped; // Defines variables
 
void setup() {
  // Sets the two pins as Outputs
  pinMode(stepPin,OUTPUT);
  pinMode(dirPin,OUTPUT);
 pinMode(dirButton, INPUT_PULLUP);

  digitalWrite(dirPin,HIGH); //change the rotation direction HIGH for clockwise and LOW for anticlockwise
}
void loop() {
   reading = digitalRead(dirButton);

  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;

    time = millis();    
  }

  digitalWrite(dirPin, state);

  previous = reading;

digitalWrite(stepPin, HIGH);
delayMicroseconds(stepDelay);
digitalWrite(stepPin, LOW);
delayMicroseconds(stepDelay);
  
  customDelayMapped = speedUp(); // Gets custom delay values from the custom speedUp function
  // Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
  digitalWrite(stepPin, HIGH);
  delayMicroseconds(customDelayMapped);
  digitalWrite(stepPin, LOW);
  delayMicroseconds(customDelayMapped);
}
// Function for reading the Potentiometer
int speedUp() {
  int customDelay = analogRead(A0); // Reads the potentiometer
  int newCustom = map(customDelay, 0, 1023, 400,15000); // Convrests the read values of the potentiometer from 0 to 1023 into desireded delay values (300 to 4000)
  return newCustom;  
}


That's what we do but not the way You might expect it. Helping members learning and growing is the aim, not fix code or circuitry for You.

As above, forum is not a repair shop but posting code mistakes are pointed out.

Learn what work is! Difficult things can take weeks, a year to solve!

1 Like

Agree. A great many posts sound like: "I want an essay about a cat climbing a tree, and I found lots of essays on cats and essays on trees, but none about what I want. Can you combine this cat essay with this tree essay to help me with my kitten climbing a holly bush essay?" Lots of programming is like that, you can't quite cut and paste the two different essays together and get a good combination. Re-writing the essay or program from scratch might be much easier than attempting a cut-and-paste merge.

1 Like

You might get the stop functionality by using the potentiometer's larger delays to skip moving the motor:

  ...
  customDelayMapped = speedUp(); // Gets custom delay values from the custom speedUp function
  // Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
  if  (customDelayMapped < 14000) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(customDelayMapped);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(customDelayMapped);
   }
}

If you just don't write a HIGH to stepPin, the motor will stop... as demonstrated in the simulation.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.