Set output HIGH or LOW with delay without using delay() problem

One last question before finishing my first project, I hope...

Now everything works well in my code (more or less) and I just want to set PIN 9 (SPin) HIGH when input A2 is HIGH but with delay of 1 second, and when A2 is LOW I want to set it LOW with delay of 0.7 seconds.

Tried that in code below but it doesn't work, it sets output HIGH or LOW right away.
What did I do wrong?

#include <LiquidCrystal.h>
#include <StopWatch.h>


LiquidCrystal lcd(7, 6, 5, 4, 3, 2);


StopWatch sw_secs(StopWatch::SECONDS);



int VPin = 12;
int MPin = 8;
int SPin = 9;
int DPG = 10;
int DPD = 11;
int GPin = A2;
int Tipke;


void setup() {
  //Welcome message

  lcd.begin(16, 2);
  lcd.setCursor(4, 0);
  lcd.print("SOMETHING");
  delay(1000);
  lcd.setCursor(5, 1);
  lcd.print("SOMETHING");
  delay(2000);

  //My way to blink displayed text
  lcd.clear();
  delay(500);

  lcd.setCursor(5, 1);
  lcd.print("SOMETHING");
  lcd.setCursor(4, 0);
  lcd.print("SOMETHING");
  delay(500);
  lcd.clear();
  delay(500);

  lcd.setCursor(5, 1);
  lcd.print("SOMETHING");
  lcd.setCursor(4, 0);
  lcd.print("SOMETHING");
  delay(500);


  
  pinMode(VPin, OUTPUT);
  pinMode(MPin, OUTPUT);
  pinMode(SPin, OUTPUT);
  pinMode(DPG, OUTPUT);
  pinMode(DPD, OUTPUT);
  pinMode(GPin, INPUT);
  lcd.clear();

  sw_secs.start();

}




void loop()
{
  
  static unsigned long Time;
  
  
  
  
  if (digitalRead(GPin) == LOW) {

   unsigned long TimerA;
   TimerA=millis();
    if (millis() - Time >= 1000)
    {
      
      Time += 1000;
      Consumption(); // 
    }
    digitalWrite(VPin, LOW);
    digitalWrite(MPin, LOW);

//PROBLEMATIC PART I GUESS
 if (TimerA - millis() >= 1000){     
  
      digitalWrite(SPin, LOW);
      
}
    
    } 
  
else  {
  unsigned long TimerB;
  TimerB=millis();
    if (millis() - Time >= 1000)
    {
      Time += 1000;
      Working(); 
    } 
    digitalWrite(VPin, HIGH);
    digitalWrite(MPin, HIGH);
  
if (TimerB - millis() >= 700){
  
      digitalWrite(SPin, HIGH);
      
}
    
}

  
  Buttons();
  Vrijeme();
  
  }




void Consumption() {
  lcd.clear();
  lcd.setCursor(3, 0);
  lcd.print("STATUS: ON");
  lcd.setCursor(0, 1);
  lcd.print("Consupt : ");
  lcd.setCursor(12, 1);
  lcd.print("kg/h");
  int Potenciometar = A1;
  int Pot1 = 0;
  int Pot2 = 0;
  Pot1 = analogRead(A1) / 10;
  Pot2 = Pot1 / 1.023;
  lcd.setCursor(9, 1);
  lcd.print(Pot2);
}



void Vrijeme() {

}


void Working() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("STATUS: WORKING");
  lcd.setCursor(0, 1);
  lcd.print("Consupt: ");
  lcd.setCursor(12, 1);
  lcd.print("kg/h");
  int Potenciometar = A1;
  int Pot1 = 0;
  int Pot2 = 0;
  Pot1 = analogRead(A1) / 10;
  Pot2 = Pot1 / 1.023;
  lcd.setCursor(9, 1);
  lcd.print(Pot2);
}


