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
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);
 }
 }
}
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.
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
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
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