Stepper motor, tmc 2209, 6 switches (speed and full rotations)

ok so im just getting into using an arduino to control a stepper motor and im flustered.

i have a nema 17 stepper motor and a TMC2209 along with 6 switches. (4 pole, 3 way rotary selectors technically) and a "trigger" button i'd like to get control over the stepper via the switches in this sort of setup

speed -
switch 1 high = slow
switch 2 high = medium
switch 3 high = fast

then the same but for rotations (so i know this is done via step count)
switch 1 high = 2 rotations
switch 2 high = 3 rotations
switch 3 high = 4 rotations

i will be using 4 pole 3 way rotary selectors for this, but honestly, other than the basic examples i found online for setting up the pinouts of the 2209 im entirely lost where to begin.

can someone direct me in the starting direction using lamens terms?

this is what ive been able to get as far as setup and definitions go but

#define EN_PIN 9    
#define STEP_PIN 10
#define DIR_PIN 11
const int lowspeed = 3;
const int mediumspeed = 4;
const int highspeed = 5;
const int twoturns = 6;
const int threeturns =7;
const int fourturns = 8;
const int trigger = 12;

void setup(){
 pinMode(EN_PIN, OUTPUT);
 pinMode(STEP_PIN, OUTPUT);
 pinMode(DIR_PIN, OUTPUT);
 digitalWrite(EN_PIN, LOW);
 digitalWrite(DIR_PIN, LOW);
 pinMode (lowspeed, INPUT);
 pinMode (mediumspeed, INPUT);
 pinMode (highspeed, INPUT);
 pinMode (twoturns, INPUT);
 pinMode (threeturns, INPUT);
 pinMode (fourturns, INPUT);
 pinMode (trigger, INPUT);
}

void loop(){
digitalWrite(DIR_PIN, LOW); //only need it to turn in one direction

if (trigger = HIGH){ //insert switch variables and commands here?
   if (lowspeed = HIGH){ //set speed to X steps per second
   }
   else();
  }
   if (mediumspeed = HIGH){
 }
  else();
  if(highspeed = HIGH){
}
  else();
}

 if (twoturns=HIGH){ //set steps to turn here?
}
  else(); //do nothing
}

its not much, but its the start i guess, i just dont know how to construct the remaining if/else arguements to check the switch possitions and make those take the correct steps, nor am i sure how to setup the stepper motor in code so 2 turns = # steps and low speed = #steps per second.

hope that all made sense and you understand where i need the help.

considering this is my very first venture im rather proud i made it this far as i THINK the code so far is good lol

  • Here is one problem :thinking:

please explain

  • Goggle
    Arduino = versus ==

please explain

thats a different arguement to the if statement, are you saying i need to set the pin to low in setup first?

  • Operator: Assignment uses = (e.g., x = 5). Equality uses == (e.g., x == 5).
    • x = 5 assigns x the value of 5.
    • x == 5 tests to see if x is equal to 5.

please, i asked for things in lamens terms, using the functions your suggesting is going outside of what i understand which isnt much as it is, i'd rather stick with the if/else statements as i understand that it is literally "if condition met, do this, else, do this/nothing)

hope that didnt come off rude, i just struggle with new concepts.

  • if(x = 5) this makes x equal to 5.

  • if(x == 5) this checks to see it x is 5.

ok but why can i not stick with the if / else instead?

  • Whenever we use if( . . .) we use ==.
  • Example
if(digitalRead(mySwitch) == HIGH) 
{
  Serial.println(“My switch is pressed”);
}

else
{
  Serial.println(“My switch is not pressed”);
}

oh.

so i would need

if (digitalRead(trigger) == HIGH){ //insert switch variables and commands here?
   if (lowspeed = HIGH){ //set speed to X steps per second
   }
   else();
   }

?

i appreciate that explanation, it made sense with the example

  • You are getting there but you still have:
    if (lowspeed = HIGH){

yeah i just edited the first bit to make sure i was understanding you correctly, i will go through and modify the remaining conditions to match then hopefully move along to the next stage. this is a huge learning curve for me.

thank you for your patients with me.

i will edit the main post and reply with the new code as well

:+1:

#define EN_PIN 9    
#define STEP_PIN 10
#define DIR_PIN 11
const int lowspeed = 3;
const int mediumspeed = 4;
const int highspeed = 5;
const int twoturns = 6;
const int threeturns =7;
const int fourturns = 8;
const int trigger = 12;

void setup(){
 pinMode(EN_PIN, OUTPUT);
 pinMode(STEP_PIN, OUTPUT);
 pinMode(DIR_PIN, OUTPUT);
 digitalWrite(EN_PIN, LOW);
 digitalWrite(DIR_PIN, LOW);
 pinMode (lowspeed, INPUT);
 pinMode (mediumspeed, INPUT);
 pinMode (highspeed, INPUT);
 pinMode (twoturns, INPUT);
 pinMode (threeturns, INPUT);
 pinMode (fourturns, INPUT);
 pinMode (trigger, INPUT);
}

void loop(){
digitalWrite(DIR_PIN, LOW); //only need it to turn in one direction

if (digitalRead(trigger) == HIGH){ //insert switch variables and commands here?
   if (digitalRead(lowspeed) == HIGH){ //set speed to X steps per second
   }
   else();
   
   
   if (digitalRead(mediumspeed) == HIGH){
  }
  else();
  
  if (digitalRead(highspeed) == HIGH){
  }
  else();
  
 if (digitalRead(twoturns) == HIGH){ //set steps to turn here?
  }
  else();

  if (digitalRead(threeturns) == HIGH){
  }
  else(); //do nothing

    if (digitalRead(fourturns) == HIGH){
  }
  else(); //do nothing
 }
}

so thats the modified code

im suddenly realising i dont think i need the "else" in there because there is no alternative other than "do nothing" am i right on that note?

  • You are starting to understand.

  • Depending how your switches are wired, we might need a pull-up resistor in the circuit.

  • If your switches are wired like this:
    [PIN]——[switch]——GND
    We need this:
    pinMode(mySwitch, INPUT_PULLUP):

  • If your switches are wired like this:
    5V——[switch]——[PIN]——[10k]——GND
    We need this:
    pinmode(mySwitch, INPUT):


  • If else() isn’t needed, we don’t need to include it.

i'll be using a nano for this project. if memory serves me right it has an internal pullup resistor so i will modify and use "pullup" instead of going through the hastle of ordering in 10k resistors.

i can loose the else statements cant i? they're not actually needed because there is no alternative other than "check the next switch"

Which of the two dozen (or more) different Arduinos are you using?

mentioned that as you was asking, nano.