void Buttons()
{
  
  Tipke = analogRead(A0);
  if (Tipke < 50) {
    lcd.setCursor(0, 0);
    lcd.print("          UP     ");
    digitalWrite(DPG, HIGH);
  }

  else if (Tipke < 250) {
    lcd.setCursor(0, 0);
    lcd.print("        DOWN   ");
    digitalWrite(DPD, HIGH);
  }

  else if (Tipke < 450) {
    lcd.print("");
    lcd.setCursor(0, 0);
    lcd.print("UP Time:         ");

    int h, m, s;
    s = sw_secs.elapsed();
    m = s / 60;
    h = s / 3600;
    s = s - m * 60;
    m = m - h * 60;

    lcd.setCursor(0, 1);
    lcd.print(h);
    lcd.setCursor(1, 1);
    lcd.print(" hrs                  ");
    lcd.setCursor(6, 1);
    lcd.print(m);
    lcd.setCursor(8, 1);
    lcd.print("min");
    lcd.setCursor(9, 1);

  }
  else {
    digitalWrite(DPG, LOW);
    digitalWrite(DPD, LOW);
  }
}
   unsigned long TimerA;
   TimerA=millis();

Time != Timer

Names that make sense make it easier to understand your code.

//PROBLEMATIC PART I GUESS
 if (TimerA - millis() >= 1000){

Since you just assigned a value to TimerA a few nanoseconds ago, there is not much chance that it is going to take 1 second or more to get to this statement.

What event, EXACTLY, is TimerA supposed to hold the time for?

TimerA is supposed to hold time for:

digitalWrite(SPin, LOW);

So that SPin is set LOW 1 second after +5V has been removed from A2.

TimerB is suposed to hold time for:

digitalWrite(SPin, HIGH);

So that SPin is set HIGH 0.7 seconds after +5V has been applied to A2.

Have you looked at the blink without delay example, to see how it declares time variables? To see how it checks if it is time to do something?

If TimerA is supposed to hold the time that the LED is turned on, don't you think that LEDOnTime would be a more reasonable name?

I agree that names I've used aren't the best for easier understanding the code.

I have been looking at that example but this example turns on or off LED indefinitely, I just need delay... And probably what I'm asking is stupid or simple, but I've just started with Arduino...

something like

  unsigned long currentTime = millis();
  if (digitalRead(GPin) == LOW) {
    if (currentTime - startDelayTime >= delayOnInterval) {
      digitalWrite(SPin, HIGH);
      stopDelayTime = currentTime;
    }
  } else {
    if (currentTime - stopDelayTime >= delayOffInterval) {
      digitalWrite(SPin, LOW);
      startDelayTime = currentTime;
    }
  }

I have been looking at that example but this example turns on or off LED indefinitely, I just need delay... And probably what I'm asking is stupid or simple, but I've just started with Arduino...

The example shows how to do something at a later time, independent of what necessitated the later action.

You need to write code that looks something like:

void loop()
{
    // If it necessary to turn the LED on, turn it on and record the time

    // Is the LED on and has it been on long enough? If so, turn it off and clear the on time
    if(LedOnTime > 0 && millis() - LedOnTime >= onTime)
    {
       // Turn LED off
       LedOnTime = 0;
    }
}

Notice that turning the LED off is completely independent of turning it on, though there is, obviously, a relationship.

I've tried something but it doesn't seem to work...

#include <LiquidCrystal.h>
#include <StopWatch.h>


LiquidCrystal lcd(7, 6, 5, 4, 3, 2);


StopWatch sw_secs(StopWatch::SECONDS);



int VPin = 12;
int MPin = 8;
int SPin = 9;
int DPG = 10;
int DPD = 11;
int GPin = A2;
int Tipke;


void setup() {
  //Welcome message

  lcd.begin(16, 2);
  lcd.setCursor(4, 0);
  lcd.print("SOMETHING");
  delay(1000);
  lcd.setCursor(5, 1);
  lcd.print("SOMETHING");
  delay(2000);

  //My way to blink displayed text
  lcd.clear();
  delay(500);

  lcd.setCursor(5, 1);
  lcd.print("SOMETHING");
  lcd.setCursor(4, 0);
  lcd.print("SOMETHING");
  delay(500);
  lcd.clear();
  delay(500);

  lcd.setCursor(5, 1);
  lcd.print("SOMETHING");
  lcd.setCursor(4, 0);
  lcd.print("SOMETHING");
  delay(500);


  
  pinMode(VPin, OUTPUT);
  pinMode(MPin, OUTPUT);
  pinMode(SPin, OUTPUT);
  pinMode(DPG, OUTPUT);
  pinMode(DPD, OUTPUT);
  pinMode(GPin, INPUT);
  lcd.clear();

  sw_secs.start();

}




void loop()
{
  
  static unsigned long Time;
  
  
  
  
  if (digitalRead(GPin) == LOW) {

   unsigned long LedOffTime;
   unsigned long offTime=700;

    if (millis() - Time >= 1000)
    {
      
      Time += 1000;
      Consumption();
    }
    digitalWrite(VPin, LOW);
    digitalWrite(MPin, LOW);

//PROBLEMATIC PART I GUESS
 if (LedOffTime > 0 && millis() - LedOffTime >= offTime){     
  
      digitalWrite(SPin, LOW);
      LedOffTime = 0;
      
}
    
    } 
  
else  {
  unsigned long LedOnTIme;
  unsigned long onTime=1000;

    if (millis() - Time >= 1000)
    {
      Time += 1000;
      Working(); 
    } 
    digitalWrite(VPin, HIGH);
    digitalWrite(MPin, HIGH);
  
if (LedOnTime > 0 && millis() - LedOnTime >= onTime){
  
      digitalWrite(SPin, HIGH);
      LedOnTime = 0;
      
}
    
}

  
  Buttons();
  Vrijeme();
  
  }




void Consumption() {
  lcd.clear();
  lcd.setCursor(3, 0);
  lcd.print("STATUS: ON");
  lcd.setCursor(0, 1);
  lcd.print("Consupt : ");
  lcd.setCursor(12, 1);
  lcd.print("kg/h");
  int Potenciometar = A1;
  int Pot1 = 0;
  int Pot2 = 0;
  Pot1 = analogRead(A1) / 10;
  Pot2 = Pot1 / 1.023;
  lcd.setCursor(9, 1);
  lcd.print(Pot2);
}



void Vrijeme() {

}


void Working() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("STATUS: WORKING");
  lcd.setCursor(0, 1);
  lcd.print("Consupt: ");
  lcd.setCursor(12, 1);
  lcd.print("kg/h");
  int Potenciometar = A1;
  int Pot1 = 0;
  int Pot2 = 0;
  Pot1 = analogRead(A1) / 10;
  Pot2 = Pot1 / 1.023;
  lcd.setCursor(9, 1);
  lcd.print(Pot2);
}


