Make timeout for led

Hey guys i,m new on this forum i have one question i making some project for my car i want to make Domelight Supervision and i don't know how to make timeout with millis() can anyone help me ?

here is my code

#include <Adafruit_NeoPixel.h>

int LED_PIN = 7;
int odklep_pin = 8;
int zaklep_pin = 9;
int totalLEDs = 9;
int ledFadeinTime = 2;
int ledFadeoutTime = 5;
int ignition_pin = 6;
int leds = false;
int kontakt = false;
unsigned long led_timeout;
unsigned long previousMillis = 0;
long OnTime = 25000;           // milliseconds of on-time //25000=25s


Adafruit_NeoPixel strip = Adafruit_NeoPixel(totalLEDs, LED_PIN, NEO_GRB + NEO_KHZ800);

void setup()
{
  strip.begin();
  Serial.begin(9600);
  strip.show(); // Initialize all pixels to 'off'
}

void loop()
{ 
  if (digitalRead(8) == HIGH && leds==false && kontakt == false) //Avto Odklenjen
  {
         currentTime = millis();
         if (currentTime<25000)
         {
           Serial.println("Avto odklenjen, Luci so prizgane");
           rgbFadeIn(255, 255, 255, ledFadeinTime); // Bela
         }
          if (currentTime>25000)
          {
            Serial.println("Avto timeout");
            rgbFadeout(255, 255, 255, ledFadeoutTime); // Bela
          }
  }
  
  if (digitalRead(9) == HIGH && leds==true) //Avto Zaklenjen
  {
       Serial.println("Avto zaklenjen luci so ugasjene");
      rgbFadeout(255, 255, 255, ledFadeoutTime); // Bela
  }

  if(digitalRead(6) == HIGH && leds == true && kontakt==false) //Kontakt
  {
      Serial.println("Kontankt je dan in luci so ugasnjene");
      rgbFadeout(255, 255, 255, ledFadeoutTime);
      kontakt == true;
  }
  if(digitalRead(6) == LOW && leds == false && kontakt==true) // Konatkt off
  {
      Serial.println("Kontakt je odvzet luci so ugasnjene");
      rgbFadeIn(255, 255, 255, ledFadeinTime);
      kontakt == false;
  }

/*
  if(leds && !kontakt && (currentMillis - previousMillis >= OnTime)) //Timeout
  {
    Serial.println("Luci so ugasnjene, predolgo ni bilo odziva :)");
    rgbFadeout(255, 255, 255, ledFadeoutTime);
    previousMillis = currentMillis;
  }*/
}

void rgbFadeIn(uint8_t red, uint8_t green, uint8_t blue, uint8_t wait)
{
    for(uint8_t b = 0; b <255; b++)
    {
         for(uint8_t i=0; i < strip.numPixels(); i++)
         {
            strip.setPixelColor(i, red * b/255, green * b/255, blue * b/255);
         }
       leds=true;
       delay(wait);
       strip.show();
    }
}
void rgbFadeout(uint8_t red, uint8_t green, uint8_t blue, uint8_t wait)
{
    for(uint8_t b=255; b > 0; b--)
    {
       for(uint8_t i = 0; i < strip.numPixels(); i++)
       {
          strip.setPixelColor(i, red * b/255, green * b/255, blue * b/255);
       }
       leds=false;
       delay(wait);
       strip.show();
    }
     strip.clear();
     strip.show();
}
  currentTime = millis();
         if (currentTime<25000)

This will only ever be true in the 25 seconds since the last reset of the processor.

I think you need to record the start time of your timeout in an unsigned long variable, and subtract that value from the current value returned by millis(), and compare that difference to 25000

in there i need a while() or someting ?

 if (digitalRead(8) == HIGH && leds==false && kontakt == false) //Avto Odklenjen
  {
         currentTime = millis();
         if (currentTime<25000)
         {
           Serial.println("Avto odklenjen, Luci so prizgane");
           rgbFadeIn(255, 255, 255, ledFadeinTime); // Bela
         }
          if (currentTime>25000)
          {
            Serial.println("Avto timeout");
            rgbFadeout(255, 255, 255, ledFadeoutTime); // Bela
          }
  }

Did you take a look at Blink without delay? Notice how millis() is used there.

The demo Several Things at a Time is an extended example of BWoD and 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

ammmm i read this but i don't understand where to put this millis command :confused: and do i must create the loop to count millis or what can anyone correct the code and show me where to put this in ?

bizjak25:
ammmm i read this but i don't understand where to put this millis command :confused: and do i must create the loop to count millis or what can anyone correct the code and show me where to put this in ?

If what you want is to make the LED turn off after a certain time then save the value of millis() when the LED goes on and then turn it off with some code like this

if(millis() - ledStartMillis >= onPeriodMillis) {
   // code to turn LED off
}

In a simple program that code can just go into the loop() function.

...R