Need help to run function before timerInterval

Hello i need help to my project,
i want to run motor with relay and stop it with limit switch.

the problem is i need to stop the relay synchronous when limit switch touched, but there is a delay like 1 second when the limit switch touched.( im using timer.setInterval(1000))

can help me with the delay or something, so when the limit switch touched the relay can off under 1sec? or can i run other function between timer set interval? i have tried to give delay on function i want to run between but it do nothing

Difficult to say without knowing more, but it sounds like you should be using an interrupt attached to your limit switch pin.

Pete.

can i do timer.setInterval under 1000?

Yes, but it depends what you’re doing in your timed function, and that’s something you’ve not shared with us.

Pete.

this is my code, i have been tried to do runRelay under 1 second so the motor can be stop on right position, there is a delay when limit swith touched

void setup(){
timer.setInterval(500L, runRelay);
timer.setInterval(1000L,sendSensor);
}

void loop(){
timer.run();
}

void runRelay(){
  kanopi();
  checkAutomation();
}
========================================
//this is limit switch function
void kanopi() {
  openK = digitalRead(kanopiOpen_pin);
  closedK = digitalRead(kanopiClose_Pin);
  delay(250);
  // Serial.println((String)"Limit Buka = "+openK);
  // Serial.println((String)"Limit Tutup = "+closedK);

  if (openK == 1 && closedK == 0) {
    checkKanopi("kanopi_tertutup", 1);
  } else if (openK == 0 && closedK == 1) {
    checkKanopi("kanopi_terbuka", 0);
  } else if (openK == 1 && closedK == 1) {
    if(staterelayOffManual == 1){
     checkKanopi("kanopi_berhenti", 4);
    }else{checkKanopi("bergerak", 2);}
  } else{
    checkKanopi("kanopi_error", 3);
  }
}

void checkKanopi(const String& event, int data_log){
  // Serial.println("---------------------------------------------");
  // Serial.println("Kanopi " + event);
  log_kanopi = data_log;
  Blynk.virtualWrite(V4, data_log);
  Blynk.logEvent(event);
}
====================================
BLYNK_WRITE(V12){
  pinvalueOtomatis = param.asInt();
}

void checkAutomation(){
  if(pinvalueOtomatis == 1){
    controlrelayAuto();
    Blynk.setProperty(V11, "isHidden", true);
    staterelayOffManual = 0;
  }else{
   Blynk.setProperty(V11, "isHidden", false);
   kanopiManual();
  }
}
====================================
void relay() {
  Serial.println("Logika Motor : " + String(log_motor) + " Logika Kanopi : " + String(log_kanopi));
}

void operateRelayBlynk(const String& statusRelay, int setpinOpen, int setpinClose, int blynkValue, int virtualPin) {
  digitalWrite(relayOpen_pin, setpinOpen);
  digitalWrite(relayClose_pin, setpinClose);
  Blynk.virtualWrite(virtualPin, blynkValue);
  // Serial.println("Kanopi" + statusRelay);
  // Serial.println("Jalan Auto");
}

void controlrelayAuto(){
  // logic_kanopi();
    if (log_motor == 0 && log_kanopi == 1) {//Opening
    operateRelayBlynk("Bergerak Membuka",HIGH, LOW, 1, V2);
    } else if (log_motor == 1 && log_kanopi == 0) {//Closing
    operateRelayBlynk("Bergerak Menutup",LOW, HIGH, 1, V2);
    }  else if (log_motor == 0 && log_kanopi == 0) {//Openend
    operateRelayBlynk("Sudah Terbuka",LOW, LOW, 0, V2);
    } else if (log_motor == 1 && log_kanopi == 1) {//Closed
    operateRelayBlynk("Sudah Tertutup",LOW, LOW, 0, V2);
    }
}

void controlrelayOpen(int kanopi){
  if (kanopi == 1) {
    operateRelayBlynk("Bergerak Membuka",HIGH, LOW, 1, V2);
  }else if (kanopi == 0) {
    operateRelayBlynk("Sudah Terbuka", LOW, LOW, 0, V2);
  }else if (log_kanopi == 2){
    operateRelayBlynk("Bergerak Membuka",HIGH, LOW, 1, V2);
  }
  // Serial.println("Jalan Manual");
}

void controlrelayClose(int kanopi){
  if (kanopi == 1) {
    operateRelayBlynk("Sudah Tertutup", LOW, LOW, 0, V2);
  }else if (kanopi == 0) {
    operateRelayBlynk("Bergerak Menutup", LOW, HIGH, 1, V2);
  }else if (log_kanopi == 2){
    operateRelayBlynk("Bergerak Menutup",LOW, HIGH, 1, V2);
  }
  // Serial.println("Jalan Manual");
}

void controlrelayOff(){
  operateRelayBlynk("Berhenti ",LOW, LOW, 0, V2);
}

====================================

It would be better if you posted your entire sketch, but you can’t do this…

and your code is very inefficient in the way it’s written.

You’d be better using interrupts.

Pete.