void Buttons()
{
  
  Tipke = analogRead(A0);
  if (Tipke < 50) {
    lcd.setCursor(0, 0);
    lcd.print("          UP     ");
    digitalWrite(DPG, HIGH);
  }

  else if (Tipke < 250) {
    lcd.setCursor(0, 0);
    lcd.print("        DOWN   ");
    digitalWrite(DPD, HIGH);
  }

  else if (Tipke < 450) {
    lcd.print("");
    lcd.setCursor(0, 0);
    lcd.print("UP Time:         ");

    int h, m, s;
    s = sw_secs.elapsed();
    m = s / 60;
    h = s / 3600;
    s = s - m * 60;
    m = m - h * 60;

    lcd.setCursor(0, 1);
    lcd.print(h);
    lcd.setCursor(1, 1);
    lcd.print(" hrs                  ");
    lcd.setCursor(6, 1);
    lcd.print(m);
    lcd.setCursor(8, 1);
    lcd.print("min");
    lcd.setCursor(9, 1);

  }
  else {
    digitalWrite(DPG, LOW);
    digitalWrite(DPD, LOW);
  }
}

Your time variables need to have global scope and extent - declare them at top level.

Did that, it didn't change anything... If I did that right...

#include <LiquidCrystal.h>
#include <StopWatch.h>


LiquidCrystal lcd(7, 6, 5, 4, 3, 2);


StopWatch sw_secs(StopWatch::SECONDS);



int VPin = 12;
int MPin = 8;
int SPin = 9;
int DPG = 10;
int DPD = 11;
int GPin = A2;
int Tipke;


 unsigned long LedOnTime;
 unsigned long onTime=1000;
 unsigned long LedOffTime;
 unsigned long offTime=1000;


void setup() {
  //Welcome message

  lcd.begin(16, 2);
  lcd.setCursor(4, 0);
  lcd.print("SOMETHING");
  delay(1000);
  lcd.setCursor(5, 1);
  lcd.print("SOMETHING");
  delay(2000);

  //My way to blink displayed text
  lcd.clear();
  delay(500);

  lcd.setCursor(5, 1);
  lcd.print("SOMETHING");
  lcd.setCursor(4, 0);
  lcd.print("SOMETHING");
  delay(500);
  lcd.clear();
  delay(500);

  lcd.setCursor(5, 1);
  lcd.print("SOMETHING");
  lcd.setCursor(4, 0);
  lcd.print("SOMETHING");
  delay(500);


  
  pinMode(VPin, OUTPUT);
  pinMode(MPin, OUTPUT);
  pinMode(SPin, OUTPUT);
  pinMode(DPG, OUTPUT);
  pinMode(DPD, OUTPUT);
  pinMode(GPin, INPUT);
  lcd.clear();

  sw_secs.start();

}




void loop()
{
  
  static unsigned long Time;
  
  
  
  
  if (digitalRead(GPin) == LOW) {

   

    if (millis() - Time >= 1000)
    {
      
      Time += 1000;
      Consumption();
    }
    digitalWrite(VPin, LOW);
    digitalWrite(MPin, LOW);

//PROBLEMATIC PART I GUESS
 if (LedOffTime > 0 && millis() - LedOffTime >= offTime){     
  
      digitalWrite(SPin, LOW);
      LedOffTime = 0;
      
}
    
    } 
  
else  {
  
    if (millis() - Time >= 1000)
    {
      Time += 1000;
      Working(); 
    } 
    digitalWrite(VPin, HIGH);
    digitalWrite(MPin, HIGH);
  
if (LedOnTime > 0 && millis() - LedOnTime >= onTime){
  
      digitalWrite(SPin, HIGH);
      LedOnTime = 0;
      
}
    
}

  
  Buttons();
  Vrijeme();
  
  }




void Consumption() {
  lcd.clear();
  lcd.setCursor(3, 0);
  lcd.print("STATUS: ON");
  lcd.setCursor(0, 1);
  lcd.print("Consupt : ");
  lcd.setCursor(12, 1);
  lcd.print("kg/h");
  int Potenciometar = A1;
  int Pot1 = 0;
  int Pot2 = 0;
  Pot1 = analogRead(A1) / 10;
  Pot2 = Pot1 / 1.023;
  lcd.setCursor(9, 1);
  lcd.print(Pot2);
}



void Vrijeme() {

}


void Working() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("STATUS: WORKING");
  lcd.setCursor(0, 1);
  lcd.print("Consupt: ");
  lcd.setCursor(12, 1);
  lcd.print("kg/h");
  int Potenciometar = A1;
  int Pot1 = 0;
  int Pot2 = 0;
  Pot1 = analogRead(A1) / 10;
  Pot2 = Pot1 / 1.023;
  lcd.setCursor(9, 1);
  lcd.print(Pot2);
}


void Buttons()
{
  
  Tipke = analogRead(A0);
  if (Tipke < 50) {
    lcd.setCursor(0, 0);
    lcd.print("          UP     ");
    digitalWrite(DPG, HIGH);
  }

  else if (Tipke < 250) {
    lcd.setCursor(0, 0);
    lcd.print("        DOWN   ");
    digitalWrite(DPD, HIGH);
  }

  else if (Tipke < 450) {
    lcd.print("");
    lcd.setCursor(0, 0);
    lcd.print("UP Time:         ");

    int h, m, s;
    s = sw_secs.elapsed();
    m = s / 60;
    h = s / 3600;
    s = s - m * 60;
    m = m - h * 60;

    lcd.setCursor(0, 1);
    lcd.print(h);
    lcd.setCursor(1, 1);
    lcd.print(" hrs                  ");
    lcd.setCursor(6, 1);
    lcd.print(m);
    lcd.setCursor(8, 1);
    lcd.print("min");
    lcd.setCursor(9, 1);

  }
  else {
    digitalWrite(DPG, LOW);
    digitalWrite(DPD, LOW);
  }
}

