Hello,
i have an ISR that sets a flag=true on a button press and returns control to the program, there's a IF test for the flag that disables interrupts, sets some outputs high, then waits for 10s sets low, clears the flag, reenables interrupts and continues main loop..
problem is that the delay is not working, i activate the interrupt, i get the serial output and instantly the program continues, delay completely ignored...
Whys is this happening?, i'm not inside the ISR so delay should be working, i tried some ridiculous value for delay like 10000 and more and still gets ignored (outputs cycle high and a fraction of a sec later go low).
here's the entire code for completeness:
pines:
Pin 2 - input: Reset (interrupt 0)
Pin 3 - input: SLO
Pin 4 - output LED: Cargando
Pin 5 - output: Rele
v8 8/2
*/
// Constantes globales y definicion pines
const byte SLO = 3;
const byte ledcharge = 4;
const byte rele = 5;
const byte reset = 2;
const long tiempo_en_prueba = 300000; //15 min tiempo en prueba 900Kmsec, usar 300K para 5 min
const int tiempo_fraccion = tiempo_en_prueba/1000; //Para fraccionar los delays largos en el ejercicio
const unsigned long tiempo_para_ejercitar = 600000; //20min hasta testear, en realidad tiene que ser 1209600000(2 semanas)
unsigned long fin_retardo = 0; //inicializo fin delay
boolean contando = false; //inicializo sin contar
volatile boolean reset_flag = false; //no esta reseteado
byte slo_state; //inicializo variable para el SLO
void setup(){
pinMode(SLO, INPUT);
pinMode(rele, OUTPUT);
pinMode(ledcharge, OUTPUT);
Serial.begin(9600);
attachInterrupt(0, reset_event, CHANGE);
}
void loop(){
posicion_inicio:
if (reset_flag == true){
noInterrupts();
fin_retardo = 0; //inicializo vars
contando = false;
digitalWrite (rele, HIGH); //activar rele
digitalWrite (13, HIGH); // debug
digitalWrite(ledcharge,LOW);
Serial.println("Reset!!!!!!"); //debug
delay(4000);
digitalWrite (rele, LOW); //desactivar rele
digitalWrite (13, LOW); //debug
reset_flag = false; //termino reset
interrupts();
}
slo_state = digitalRead (SLO); //leer pin SLO, recordar que esta negado
if ((slo_state == LOW) && (contando == false)){ //esta en float, no estoy testeando carga y aún no estoy contando para testear
unsigned long currenttime = millis();
fin_retardo = currenttime + tiempo_para_ejercitar; // fin retardo, tiempo actual+diff tiempo hasta ejercitar
Serial.print("el fin del delay es: "); //debug
Serial.println(fin_retardo); //debug
Serial.println(currenttime); //debug
contando = true; //Empieza el conteo regresivo a la ejercitacion
}
else if (slo_state == HIGH && contando == true){
contando = false; //Si mientras cuento se va de flote, abortar cuenta
}
if (slo_state == HIGH) { // si no esta a flote, encender led de carga
digitalWrite(ledcharge,HIGH);
}
else digitalWrite(ledcharge,LOW); // si esta a flote, apagarlo
Serial.print("la diferencia actual es: "); //debug
Serial.println((long)( millis() - fin_retardo )); //debug
if (contando == true) Serial.println("contando"); //debug
delay(1000); //debug
if ((contando == true) && ((long)( millis() - fin_retardo ) >= 0)){ //Si termino la cuenta regresiva empezar a ejercitar
contando = false; //termino la cuenta regresiva
for(byte l = 0; l < 4; l++){ // 4 loops de ejercitacion(0 a 3)
if (reset_flag == true) goto posicion_inicio; //abortar si reset
digitalWrite (rele, HIGH); //activar rele
digitalWrite (13, HIGH); // debug
Serial.print("Testeando! loop: "); //debug
Serial.println(l,DEC); //debug
//comienzo delay sintetizado
for (int i = 0; i < 1000; i++) {
delay (tiempo_fraccion);
if (reset_flag == true) goto posicion_inicio; //abortar si reset
} //fin delay sintetizado
digitalWrite (rele, LOW); //desactivar rele
digitalWrite (13, LOW); //debug
Serial.println("Descansando!"); //debug
//comienzo delay sintetizado half
for (int i = 0; i < 500; i++) {
delay (tiempo_fraccion);
if (reset_flag == true) goto posicion_inicio; //abortar si reset
} //fin delay sintetizado
} //fin loop ejercitacion
}
}
void reset_event(){
reset_flag = true; //set reset
}