Using millis in a (for me) big Code

Hello there,

I've been working on a code for a while now and are close to finishing it now.
Yet I have stumbled upon the delay function problem and on replacing it with millis,
using it on smaller programs i never had problems integrating the millis function(classical blinking etc.),
but in my recent code i have a delay(1500); which completely messes up my code, and since I have only used millis on smaller programs I'm completely lost here, not even google was able to help me out on millis in bigger codes:(

For my code: I've creted a more simplified version of the code without the other loops in it. I would be very grateful if anyone could show me on how to use the millis function on the delay(1500); part, so i can study it and then apply it on the rest of the code of mine.
Without further a do here is the code:

const int buttonPin = A3; // the number of the pushbutton pin
const int motor = 13;
const int ledrd = A4; // the number of the LED pin
const int ledorange = 12;
const int linmotRaus = A1;
const int linmotRein = A2;
const int Sensor360 = 6;

// variables will change:
int buttonState = 0;
int buttonstateSensor360 = 0; // variable for reading the pushbutton status
int zahl = 5;
int ctu;
int var;
int var1;

void setup() {
  // initialize the LED pin as an output:
  pinMode(motor, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  pinMode(ledrd, OUTPUT);
  pinMode(linmotRaus, OUTPUT);
  pinMode(linmotRein, OUTPUT);
  pinMode(ledorange, OUTPUT);
  pinMode(Sensor360, INPUT);
  Serial.begin(9600);

}

void loop() {

  buttonState = digitalRead(buttonPin);
  buttonstateSensor360 = digitalRead(Sensor360);

  if (buttonState == HIGH) {
    ctu++;
    delay(200);
  }

  //Serial.println(ctu);Serial.println(var); Serial.println(var1);Serial.println(zahl);
  Serial.println((ctu == 3) && (var1 < zahl));

  if ((ctu == 1) && (var < zahl)) {
    digitalWrite(motor, HIGH);
    digitalWrite(ledrd, HIGH);

    if (buttonstateSensor360 == HIGH) {
      digitalWrite(linmotRaus, HIGH);
      var ++;
      delay(1500);
    } else {
      digitalWrite(linmotRaus, LOW);
    }
	delay(100);

  } else if ((ctu == 3) && (var1 < zahl)) {
    digitalWrite(motor, HIGH);
    digitalWrite(ledrd, HIGH);
    if (buttonstateSensor360 == HIGH) {
      digitalWrite(linmotRein, HIGH);
      var1++;
      delay(1500);
    } else {
      digitalWrite(linmotRein, LOW);
    }
  } else {
    digitalWrite(motor, LOW);
    digitalWrite(ledrd, LOW);
    // var=0;
	delay(100);
  }
  //-------------------------------------------------------------rein

}

Thanks in advance and have a great week :slight_smile:

Have a look at how millis() is used to manage timing without blocking in Several things at a time. Note how each function runs very briefly and returns to loop() so the next one can be called. Long running processes are achieved a tiny piece at a time. And there may be dozens of calls to some functions before it is actually time for anything to happen.

...R

Hello Robin,
Thanks a lot, that was exactly what i had been serching for, an example of millis used in bigger codes,
I'll read into it and thanks a lot for your time :slight_smile:

If a more extensive example would be useful have a look at Planning and Implementing a Program

...R