BASIC QUESTION change stepper speed with button

I am trying to make a really simple hand controller for my telescope. I have the stepper motor running the way I want with the TMC2100 drivers. I want to press a button to slow down the motor by 2x (I’ll add more buttons later for different speeds).

I press the button and no changes happen. I am super new to this and am pretty happy that I have got this far.

Note: I am only asking to change the speed of one motor in this code for now
I am using a resistor between ground and the button per the many tutorials which I understand to be a pullup resistor

#include <AccelStepper.h>

int motorSpeed = 2000; // steps per second
int DecMotorSpeed = 500;
int half_RAspeed = 1000;

int motorDirPin = 12; //digital pin 12
int motorStepPin = 11; //digital pin 11
int EN_PIN = 10; //enable (CFG6)

int EN_PIN_dec = 7; //enable dec cfg6
int DecDir = 9; //dec direction pin
int DecStep = 8; //dec step pin

const int RA_buttonSlow = 14; // the number of the pushbutton pin
int RA_buttonState = 0; // variable for reading the pushbutton status

//set up the accelStepper intance
//the “1” tells it we are using a driver

AccelStepper stepper(1, motorStepPin, motorDirPin);
AccelStepper stepperDec (1, DecStep, DecDir);

void setup()
{
stepper.setMaxSpeed(motorSpeed);
stepper.setSpeed(motorSpeed);
stepperDec.setMaxSpeed(DecMotorSpeed);
stepperDec.setSpeed(DecMotorSpeed);
pinMode(RA_buttonSlow, INPUT);

}

void loop()
{
stepper.runSpeed();
stepperDec.runSpeed();

// read the state of the pushbutton value:
RA_buttonState = digitalRead(RA_buttonSlow);

// check if the pushbutton is pressed. If it is, the buttonState is HIGH

if (RA_buttonState == HIGH) { // make motor turn half as fast

stepper.setSpeed(half_RAspeed);

}

else if (RA_buttonState == LOW)

{
stepper.setSpeed(motorSpeed); // return to normal speed

}

}

My first guess would be the button being wired wrong. Try using Serial.print() to figure out if the button is switching as expected.

johnwasser:
My first guess would be the button being wired wrong. Try using Serial.print() to figure out if the button is switching as expected.

I am running the whole setup from a 12 volt ac adapter with a 5v regulator going to the teensy. My understanding is that the power up sequence is very sensitive for the TMC 2100 drivers. So I don't want to use the 5v power from the USB port.

Does the code look right? If so I'll revisit the wiring. Thanks for the response

jnanof:
I am running the whole setup from a 12 volt ac adapter with a 5v regulator going to the teensy. My understanding is that the power up sequence is very sensitive for the TMC 2100 drivers. So I don't want to use the 5v power from the USB port.

Does the code look right? If so I'll revisit the wiring. Thanks for the response

I just took the resistor off and went straight to ground, now the button works but when button is pressed the speed is double. Have I got something backwards?

jnanof:
I just took the resistor off and went straight to ground, now the button works but when button is pressed the speed is double. Have I got something backwards?

had the HIGH/LOW backwards. All is good now :slight_smile: