Millis timing relays start with a pushbutton

Hello i start my code witch put on and off 8 relays with a button. With the delay function this was never a problem bud im made a new code with the millis function so i can control it more easy and accurate. I just cant combine the push button code with my millis code.
I use a Mega and sometimes a Uno

This is the code is use for my button:

Serial.println(duinoinValue);
  //Check if I can start
  duinoinValue = digitalRead(duinoin);
  delay(20);
  Serial.println(duinoinValue);
  if(duinoinValue == HIGH){

    //Stuff to exicute 

     digitalWrite(duinoinValue, LOW);
  }

My millis code

int in1 = 4;
int in2 = 5;
int in3 = 6;
int in4 = 7;
int in5 = 8;
int in6 = 9;
int in7 = 10;
int in8 = 11;





/// HIER VERANDEREN
int startDelay = 2000;

int timer1 = 1000;  //...
int timer2 = 2000;  //...
int timer3 = 3000;  //...
int timer4 = 4000;  //...
int timer5 = 1500;  //...
int timer6 = 2500;  //...
int timer7 = 3500;  //...
int timer8 = 500;  //...

int timeOpen1 = 1000;   //...
int timeOpen2 = 1000;   //...
int timeOpen3 = 1000;   //...
int timeOpen4 = 1000;   //...
int timeOpen5 = 1000;   //...
int timeOpen6 = 1000;   //...
int timeOpen7 = 1000;   //...
int timeOpen8 = 1000;   //...

int timerStop = 5000; //Wanneer het stopt

int stopped = 0;

int teller1 = 0;
int teller2 = 0;
int teller3 = 0;
int teller4 = 0;
int teller5 = 0;
int teller6 = 0;
int teller7 = 0;
int teller8 = 0;

int done1 = 0;
int done2 = 0;
int done3 = 0;
int done4 = 0;
int done5 = 0;
int done6 = 0;
int done7 = 0;
int done8 = 0;
int donestop1 = 0;
int donestop2 = 0;
int donestop3 = 0;
int donestop4 = 0;
int donestop5 = 0;
int donestop6 = 0;
int donestop7 = 0;
int donestop8 = 0;

int tellerStop = 0;






int teller = 1;


