Using millis for time delay for (push ON/OFF button)

kiriti:
yea crimony, but the millis button will overflow back to 0 after 50 days, that time the "timep-time" would go negative and the condition without abs "(timep-time) <= timedelay" would be satisfied for 50 more days :stuck_out_tongue:

thats why i included the abs function, or is it not necessary or am i overlooking something?

Your code uses time in seconds, so you can't use the standard transparent technique for managing millis() rollover, which would be something like this (not tested, likely to contain some errors):

unsigned long timedelay;
unsigned long time;
unsigned long timep;
bool ac_on = false;

void loop()
{
  val = digitalRead(sensor);                                //pir sensor
  potval = analogRead(pot);                                //pot value for time delay
  timedelay = 1000 * map(potval, 0, 1023, 20, 600); // delay in milliseconds

  if(val == 1)                                                     // if pir sensor triggered
  {
    digitalWrite(led, HIGH);
    if (!ac_on)                                              // only toggle the AC if it's not on yet.
    {
      ac();
      ac_on = true;
    }
    time = millis();
  }
  timep = millis();
  if( ac_on && timep - time > timedelay)                   // time delay exceeded
  {
    digitalWrite(led, LOW);
    ac();
    ac_on = false;
  }
  
  Serial.println(timedelay);
  delay(100);
}

This code assumes the AC starts OFF. If it's not, then the logic will be reversed. You really should have a mechanism for detecting whether the AC is on or off, it would make your solution much more robust.