Loop delay avoiding

Hi guys!
I have a question, I am trying to control a stepper motor with an pololu stepper driver. I would like to change the speed of the motor with +5V singals from a parallel port, and with buttons. I have made a code and it works, but i have a problem with the delay function. The loop runs non stop and everytime the delay is used, but when I would like the speed of the motor to be for example with delay of 250ms, it runs one loop and then uses a normal delay of 500ms, and than again 250ms. Is it possible to run only one delay at a thime? I was experimenting with milis() but i failed.

const int Step = 5;     
const int Smer =  6; 
const int E = 4;
const int W = 3;

int KomandaE = 0;
int KomandaW = 0;


   

void setup() {
  // put your setup code here, to run once:
pinMode(Step, OUTPUT);
pinMode(Smer, OUTPUT);
pinMode(E, INPUT);
pinMode(W, INPUT);


}


void loop()
{
digitalWrite(Smer, LOW);
digitalWrite(Step, LOW);
Serial.begin(9600);
unsigned long currentMillis = millis();
   {
    digitalWrite(Step,HIGH); // Output high
    delay(500); // Wait
    digitalWrite(Step,LOW); // Output low
    delay(500); // Wait
  }
 KomandaE = digitalRead (E);
  if (KomandaE==HIGH) 
    {
      digitalWrite(Step,HIGH); // Output high     <== I would like to run only this delay
      delay(250); // Wait                                       ||
      digitalWrite(Step,LOW); // Output low       <===
      delay(250); // Wait   
    }
   else
   {
    digitalWrite(Step,HIGH); // Output high
    delay(500); // Wait
    digitalWrite(Step,LOW); // Output low
    delay(500); // Wait
  }
   }

You need to remove the delay() calls, as you have already experimented.

You need to understand Finite State Machines (FSM) in order to implement this type of code properly. Please look it up - there is a lot of information in this. This will allow you to separate the command and the motor running code more effectively.

this is not really the ansear as I have no understanding of what you are doing but if you say the code works with delays then this should work as a example of using millis

look at the serial prints to see the changes being made

const int Step = 5;
const int Smer =  6;
const int E = 4;
const int W = 3;

int KomandaE = 0;
int KomandaW = 0;

unsigned long interval = 500;
unsigned long prevmillis = 0;
byte toggleBit = 0;
byte prevtoggleBit = 0;


void setup() {
  // put your setup code here, to run once:
  pinMode(Step, OUTPUT);
  pinMode(Smer, OUTPUT);
  pinMode(E, INPUT);
  pinMode(W, INPUT);
  Serial.begin(9600);
  digitalWrite(Smer, LOW);
  digitalWrite(Step, LOW);
}


void loop()
{
  unsigned long currentMillis = millis();

  KomandaE = digitalRead (E);
  if (KomandaE == HIGH)
  { interval = 250;//set the interval between changes
  } else {
    interval = 500;
  }

  if (currentMillis  - prevmillis > interval) { //compair tme to timestamp
    toggleBit = ! toggleBit; //toggle a flag from 0 to 1 or 1 to 0
    prevmillis = currentMillis;//take new times stamp
  }
  if (prevtoggleBit != toggleBit) {// if the flag has changed
    if (toggleBit == 1) { //if the flag is 1
        digitalWrite(Step, HIGH);
        Serial.print("on     ");
        Serial.println(interval);
      } else { //if the flag is 0
        digitalWrite(Step, LOW);
        Serial.print("off    ");
        Serial.println(interval);
      }
  }
  prevtoggleBit = toggleBit;
}

The second example in this Simple Stepper Code uses millis() and micros() for timing.

...R Stepper Motor Basics

gpop1, im trying to do a telescope guider. I have a computer with a parallel port, the computer runs a program with star tracking software, and sends signals to the parallel port. I connected the Arduino nano with the port and I am trying to control 2 steppers. One for RA axis and one for DEC axis. The RA motor has to run at a standard speed of one revolution per minute, and everytime an error ocures with the guiding (the star isnt in the centre of the telescope), the guiding software sends a signal to the arduino to move the RA motor a couple of steps faster, to catch the star, and put it back in centre.

So i need to write a program, that runs a motor with a normal speed, and when the arduino gets a signal, to change the speed...

And thank you for the program! It works excelent, but is it possible to have more acurate faster steps? When I give the arduino the signal for faster stepping it is sending those signals quite some time, after the input pin is again LOW.

Thanks for the link Robin, i will look at your program and try to do something :) And any more help would be much appreciated.

const int Step = 5;     
const int Smer =  6; 
const int E = 7;
const int W = 4;

int KomandaE = 0;
int KomandaW = 0;


