Stepper code mod

I need to change this code so that I can use a toggle switch instead of a potentiometer.
The code is for controlling a stepper motor with a single axis joystick, which would be off when in center position, forward or reverse with variable speed. I need to replace the joystick with a momentary 3 way toggle switch that work the same only without variable speed. Just either off, forward or reverse. If you can help I'd appreciate it.


#include <AccelStepper.h> // accelstepper library
AccelStepper stepper(1, 8, 9); // direction Digital 9 (CCW), pulses Digital 8 (CLK)

//Pins
const byte Analog_X_pin = A1; // x-axis readings
const byte enablePin = 7;

//Variables
int Analog_X = 0;             //x-axis value
int Analog_X_AVG = 0;         //x-axis value average
  //---------------------------------------------------------------------------- 
void setup() {
  Serial.begin(9600);
  //----------------------------------------------------------------------------    
  //PINS
  pinMode(Analog_X_pin, INPUT);
  pinMode(enablePin, OUTPUT);
  pinMode(2, INPUT_PULLUP); 
  pinMode(3, INPUT_PULLUP);
  //----------------------------------------------------------------------------  
  InitialValues(); // averaging the values of analog pin 0 (value from potmeter)
  //----------------------------------------------------------------------------  
  // Stepper parameters
  // setting up some default values for maximum speed and maximum acceleration
  stepper.setMaxSpeed(2000);     //SPEED = Steps / second  
  stepper.setAcceleration(1000); //ACCELERATION = Steps /(second)^2    
  stepper.setSpeed(1000);
  delay(1000);
}

void loop()
{
  ReadAnalog();
  ReadLimits();  
  stepper.runSpeed(); //step the motor (this will step the motor by 1 step at each loop indefinitely)
}

void ReadLimits()
{
  if( digitalRead(2)==LOW && stepper.speed()>0 )//read input, assuming they have pull-up resistors.
  {
    stepper.setSpeed(0);
  }

  if( digitalRead(3)==LOW && stepper.speed()<0 )//read input, assuming they have pull-up resistors.
  {
    stepper.setSpeed(0);
  }
}

void ReadAnalog() {
  if (abs(Analog_X-Analog_X_AVG) > 50) {
    digitalWrite(enablePin, HIGH);  // enable the driver
    stepper.setSpeed(5*(Analog_X-Analog_X_AVG));   
  } else {
    digitalWrite(enablePin, LOW);   // disable the driver
    stepper.setSpeed(0);
  }
  // Reading the potentiometer in the joystick: 
  Analog_X = analogRead(Analog_X_pin);  
  // if the value is 25 "value away" from the average (midpoint), we allow the update of the speed
  // This is a sort of a filter for the inaccuracy of the reading
  if (abs(Analog_X-Analog_X_AVG) > 50) {
    stepper.setSpeed(5 * (Analog_X-Analog_X_AVG));    
  } else {
    stepper.setSpeed(0);
  }
}

void InitialValues() {
  //Set the values to zero before averaging
  float tempX = 0;
  //----------------------------------------------------------------------------  
  // read the analog 50x, then calculate an average. 
  // they will be the reference values
  for (int i = 0; i<50; i++) {
    tempX += analogRead(Analog_X_pin);  
    delay(10); //allowing a little time between two readings
  }
  //----------------------------------------------------------------------------  
  Analog_X_AVG = tempX/50; 
  //----------------------------------------------------------------------------  
  Serial.print("AVG_X: ");
  Serial.println(Analog_X_AVG);
  Serial.println("Calibration finished");  
}

something like this maybe?
(Compiles, NOT tested!)

#include <AccelStepper.h> // accelstepper library
AccelStepper stepper(1, 8, 9); // direction Digital 9 (CCW), pulses Digital 8 (CLK)

//Pins
const byte forwardPin = 5;
const byte reversePin = 6;
const byte enablePin = 7;