marko5000:
Now everything works well in my code (more or less) and I just want to set PIN 9 (SPin) HIGH when input A2 is HIGH but with delay of 1 second, and when A2 is LOW I want to set it LOW with delay of 0.7 seconds.

When the delays start, do you want to lock out further input, or allow a change of input to initiate a new delay?

I'm not sure I understood the question but this Pin that I want to set high after delay is used to start one part of the machine I'm building, so whenever I press (and while I'm holding) the Start button (which is connecting +5V to A2) this part of the machine needs to start operating with 1 second delay and also continue operating for 0.7 seconds after start button is released (after i remove +5V from A2).

Point out, please, the line in the code where you assign a value to LedOffTime.

Point out, please, the line in the code where you assign a value to LedOnTime.

Values to LedOffTime and LedOnTime are assigned in these parts of code:

if (LedOffTime > 0 && millis() - LedOffTime >= offTime){     
  
      digitalWrite(SPin, LOW);
      LedOffTime = 0; //HERE
      
}
if (LedOnTime > 0 && millis() - LedOnTime >= onTime){
  
      digitalWrite(SPin, HIGH);
      LedOnTime = 0; //HERE
      
}

Should I assign values to LedOffTime and LedOnTIme also in these lines?

unsigned long LedOnTime; //HERE
 unsigned long onTime=1000;
 unsigned long LedOffTime; //HERE
 unsigned long offTime=1000;
unsigned long LedOffTime;
...
    //PROBLEMATIC PART I GUESS
    if (LedOffTime > 0 && millis() - LedOffTime >= offTime) {

      digitalWrite(SPin, LOW);
      LedOffTime = 0;

    }

LedOffTime starts as zero. It will never be greater than zero so that code inside the "if" will never be executed.

Plus, you only ever assign zero to it. So it is a pretty useless variable. :slight_smile:

So value I need to assign to it should be time of delay?
If so, what is offTime for?

And if I assign millis() to LedOnTime and LedOffTime they have something to start with, but it doesn't work...

I'm confused.

marko5000:
And if I assign millis() to LedOnTime and LedOffTime they have something to start with, but it doesn't work...

I'm confused.

Please post the complete code of your latest version.

Here is code of latest version:

#include <LiquidCrystal.h>
#include <StopWatch.h>



LiquidCrystal lcd(7, 6, 5, 4, 3, 2);


StopWatch sw_secs(StopWatch::SECONDS);



int VPin = 12;
int MPin = 8;
int SPin = 9;
int DPG = 10;
int DPD = 11;
int GPin = A2;
int Tipke;


 unsigned long LedOnTime=millis();
 unsigned long onTime=1000;
 unsigned long LedOffTime=millis();
 unsigned long offTime=1000;


void setup() {
  //Welcome message

  lcd.begin(16, 2);
  lcd.setCursor(4, 0);
  lcd.print("SOMETHING");
  delay(1000);
  lcd.setCursor(5, 1);
  lcd.print("SOMETHING");
  delay(2000);

  //My way to blink displayed text
  lcd.clear();
  delay(500);

  lcd.setCursor(5, 1);
  lcd.print("SOMETHING");
  lcd.setCursor(4, 0);
  lcd.print("SOMETHING");
  delay(500);
  lcd.clear();
  delay(500);

  lcd.setCursor(5, 1);
  lcd.print("SOMETHING");
  lcd.setCursor(4, 0);
  lcd.print("SOMETHING");
  delay(500);


  
  pinMode(VPin, OUTPUT);
  pinMode(MPin, OUTPUT);
  pinMode(SPin, OUTPUT);
  pinMode(DPG, OUTPUT);
  pinMode(DPD, OUTPUT);
  pinMode(GPin, INPUT);
  lcd.clear();

  sw_secs.start();

}




void loop()
{
  
  static unsigned long Time;
  
  
  
  
  if (digitalRead(GPin) == LOW) {

   

    if (millis() - Time >= 1000)
    {
      
      Time += 1000;
      Consumption();
    }
    digitalWrite(VPin, LOW);
    digitalWrite(MPin, LOW);

//PROBLEMATIC PART I GUESS
 if (LedOffTime > 0 && millis() - LedOffTime >= offTime){     
  
      digitalWrite(SPin, LOW);
      LedOffTime = 0;
      
}
    
    } 
  
else  {
  
    if (millis() - Time >= 1000)
    {
      Time += 1000;
      Working(); 
    } 
    digitalWrite(VPin, HIGH);
    digitalWrite(MPin, HIGH);
  
if (LedOnTime > 0 && millis() - LedOnTime >= onTime){
  
      digitalWrite(SPin, HIGH);
      LedOnTime = 0;
      
}
    
}

  
  Buttons();
  Vrijeme();
  
  }




void Consumption() {
  lcd.clear();
  lcd.setCursor(3, 0);
  lcd.print("STATUS: ON");
  lcd.setCursor(0, 1);
  lcd.print("Consupt : ");
  lcd.setCursor(12, 1);
  lcd.print("kg/h");
  int Potenciometar = A1;
  int Pot1 = 0;
  int Pot2 = 0;
  Pot1 = analogRead(A1) / 10;
  Pot2 = Pot1 / 1.023;
  lcd.setCursor(9, 1);
  lcd.print(Pot2);
}



void Vrijeme() {

}


void Working() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("STATUS: WORKING");
  lcd.setCursor(0, 1);
  lcd.print("Consupt: ");
  lcd.setCursor(12, 1);
  lcd.print("kg/h");
  int Potenciometar = A1;
  int Pot1 = 0;
  int Pot2 = 0;
  Pot1 = analogRead(A1) / 10;
  Pot2 = Pot1 / 1.023;
  lcd.setCursor(9, 1);
  lcd.print(Pot2);
}