void setup() {
pinMode(Step, OUTPUT);
pinMode(Smer, OUTPUT);
pinMode(E, INPUT);
pinMode(W, INPUT);

digitalWrite(Smer, LOW);
digitalWrite(Step, LOW);
Serial.begin(9600);

}

void loop() {
  
  KomandaE = digitalRead (E);
  KomandaW = digitalRead (W);
  
if (KomandaE==LOW){         //EAST, if no error ocured, the motor runs at its standard speed
  digitalWrite(Smer, LOW);
  digitalWrite(Step,HIGH); 
  delay(500); 
  digitalWrite(Step,LOW); 
  delay(500); 
     }
    
else if(KomandaE==HIGH){      // EAST, with error, the stepping speed increased
    digitalWrite(Step,HIGH); 
    delay(250); 
    digitalWrite(Step,LOW); 
    delay(250);
    digitalWrite(Smer, LOW);
    
    }

else if (KomandaW == HIGH){     //WEST, with error, the stepping speed increased                       
    digitalWrite(Step,HIGH);    //And direction (Smer) set to high
    delay(250); 
    digitalWrite(Step,LOW);      // !!!!! IT DOES NOT WORK !!!!!!
    delay(250); 
    digitalWrite(Smer, HIGH);
  
  }
  
else if (KomandaW==LOW){       //WEST, no errors, just stepping with the normal speed and direction
    digitalWrite(Smer, LOW);
    digitalWrite(Step,HIGH); 
    delay(500); 
    digitalWrite(Step,LOW); 
    delay(500);  
    }
    

}

Here is the code that I wrote yesterday, it works for one direction, but when I change the direction the speed remains the same, I wrote some comments in the code, if you would take some time and take a look. Would it be possible to control the stepping more efficently? When the input is set to high the speed increases, but when the input is set to low, the steping speed still does a couple of fast steps and then changes to normal.

I'm not sure I understand this

else if (KomandaW == HIGH){     //WEST, with error, the stepping speed increased                       
    digitalWrite(Step,HIGH);    //And direction (Smer) set to high
    delay(250);
    digitalWrite(Step,LOW);      // !!!!! IT DOES NOT WORK !!!!!!
    delay(250);
    digitalWrite(Smer, HIGH);

  }

I get the impression the last line sets the direction. Should that not be the first line - to set the direction before there is an attempt to move.

And what does "it does not work" mean ?

Wouldn't it be a great deal simpler to have a single short function to cause the steps rather than repeating the lines

    digitalWrite(Step,HIGH);
    delay(250);
    digitalWrite(Step,LOW);
    delay(250);

in several different places?

Something like

void moveOneStep( int interval) {
    digitalWrite(Step,HIGH);
    digitalWrite(Step,LOW);
    delay(interval);
}

Have a look at Planning and Implementing a Program

...R

It works excelent, but is it possible to have more acurate faster steps? When I give the arduino the signal for faster stepping it is sending those signals quite some time, after the input pin is again LOW.

haven't got time this morning to check your code but this sounds like the digital pin is floating. Have a look at using INPUT_PULLUP for your button or how to add a pull down resistor to a button. (if you look at the top of the page for learning- references- input_pullup it explains about pin floating)

Robin2: I'm not sure I understand this

else if (KomandaW == HIGH){     //WEST, with error, the stepping speed increased                       
    digitalWrite(Step,HIGH);    //And direction (Smer) set to high
    delay(250);
    digitalWrite(Step,LOW);      // !!!!! IT DOES NOT WORK !!!!!!
    delay(250);
    digitalWrite(Smer, HIGH);

 }



I get the impression the last line sets the direction. Should that not be the first line - to set the direction before there is an attempt to move.

And what does "it does not work" mean ?

Well, this part just dosent want to work. I cant find an error, when West is set to high the speed doesnt change.

if east "works" and west doesn't, just look at both and you can see the difference.

Robin pointed out the answer and your own code comments tells you what is wrong.

gpop1 you are the man! I added the 10k resistor from gnd to the pins and now it realy works the way i want it to! :D

But still having problems with the west, I checked the code, i put the direction comand at the top of the code, so the motor should rotate in the oposite direction, but no success... Sorry guys I am a realy bad programer, and thanks for all your time.

lol read your own notes in program

  else if (KomandaW == HIGH) {    //WEST, with error, the stepping speed increased
    digitalWrite(Step, HIGH);   //And direction (Smer) set to high <-------------
    delay(250);
    digitalWrite(Step, LOW);     // !!!!! IT DOES NOT WORK !!!!!!
    delay(250);
    digitalWrite(Smer, HIGH);

it costs nothing to use long names in the code when you have 2 names that look the same its easy to get them mixed up