Switchcase trouble

Hello
Im having some trouble with a simple stepper motor code.
Im trying to control a single linear axle.
The following code WORKS with Delay, but NOT with DelayMicroseconds in the step pulses.

Can anyone point me in the right direction please :confused:

Regards
Mathias

// Speed, Direction,limits on home and end, delay microseconds


#define StepPin 9 
 
#define DirPin 8 
  
#define SpeedSwitch 3 
 
#define ForwardButton 4 

#define ReverseButton 5 

#define HomeLimit 2 
 
#define EndLimit 1 

 



int StepSpeed = 1500; 

 

void setup() {

   pinMode(DirPin, OUTPUT);

   pinMode(StepPin, OUTPUT);

   pinMode(HomeLimit, INPUT);

   pinMode(EndLimit, INPUT);

   pinMode(SpeedSwitch, INPUT_PULLUP);

   pinMode(ForwardButton, INPUT_PULLUP);

   pinMode(ReverseButton, INPUT_PULLUP);

}



void loop() {

  if (!digitalRead(SpeedSwitch)) { 
 
    
switch (StepSpeed) { 
 
      
      
case 0:
        StepSpeed=1500;  // slow speed

break;

      
case 1:
        StepSpeed=1000;  // medium slow

        break;

      
case 2:
        StepSpeed=800;  // medium fast

        break;


        case 3:
        StepSpeed=500;  // high speed

        break;

 }

  } 

   
    
  if (!digitalRead(ForwardButton) ) { 

 if (!digitalRead(HomeLimit)) {} 

  else { 
digitalWrite(DirPin, LOW);  
digitalWrite(StepPin, HIGH);

        delayMicroseconds(StepSpeed);

        digitalWrite(StepPin, LOW);

        delayMicroseconds(StepSpeed);


 } 
     
  }

  
    if (!digitalRead(ReverseButton) ) { 
  
 if (!digitalRead(EndLimit)) {} 

  else { 
 
      
        digitalWrite(DirPin, HIGH); 
 
        digitalWrite(StepPin, HIGH);

        delayMicroseconds(StepSpeed);

        digitalWrite(StepPin, LOW);

        delayMicroseconds(StepSpeed);

  } 
 }

}

What does it do? What do you want it to do?

StepSpeed is 1500. That means that your switch will never do anything.

Please be specific about what doesn't work.

I note that you initialize StepSpeed to 1500 and never set it to anything else again. None of the cases in your switch statement will ever execute so StepSpeed will always be 1500.

Hi Lillmatte,

karma for posting code as a codesection in your first post.

you use variable StepSpeed in the switch-statement

switch (StepSpeed)

and immediately assign values that are far away of all case statements to the same variable
that makes no sense.
at the definition of variable StepSpeed you initialise with value

int StepSpeed = 1500;

This means none of the case-statements becomes true
case uses values 0,1,2,3 but never 1500 or any other of this high values

    switch (StepSpeed) {
      case 0:
        StepSpeed = 1500; // slow speed
        break;

the variable used in in the switch-statement must be different from StepSpeed

How about using a library like te library MobaTools that offer functions that to do all the details of the bitbanging
for step / dir?
best regards Stefan

thx for the answers
Im kind of new to coding so I want to try and get the hang of it without using libraries at first

I want the code to switch between different speeds when I press the button.

If I set "int StepSpeed" to 10 and write the switchcase as below it does what I want

switch (step_speed) {
case 1:
step_speed=10;
break;

case 2:
step_speed=1;
break;
case 3:
step_speed=3;
break;

Im a real newbie so go slow :o

/Mathias

This is still not good. You should use two different variables.
On for the switch-case

and a second and different variable for the speed

In programming you can have (almost) as much variables as you like.
Of course you don't add variables just to have more of them. But in this case it is very very very clear to use two different variables

here is a codeversion that show how to use it with a second variable named SpeedSelector
which I find a self-explaining name. by pressing a button the speeds slow, medium-slow,, medium-fast, fast can be selected.
Though this codeversion only shows the use of a second variable
it does not (yet) include the functionality of change speed through pressing a button

This needs additional lines of code with state-change-detection.

// Speed, Direction,limits on home and end, delay microseconds

#define StepPin 9
#define DirPin 8
#define SpeedSwitch 3
#define ForwardButton 4
#define ReverseButton 5
#define HomeLimit 2
#define EndLimit 1

int StepSpeed     = 1500;
int SpeedSelector = 0;     // added a new variable

void setup() {
  pinMode(DirPin, OUTPUT);
  pinMode(StepPin, OUTPUT);
  pinMode(HomeLimit, INPUT);
  pinMode(EndLimit, INPUT);
  pinMode(SpeedSwitch, INPUT_PULLUP);
  pinMode(ForwardButton, INPUT_PULLUP);
  pinMode(ReverseButton, INPUT_PULLUP);
}


void loop() {

  if (!digitalRead(SpeedSwitch)) {
    
    switch (SpeedSelector) {
      case 0:
        StepSpeed = 1500; // slow speed
        break;

      case 1:
        StepSpeed = 1000; // medium slow
        break;

      case 2:
        StepSpeed = 800; // medium fast
        break;

      case 3:
        StepSpeed = 500; // high speed
        break;
    }
  }

  if (!digitalRead(ForwardButton) ) {
    if (!digitalRead(HomeLimit)) {}

    else {
      digitalWrite(DirPin, LOW);
      digitalWrite(StepPin, HIGH);
      delayMicroseconds(StepSpeed);
      digitalWrite(StepPin, LOW);
      delayMicroseconds(StepSpeed);
    }
  }

  if (!digitalRead(ReverseButton) ) {
    if (!digitalRead(EndLimit)) {}
    else {
      digitalWrite(DirPin, HIGH);
      digitalWrite(StepPin, HIGH);
      delayMicroseconds(StepSpeed);
      digitalWrite(StepPin, LOW);
      delayMicroseconds(StepSpeed);
    }
  }
}

As additional hints: you shouldn't use sooo many empty lines. This makes it hard to keep an overview

best regards Stefan

@OP,
Which input(s) number(s) are you using to select the speed?
Or is it a three positions selector?

Thank you Stefan
I suspected that i needed some more variables.

But how come it does work, when I use delay?

Sorry for the extra lines, I was trying to clean up the code when I pasted it, turned out like that when I hit post

/Mathias

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