void Buttons()
{
  
  Tipke = analogRead(A0);
  if (Tipke < 50) {
    lcd.setCursor(0, 0);
    lcd.print("          UP     ");
    digitalWrite(DPG, HIGH);
  }

  else if (Tipke < 250) {
    lcd.setCursor(0, 0);
    lcd.print("        DOWN   ");
    digitalWrite(DPD, HIGH);
  }

  else if (Tipke < 450) {
    lcd.print("");
    lcd.setCursor(0, 0);
    lcd.print("UP Time:         ");

    int h, m, s;
    s = sw_secs.elapsed();
    m = s / 60;
    h = s / 3600;
    s = s - m * 60;
    m = m - h * 60;

    lcd.setCursor(0, 1);
    lcd.print(h);
    lcd.setCursor(1, 1);
    lcd.print(" hrs                  ");
    lcd.setCursor(6, 1);
    lcd.print(m);
    lcd.setCursor(8, 1);
    lcd.print("min");
    lcd.setCursor(9, 1);

  }
  else {
    digitalWrite(DPG, LOW);
    digitalWrite(DPD, LOW);
  }
}

Now I've tried something with plcLib.h, it seemed promising, but it doesn't work every time... (to simplify things I've use 1 second delay for turn on and turn off).

#include <LiquidCrystal.h>
#include <StopWatch.h>
#include <plcLib.h>


LiquidCrystal lcd(7, 6, 5, 4, 3, 2);


StopWatch sw_secs(StopWatch::SECONDS);



int VPin = 12;
int MPin = 8;
int SPin = 9;
int DPG = 10;
int DPD = 11;
int GPin = A2;
int Tipke;


 unsigned long TIMER0 = 0;


void setup() {
  //Welcome message

  lcd.begin(16, 2);
  lcd.setCursor(4, 0);
  lcd.print("SOMETHING");
  delay(1000);
  lcd.setCursor(5, 1);
  lcd.print("SOMETHING");
  delay(2000);

  //My way to blink displayed text
  lcd.clear();
  delay(500);

  lcd.setCursor(5, 1);
  lcd.print("SOMETHING");
  lcd.setCursor(4, 0);
  lcd.print("SOMETHING");
  delay(500);
  lcd.clear();
  delay(500);

  lcd.setCursor(5, 1);
  lcd.print("SOMETHING");
  lcd.setCursor(4, 0);
  lcd.print("SOMETHING");
  delay(500);


  
  pinMode(VPin, OUTPUT);
  pinMode(MPin, OUTPUT);
  pinMode(SPin, OUTPUT);
  pinMode(DPG, OUTPUT);
  pinMode(DPD, OUTPUT);
  pinMode(GPin, INPUT);
  lcd.clear();

  sw_secs.start();

  setupPLC();

}




void loop()
{
  
  static unsigned long Time;
  
  
  
  
  if (digitalRead(GPin) == LOW) {

   

    if (millis() - Time >= 1000)
    {
      
      Time += 1000;
      Consumption();
    }
    digitalWrite(VPin, LOW);
    digitalWrite(MPin, LOW);

in(GPin); //Read this input
timerOff(TIMER0, 1000); //1 second turn off delay
out(SPin);
      
}
    
     
  
else  {
  
    if (millis() - Time >= 1000)
    {
      Time += 1000;
      Working(); 
    } 
    digitalWrite(VPin, HIGH);
    digitalWrite(MPin, HIGH);
  
in(GPin);
timerOn(TIMER0, 1000);
out(SPin);
      

   
}

  
  Buttons();
  Vrijeme();
  
  }




void Consumption() {
  lcd.clear();
  lcd.setCursor(3, 0);
  lcd.print("STATUS: ON");
  lcd.setCursor(0, 1);
  lcd.print("Consupt : ");
  lcd.setCursor(12, 1);
  lcd.print("kg/h");
  int Potenciometar = A1;
  int Pot1 = 0;
  int Pot2 = 0;
  Pot1 = analogRead(A1) / 10;
  Pot2 = Pot1 / 1.023;
  lcd.setCursor(9, 1);
  lcd.print(Pot2);
}



void Vrijeme() {

}


void Working() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("STATUS: WORKING");
  lcd.setCursor(0, 1);
  lcd.print("Consupt: ");
  lcd.setCursor(12, 1);
  lcd.print("kg/h");
  int Potenciometar = A1;
  int Pot1 = 0;
  int Pot2 = 0;
  Pot1 = analogRead(A1) / 10;
  Pot2 = Pot1 / 1.023;
  lcd.setCursor(9, 1);
  lcd.print(Pot2);
}


