millis not working.....

Hi forum,
I need some help with this sketch:
it's a simple one, nothing fancy:
a digital input that activates a PWM output for a constant time (delay), and then ramps the PWM down to 0.
I'm struggling to get the delay working.

First, I wrote the sketch using millis, but when I test the circuit it does not have any delay, in other words the PWM is activated and then it starts to ramp down right away.

I've replaced the (millis) with a (delay) and it works, but I would like to go back to millis, and I can't figure out what's wrong.

Could you guys have a look at it and let me know?
thanks

int SENSR_SX = 5; //
int OUT_SX = 10; //
int SP_LDR = A0; //
int LDR = A2;    //

int K_PWM_SX = 255;  //
int light = 0;     //
int light_SP = 0;  // (
bool light_OK = LOW; //
bool PIR_SX = LOW;   //

unsigned long K_timer = 10000;  //
unsigned long i = 0; //
unsigned long startMillis;  //
unsigned long currentMillis_SX; //


void setup()
{
  pinMode(SENSR_SX, INPUT);//
  pinMode(OUT_SX, OUTPUT);
  startMillis = millis();  //
}


void loop()
{
  light = analogRead(LDR);       //
  light_SP = analogRead(SP_LDR); //

  if (light > light_SP) //
  {
    (light_OK = LOW); //
  }

  else if (light < light_SP) //
  {
    (light_OK = HIGH); //
  }

  PIR_SX = digitalRead(SENSR_SX);//

  if (PIR_SX == HIGH && light_OK == HIGH) //
  {
    analogWrite(OUT_SX, K_PWM_SX); //

    // delay(K_timer);// REMOVED THE NEXT 2 LINES AND INSERTED THIS ONE,IT WORKS

    currentMillis_SX = millis(); //
    if (currentMillis_SX - startMillis >= K_timer)  //

      for (int i = K_PWM_SX; i > 0; i--) //

      {
        analogWrite(OUT_SX, i); //

      }

    analogWrite(OUT_SX, 0); //

  }
}

You never update ‘startMillis’

You never update 'startMillis'

does it mean that it shoud be outside the curly brackets?

This is sloppy.

    if (currentMillis_SX - startMillis >= K_timer)  //

      for (int i = K_PWM_SX; i > 0; i--) //

      {
        analogWrite(OUT_SX, i); //

      }

Use:

    if (currentMillis_SX - startMillis >= K_timer)  //
    {
      startMillis = currentMillis_SX;
       
      for (int i = K_PWM_SX; i > 0; i--) //
       {
         analogWrite(OUT_SX, i); //
       }
    }

Larry,
thanks for the reply.

Larry,
I've been trying to understand the change you've made, but I'm struggling.

Could you please explain it to me?

thanks

    if (currentMillis_SX - startMillis >= K_timer)  //
    { //the start of the code to be executed

      // this ‘restarts’ the timing
      startMillis = currentMillis_SX;
       
      for (int i = K_PWM_SX; i > 0; i--) //
       {
         analogWrite(OUT_SX, i); //
       }

    } //the end of the code you wanted to execute.

all clear now,

thanks again !! :slight_smile:

Larry,
I've modified the code as suggested, but I'm still getting the same results:
the PWM is activated and the fading starts right away, no 10 seconds delay..

I've modified the code as suggested,

Then please post it here

here we go

int SENSR_SX = 5; //
int OUT_SX = 10; //
int SP_LDR = A0; //
int LDR = A2;    //

int K_PWM_SX = 255;  //
int light = 0;     //
int light_SP = 0;  //
bool light_OK = LOW; //
bool PIR_SX = LOW;   //

unsigned long K_timer = 2000;  //
unsigned long i = 0; //

unsigned long startMillis;  //
unsigned long currentMillis_SX; //
unsigned long currentMillis_DX; //

void setup()
{
  pinMode(SENSR_SX, INPUT);//
  pinMode(OUT_SX, OUTPUT);

  startMillis = millis();  //

  Serial.begin(1200); //
}

