Help me merge a code which contains delay with main code

INTRO:
I have made a sketch which controls the motor and does what i need it to do which is:... start the motor... wait for one limit switch to be pushed, stop the motor wait for the SPECIFIED TIME to pass and then start the motor again and keep going until it touches another switch.

PROBLEM:
The problem is that it works with delay. i haven't been able to modify the code so that it could work with millis... i have tried but failed. if you wanna hear my failure stories just ask.. i'd be glad to link you to my documentation. now since my main sketch and board contains a long code which watches over many functions including button states.... i cannot afford for the motor to use delay in the sketch. currently i am using another micro controller (Attiny85) to carry out this motor function... which is a waste.. i know.. which is why i need to merge them together. any help would be greatly appreciated. i think i'd be changing the code later since my design will change so if you could teach me to fish instead of giving me a fish i'd be very thankful.

CODE LINKS

The complete code i am currently using which does not contain the motor code:

Only the motor code:

NOTE: I did not include any direct downloads links because i realize nobody wants to download any thing.. so i uploaded the code to pastebin which is like github. so take a look and tell me what you think. any other code improvements or any suggestions which is not related to my problem will also be very much appreciated.

unsigned long currentMillis;
unsigned long previousMillis;
unsigned long previous2Millis;
const long onInterval = 10000;
const long offInterval = 20000;
const byte in1 = 10;
const byte in2 = 11;
const byte sw1 = 7;  //this is left switch
const byte sw2 = 5;  //this is right switch
boolean sw1Status;
boolean sw2Status;
int stateMachine;
 
void setup() {
  // put your setup code here, to run once:
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(sw1, INPUT_PULLUP);
  pinMode(sw2, INPUT_PULLUP);
  Serial.begin(9600);
 
 
}
 
void loop() {
 
  currentMillis = millis();
  sw1Status = digitalRead(sw1);
  sw2Status = digitalRead(sw2);
  switch (stateMachine) {
    case 0:
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
    if(sw1Status == HIGH && sw2Status == LOW)
    {
      Serial.println("Just chose case 2 from case 0");
      delay(5000);              //waiting before going to the next command
      stateMachine = 2;
    } else if (sw1Status == LOW && sw2Status == HIGH) {  
      delay(5000);
      stateMachine = 1;  
    }
    break;
 
    case 1:
    if(sw2Status == HIGH) {  
        digitalWrite(in1, HIGH);
        digitalWrite(in2, LOW);
    } else if (sw2Status == LOW) {  
      stateMachine = 0;  // go to reset
    }
    break;
 
    case 2:  
    if(sw1Status == HIGH) {
      digitalWrite(in1, LOW);  
      digitalWrite(in2, HIGH);
    } else if (sw1Status == LOW) {    
      stateMachine = 0;
    }
    break;
 
   
  }
  }

Please post the code here - especially the motor code that works with delay() and you best attempt at converting that to use millis().

The demo Several Things at a Time i illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

...R

Robin2:
Please post the code here - especially the motor code that works with delay() and you best attempt at converting that to use millis().

The demo Several Things at a Time i illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

...R

I actually have read that. i have never posted anything without researching and when you see my main code you will see that i have used millis in some places so i am not new to the concept. i was just having difficulty getting rid of the delay which is why i started a new topic... i don't understand why would people assume that i have not checked out 'using millis for beginners' or any other thread introductory thread in this forum.

Now that is off your chest please post the code that you are having problems with replacing delay()

manhoosbilli1:
i don't understand why would people assume that i have not checked out 'using millis for beginners' or any other thread introductory thread in this forum.

I did not assume anything or make any criticism. I asked you to do something that you have not done and I provided some links in case they might be useful.

...R

UKHeliBob:
Now that is off your chest please post the code that you are having problems with replacing delay()

haha. i have posted the link to it. 'motor code' ^

Robin2:
Please post the code here - especially the motor code that works with delay() and you best attempt at converting that to use millis().

The demo Several Things at a Time i illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

...R

As i said.. i have posted the link to the motor code above. and as for my last attempt.... here it is: Working motor reversal code with a unsteady delay - Pastebin.com

I apologize for my tone. you were only trying to help. i thought you had different intentions.

problem with this code is that the motor does not stay turned off for consistent period.

Based on the motor code in the opening post, add extra state(s) to your motor code where you can wait for a given period of time using a millis() based approach. When the millis() based timing expires, go to the next state.

Might also work for the code in reply #6.

manhoosbilli1:
As i said.. i have posted the link to the motor code above.

I was aware of that when I wrote Reply #1.

If you want our help why make us go to pastebin to see your code when you can post it here?

...R

Having your code posted here gives people an idea of where you are having issues. Code posted on external sites usually don't get looked at. If the code is too long to fit into one post, it can simply be included as an attachment.

An additional reason is that people who have similar problems may be able to follow the thread in the future and see what got modified etc, which is hard to do if the problematic code is ever removed from the external site.

Anyway, having said all that, I've not looked at your code but I'm working on something vaguely similar at the moment.

This is a quick and (very) dirty idea of how you can go about things

it in (sort of) pseudo code and very much untested, but it might give you some food for thought

bool motor_action_required = false;
unsigned long limit_1_reached_time;
unsigned long motor_pause_time = (however many milliseconds you need to stop);
bool motor_started = false;
uint8_t movement_phase = 1;

loop() {

  ....
  
  if (some_thing_happened_that_also_required_motor) {
   ...
   ...
    motor_action_required = true;
    ...
  }
  
  ...
  
  if (motor_action_required) {
    do_motor_things()
  }
  
  ...
}


void do_motor_things() {

  if ((movement_phase == 1) && (!motor_started)) {
    move_motor();
    motor_started = true;
  }
  if (movement_phase = 1) {
    if (limit_switch_1 has been hit) { 
     stop_motor();
     limit_1_reached_time = millis();
     movement_phase = 2;
    }
  }
  if ((movement_phase == 2) && ((millis() - limit_1_reached_time) >= motor_pause_time)) {
   move_motor();
   movement_phase = 3;
  }
  if (movement_phase == 3) {
   if (limit_switch_2 has been hit) {
   stop_motor();
   motor_action_required = false;
   motor_started = false;
   movement_phase = 1;
   }
  }
}

you might need to do additional checks, such as not trying to do anything with the motor while motor_action_required is true, or build in conditionals which will deal with a routine wanting to control the motor while do_motor_things() has control of the motor.

The whole point is to use flags to 1) keep track of where you are in your logic and 2) set and clear conditions of each phase of your operation.