void Buttons()
{
  
  Tipke = analogRead(A0);
  if (Tipke < 50) {
    lcd.setCursor(0, 0);
    lcd.print("          UP     ");
    digitalWrite(DPG, HIGH);
  }

  else if (Tipke < 250) {
    lcd.setCursor(0, 0);
    lcd.print("        DOWN   ");
    digitalWrite(DPD, HIGH);
  }

  else if (Tipke < 450) {
    lcd.print("");
    lcd.setCursor(0, 0);
    lcd.print("UP Time:         ");

    int h, m, s;
    s = sw_secs.elapsed();
    m = s / 60;
    h = s / 3600;
    s = s - m * 60;
    m = m - h * 60;

    lcd.setCursor(0, 1);
    lcd.print(h);
    lcd.setCursor(1, 1);
    lcd.print(" hrs                  ");
    lcd.setCursor(6, 1);
    lcd.print(m);
    lcd.setCursor(8, 1);
    lcd.print("min");
    lcd.setCursor(9, 1);

  }
  else {
    digitalWrite(DPG, LOW);
    digitalWrite(DPD, LOW);
  }
}

Thing with plcLib.h doesn't work because it sets inputs and outputs automatically, if I figured it out well...

So back to what PaulS suggested. Any example of something similar to what I need? Or few more hints? Because I just don't get it...