I am using a function outside loop(), which is called after an interrupt. Inside this function, I am setting several relays that need to change after a second or two, depending on my setting. I tried the delay, it doesn't work. millis() isn't working as well; I found out it stops counting when I exit the loop function.
My code looks something like this:
const long on_interval = 2000;
unsigned long currentMillis = millis();
bool hv_bool = false;
void hv_switch() {
if (digitalRead(hv_pin)==HIGH){
hv_startup();
}
}
void hv_startup() {
unsigned long previousMillis = 0;
digitalWrite(pre_charge, HIGH);
digitalWrite(main_charge, LOW);
//Wait interval
previousMillis = currentMillis;
while (currentMillis - previousMillis < on_interval) {
Serial.println(millis());
//Wait
}
digitalWrite(pre_charge, LOW);
digitalWrite(main_charge, HIGH);
hv_bool = true;
loop();
}
void setup(){
//Set some ports
//Initiate interrupt
attachInterrupt(digitalPinToInterrupt(hv_pin), hv_switch, RISING);
}
void loop() {
//Low voltage loop after startup, high voltage off
while (hv_bool == false) {
Serial.println("Waiting on high voltage");
digitalWrite(hv_led, LOW);
digitalWrite(pre_charge, LOW);
digitalWrite(main_charge, LOW);
}
}
millis()works fine in any function, except in interrupt context, where it doesn't increment.