void loop()
{

  light = analogRead(LDR);       //
  light_SP = analogRead(SP_LDR); //

  if (light > light_SP) //
  {
    (light_OK = LOW); //
  }

  else if (light < light_SP) //
  {
    (light_OK = HIGH); //
  }


  PIR_SX = digitalRead(SENSR_SX);//

  if (PIR_SX == HIGH && light_OK == HIGH) //
  {
    analogWrite(OUT_SX, K_PWM_SX); //

    currentMillis_SX = millis(); //

    if (currentMillis_SX - startMillis >= K_timer)  //

    {
      startMillis = currentMillis_SX;//

      for (int i = K_PWM_SX; i > 0; i--) //
      {
        analogWrite(OUT_SX, i); //

        delay(50);  //
      }
    } //
    analogWrite(OUT_SX, 0); //
  }

  Serial.print("light(LDR)=");  //
  Serial.print(light);  //
  Serial.print("  ");  //
  Serial.print("- light_SP(TRIMMER)=");  //
  Serial.print(light_SP);  //
  Serial.print("  ");  //
  Serial.print("- light_OK=");  //
  Serial.print(light_OK);  //
  Serial.print("  ");  //
  Serial.println(); //  (START NEW LINE)

  Serial.print("PIR_SX=");  //
  Serial.print(PIR_SX);  //
  Serial.print("  ");  //
  //Serial.print("- currentMillis_SX=");  //
  //Serial.print(currentMillis_SX);  //
  Serial.print("  ");  //
  Serial.print("- OUT_SX(PWM)=");  //
  Serial.print(OUT_SX);  //
  Serial.print("  ");  //
  Serial.println(); // (START NEW LINE)
  Serial.println(); //  (START NEW LINE)
  Serial.println(); //   (START NEW LINE)//
}

You are not picking up the startTime correctly. Try using a boolean control variable to help set the startTime for the delay when the start condition becomes true. Your fade loop timing is longer than the start delay, and with what you are currently doing to set the start time the millis() - startTime is immediately greater than the K_time.

int SENSR_SX = 5; //
int OUT_SX = 10; //
int SP_LDR = A0; //
int LDR = A2;    //

int K_PWM_SX = 255;  //
int light = 0;     //
int light_SP = 0;  //
bool light_OK = LOW; //
bool PIR_SX = LOW;   //

unsigned long K_timer = 2000;  //
unsigned long i = 0; //

unsigned long startMillis;  //
unsigned long currentMillis_SX; //
unsigned long currentMillis_DX; //

boolean timing = false;

void setup()
{
  pinMode(SENSR_SX, INPUT);//
  pinMode(OUT_SX, OUTPUT);

  startMillis = millis();  //

  Serial.begin(1200); //
}

void loop()
{

  light = analogRead(LDR);       //
  light_SP = analogRead(SP_LDR); //

  if (light > light_SP) //
  {
    (light_OK = LOW); //
  }

  else if (light < light_SP) //
  {
    (light_OK = HIGH); //
  }


  PIR_SX = digitalRead(SENSR_SX);//

  if (PIR_SX == HIGH && light_OK == HIGH && timing==false) //
  {
    analogWrite(OUT_SX, K_PWM_SX); //
    startMillis = millis();
    //currentMillis_SX = millis(); //
    timing = true;
  }
  if (timing == true)
  {
    if (millis() - startMillis >= K_timer)  //
    {
      //startMillis = currentMillis_SX;//
      for (int i = K_PWM_SX; i > 0; i--) //
      {
        analogWrite(OUT_SX, i); //

        delay(50);  //
      }
     timing = false;
    } //
    analogWrite(OUT_SX, 0); //
  }

  Serial.print("light(LDR)=");  //
  Serial.print(light);  //
  Serial.print("  ");  //
  Serial.print("- light_SP(TRIMMER)=");  //
  Serial.print(light_SP);  //
  Serial.print("  ");  //
  Serial.print("- light_OK=");  //
  Serial.print(light_OK);  //
  Serial.print("  ");  //
  Serial.println(); //  (START NEW LINE)

  Serial.print("PIR_SX=");  //
  Serial.print(PIR_SX);  //
  Serial.print("  ");  //
  //Serial.print("- currentMillis_SX=");  //
  //Serial.print(currentMillis_SX);  //
  Serial.print("  ");  //
  Serial.print("- OUT_SX(PWM)=");  //
  Serial.print(OUT_SX);  //
  Serial.print("  ");  //
  Serial.println(); // (START NEW LINE)
  Serial.println(); //  (START NEW LINE)
  Serial.println(); //   (START NEW LINE)//
}

Thank you guys for the help.