//----------------------------------------------------------------------------
void setup() {
  Serial.begin(9600);
  //----------------------------------------------------------------------------
  //PINS
  pinMode(enablePin, OUTPUT);
  pinMode(forwardPin, INPUT_PULLUP);
  pinMode(reversePin, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);

  // Stepper parameters
  // setting up some default values for maximum speed and maximum acceleration
  stepper.setMaxSpeed(2000);     //SPEED = Steps / second
  stepper.setAcceleration(1000); //ACCELERATION = Steps /(second)^2
  stepper.setSpeed(1000);
  delay(1000);
}

void loop()
{
  ReadToggleSwitch();
  ReadLimits();
  stepper.runSpeed(); //step the motor (this will step the motor by 1 step at each loop indefinitely)
}

void ReadLimits()
{
  if ( digitalRead(2) == LOW && stepper.speed() > 0 ) //read input, assuming they have pull-up resistors.
  {
    stepper.setSpeed(0);
  }

  if ( digitalRead(3) == LOW && stepper.speed() < 0 ) //read input, assuming they have pull-up resistors.
  {
    stepper.setSpeed(0);
  }
}

void ReadToggleSwitch() {
  if (digitalRead(forwardPin) == LOW) {
    digitalWrite(enablePin, HIGH);  // enable the driver
    stepper.setSpeed(250);   //<-------------set desired speed here!
  } else {
    digitalWrite(enablePin, LOW);   // disable the driver
    stepper.setSpeed(0);
  }

  if (digitalRead(reversePin) == LOW) {
    digitalWrite(enablePin, HIGH);  // enable the driver
    stepper.setSpeed(-250);   //<-------------set desired speed here!
  } else {
    digitalWrite(enablePin, LOW);   // disable the driver
    stepper.setSpeed(0);
  }
}

how do you plan to wire the toggle switch? to separate digital pins are with resistors to the same analog pin?

Thanks, this is how it's wired now. it works but the joystick keeps going bad so and I don't need variable speed on this motor so I'd like to use the same wireing and replace the pot with a 3 way toggle switch.

i think you could replace the pot with a pair of 10k resistors in series between 5V and ground with the center going to A1. the center of the toggle also gets wired to A1 with the other terminals wired to 5V and ground.

in one position the toggle switch connects A1 to 5V, the other to ground. no code changes

I've been trying to find an example of a stepper motor with just forward and reverse buttons using the tb6600. If anyone knows of one.

I don't know how to wire this code. I am using the TB6600 can you show me what you have in mind?

it would probably be better it for showed YOUR wiring first. We can then start modifying it to fit in the toggle switch...

also looking online there are examples for the TB6600 that do not use any libraries, but that your choice! :wink:

Yes, I should have included the wiring I'm sorry. I do appreciate your help. If needed I can rewire it, I just need to keep the end switches and the part that turns the motor off when idol because it is battery powered and I don't need holding torque.

sorry donno why the picture in reply #4 was not showing before.

anyway, based on the code proposed in reply #2, this is how I would wire in the toggle switch.

Please take care when testing.

hope that helps...

Doesn't work, motor is on slow when switch is neutral, faster but same direction when switch connects to d5. then off when connected to d6. fried my TB600.

How, what is your stepper power supply?
What is the current limit control set at?

Can you please post some images of your project so we can see your component layout?

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

I took it down so no photos but there was no crossing of wires or power supply issues, I had my other setup working just before I changed to this on which was only a matter of changing a few wires and re programming the nano. When I first turned the power on with the switch in the off position the motor ran slowly, when I when I switched it to one side it ran faster in the same direction. When I switched it the other side it turned off. I suppect because it was shorting I don't know. I should hav quit then but I tried it several more times and then the TB600 would no longer light up. Unless I can find an example of a stepper with just a switch on/off forward, on/off reverse, that works and I could modify I'll just keep it the way it is. The joystick Is problematic but I can make it work. I can't afford to ruin any thing else.

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