Stepper & serial port

First of all - sorry for my english (I assume nobody in poland could help as you could :D). I’ve got a little problem with my stepper (driver TB6560) and code (although everything works fine), there is one thing - when i write to serial e.g. 200s1600+ (in microstepping 1/8, there is 1600 steps per 1 revolution), stepepr runs half of 1600: 800 steps (so i must multiply input value by 2)

const int dirPin=8;
const int stepPin=9;
 int speed=100;
 int steps=0;
 int stepsPerRevolution=1600;
 int stepState = LOW;
 void setup(){
   pinMode(dirPin,OUTPUT);
      pinMode(stepPin,OUTPUT);
      Serial.begin(9600);
      digitalWrite(stepPin, LOW);
 
 }
 
 void loop(){
 
  
 
 if (Serial.available()){
   char ch=Serial.read();
   if(ch>='0' && ch<='9'){
     steps=steps*10+ch-'0';
   }
   else if(ch=='+'){
     step(steps);
     steps=0;
   }
  else if(ch=='-'){
     step(-steps);
     steps=0;
   }
    else if(ch=='s'){
     speed=steps;
  Serial.print("Setting speed to ");
    Serial.println(steps);
     steps=0;
    }
   }
 }
 
 void step(int steps)
 {
  // int stepDelay=60L*1000000L / stepsPerRevolution / speed;
 int stepDelay=300000L/(16L*speed);
 int stepsLeft;
 unsigned long previousMicros=0;
 
 if(steps>0)
 {
   digitalWrite(dirPin,HIGH);
   stepsLeft=steps;  //to solve my problem, here I must multiply by 2
 }
 
 if (steps<0)
 {
   digitalWrite(dirPin,LOW);
   stepsLeft=-steps;   ////to solve my problem, here I must multiply by 2
 }
 
 while(stepsLeft>0)
 {
 if ((unsigned long)(micros() - previousMicros) >= stepDelay) 
 {
  previousMicros = micros();
  // if (stepState == LOW)
    //  stepState = HIGH;
   // else
     // stepState = LOW;
   // digitalWrite(stepPin, stepState);
digitalWrite(stepPin, !digitalRead(stepPin));  // Ooooh magic toggle code!
 stepsLeft--;
 }
 }
 }

Most of the code is from “Arduino cookbook”, but a made a little changes (for example I used micros() instead of delay())

I can think of two obvious possibilities: is your stepper actually doing 1/16 micro steps? The other is that you're trying to step too fast - try increasing the step delay.

madman23: when i write to serial e.g. 200s1600+ (in microstepping 1/8, there is 1600 steps per 1 revolution), stepepr runs half of 1600: 800 steps (so i must multiply input value by 2)

Are there situations in which it makes the correct number of steps or is it always necessary to multiply by 2?

...R

The step pin triggers on rising edges only, you need to change this line:

 digitalWrite(stepPin, !digitalRead(stepPin));  // Ooooh magic toggle code!

to

  digitalWrite (stepPin, HIGH) ;
  delayMicroseconds (5) ;
  digitalWrite (stepPin, LOW) ;

wildbill: I can think of two obvious possibilities: is your stepper actually doing 1/16 micro steps? The other is that you're trying to step too fast - try increasing the step delay.

No, there is 1/8 microstepping mode (works better than 1/16), so I think it's not about it.

Robin2: Are there situations in which it makes the correct number of steps or is it always necessary to multiply by 2?

...R

No, this situation happens with all input values of steps.

MarkT: The step pin triggers on rising edges only, you need to change this line:

 digitalWrite(stepPin, !digitalRead(stepPin));  // Ooooh magic toggle code!

to

  digitalWrite (stepPin, HIGH) ;
  delayMicroseconds (5) ;
  digitalWrite (stepPin, LOW) ;

I've changed my code and it works propoerly with steps, but now motor works faster (at speed 1 RPM, but 1600 steps - one revolution - take less than 1 minute)

Maybe this simple stepper code would be useful for testing. And there may be something useful in stepper motor basics.

...R