void setup() {
  Serial.begin(9600);

  teller = 1;
  teller = millis();

  teller1 = (timer1 + teller + startDelay);
  teller2 = (timer2 + teller + startDelay);
  teller3 = (timer3 + teller + startDelay);
  teller4 = (timer4 + teller + startDelay);
  teller5 = (timer5 + teller + startDelay);
  teller6 = (timer6 + teller + startDelay);
  teller7 = (timer7 + teller + startDelay);
  teller8 = (timer8 + teller + startDelay);

  Serial.println(teller1);
  Serial.println(teller2);
  Serial.println(teller3);
  Serial.println(teller4);
  Serial.println(teller5);
  Serial.println(teller6);
  Serial.println(teller7);
  Serial.println(teller8);

  tellerStop = (timerStop + teller + startDelay);

  Serial.println(tellerStop);

  pinMode(in1, OUTPUT);
  digitalWrite(in1, HIGH);
  pinMode(in2, OUTPUT);
  digitalWrite(in2, HIGH);
  pinMode(in3, OUTPUT);
  digitalWrite(in3, HIGH);
  pinMode(in4, OUTPUT);
  digitalWrite(in4, HIGH);
  pinMode(in5, OUTPUT);
  digitalWrite(in5, HIGH);
  pinMode(in6, OUTPUT);
  digitalWrite(in6, HIGH);
  pinMode(in7, OUTPUT);
  digitalWrite(in7, HIGH);
  pinMode(in8, OUTPUT);
  digitalWrite(in8, HIGH);
}
void loop() {

  if (teller <= tellerStop) {
    teller = millis();
    Serial.println(teller);
    if (teller <= startDelay) {
      Serial.println("Waiting....");
    }
  } else {
    if (stopped == 0) {
      digitalWrite(in1, HIGH);
      digitalWrite(in2, HIGH);
      digitalWrite(in3, HIGH);
      digitalWrite(in4, HIGH);
      digitalWrite(in5, HIGH);
      digitalWrite(in6, HIGH);
      digitalWrite(in7, HIGH);
      digitalWrite(in8, HIGH);
      Serial.println("STOP");
      stopped = 1;
    }
  }





  if (teller >= teller1) {
    if (done1 == 0) {
      digitalWrite(in1, LOW);
      Serial.println("1 LOW");
      done1 = 1;
    }
  }

  if (teller >= (teller1 + timeOpen1)) {
    if (donestop1 == 0) {

      digitalWrite(in1, HIGH);
      Serial.println("dicht");
      donestop1 = 1;
    }
  }

  if (teller >= teller2) {
    if (done2 == 0) {
      digitalWrite(in2, LOW);
      Serial.println("2 LOW");
      done2 = 1;
    }
  }

  if (teller >= (teller2 + timeOpen2)) {
    if (donestop2 == 0) {

      digitalWrite(in2, HIGH);
      Serial.println("dicht");
      donestop2 = 1;
    }
  }

  if (teller >= teller3) {
    if (done3 == 0) {
      digitalWrite(in3, LOW);
      Serial.println("3 LOW");
      done3 = 1;
    }
  }

  if (teller >= (teller3 + timeOpen3)) {
    if (donestop3 == 0) {

      digitalWrite(in3, HIGH);
      Serial.println("dicht");
      donestop3 = 1;
    }
  }

  if (teller >= teller4) {
    if (done4 == 0) {
      digitalWrite(in4, LOW);
      Serial.println("4 LOW");
      done4 = 1;
    }
  }

  if (teller >= (teller4 + timeOpen4)) {
    if (donestop4 == 0) {

      digitalWrite(in4, HIGH);
      Serial.println("dicht");
      donestop4 = 1;
    }
  }


  if (teller >= teller5) {
    if (done5 == 0) {
      digitalWrite(in5, LOW);
      Serial.println("5 LOW");
      done5 = 1;
    }
  }

  if (teller >= (teller5 + timeOpen5)) {
    if (donestop5 == 0) {

      digitalWrite(in5, HIGH);
      Serial.println("dicht");
      donestop5 = 1;
    }
  }


  if (teller >= teller6) {
    if (done6 == 0) {
      digitalWrite(in6, LOW);
      Serial.println("6 LOW");
      done6 = 1;
    }
  }

  if (teller >= (teller6 + timeOpen6)) {
    if (donestop6 == 0) {
      digitalWrite(in6, HIGH);
      Serial.println("dicht");
      donestop6 = 1;
    }
  }

  if (teller >= teller7) {
    if (done7 == 0) {
      digitalWrite(in7, LOW);
      Serial.println("7 LOW");
      done7 = 1;
    }
  }

  if (teller >= (teller7 + timeOpen7)) {
    if (donestop7 == 0) {
      digitalWrite(in7, HIGH);
      Serial.println("dicht");
      donestop7 = 1;
    }
  }

  if (teller >= teller8) {
    if (done8 == 0) {
      digitalWrite(in8, LOW);
      Serial.println("8 LOW");
      done8 = 1;
    }
  }

  if (teller >= (teller8 + timeOpen8)) {
    if (donestop8 == 0) {

      digitalWrite(in8, HIGH);
      Serial.println("dicht");
      donestop8 = 1;
    }
  }




  delay(50);


}


I hope you guys can help.
cheers
Willem

Hello, do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (including code tags and necessary documentation of your ask).

Some tutorials to help you:

tnx, i did not do a question on a form for a long long time. So sorry bud i think i modify my question better now.

I looked in a lot of tutorials and seen a bit to much youtube videos lol And i can find a lot. Bud i cant combine my to codes i a way they work.

the function millis() counts up milliseconds until the counter reaches
the huge number of 4.294.967.296

vaiables of type int can store only values up to 32.767 so after 32 seconds your command

teller = millis();

goes wrong

all variables that deal with millis() should be of type unsigned long

there are standard-variable-names used for timing based on millis()
currentMillis = millis()

previousMillis

interval

The proper way of measuring if a certain interval of time has passed by is

if (currentMillis - previousMillis >= interval)

bevause this way the rollover from max-value to zero is handled automatically right.

You should write a description that describes step by step what should happen from the perspecive of a bystanding watcher.
What behaviour does the watcher just see?

The code for the button-press depends on what should happen

powering up my microcontroller: what should happen?
I press the button: what should happen?
what should happen?

your variable-name "teller" says nothing. From a codeline like
teller = millis() I can conclude "has to do with timing".

"teller" means "counter" that's a variable-name that doesn't explain what the value is used for

If you want help in an english speaking-forum you should at least add english comments to your variable-names

best regards Stefan

1 Like

Do yourself a big favour, start adding good comments to your code.

Lots of timers are better implemented using a structure.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.