How to interrupt a function?

Hello everyone,

I use the following code:

else if ((etatStart == 1) && (etatAu == 0) && (etatTempoout == 0) && (EV == 0) && (AlimOn == 0))
  {
    digitalWrite(TempoPout, HIGH);
    delay(10000);
    digitalWrite(TempoPout, LOW);
    delay(1000);
    digitalWrite(StartAlimPout, HIGH);
    AlimOn = 1;
    EV = 0;
  }

with this in my setup

attachInterrupt(digitalPinToInterrupt(2), blink1, RISING);

and finally the blink1 function

void blink1() {
  digitalWrite(StartAlimPout, LOW);
  digitalWrite(TempoPout, LOW);
  EV = 1;
}

So when I use the start button the fisrt condition is applied
When I use the AU button, the interrupt is applied but the first condition seems not canceled.

I mean, when I cancel the AU button, I see I still inside the fist condition. So the Tempoout is still HIGH or the StartalimPout is HIGH whereas I would like to cancel everythings (including stop the delay(10000) when I use the AU button).

How I can do ?

Don't post snippets (Snippets R Us!)

assuming EV is a byte declared as volatile, as you reset it to 0 at the end of the if

even if the interrupt had set it to 1 you won't see it next time you evaluate the if

also delays will make your code unresponsive

➜ s look at Using millis() for timing. A beginners guide and Several things at the same time

Thanks for your help.

How I should declare EV ?

Sorry for the snippets, here is the whole code


int ThermistorPin = A0;
int smokePin1 = A1;
int smokePin2 = A2;

const int BoutonStart = 3;
const int EVPout = 10;
const int TempoPout = 8;
const int StartAlimPout = 6;

int etatStart = 0;
int etatAu = 0;
int etatStartout = 0;
int etatTempoout = 0;
int etatEV = 0;

int AlimOn = 0;
int EV = 0;


double adcMax, Vs;

double R1 = 10000.0;   // voltage divider resistor value
double Beta = 3950.0;  // Beta value
double To = 298.15;    // Temperature in Kelvin for 25 degree Celsius
double Ro = 10000.0;   // Resistance of Thermistor at 25 degree Celsius

void setup() {
  Serial.begin(115200);
  delay(2000);



  attachInterrupt(digitalPinToInterrupt(2), blink1, RISING);
  attachInterrupt(digitalPinToInterrupt(3), blink2, LOW);



  adcMax = 1023.0; // ADC resolution 10-bit (0-1023)
  Vs = 5.0;          // supply voltage


  pinMode(EVPout, OUTPUT);
  digitalWrite(EVPout, LOW);
  pinMode(TempoPout, OUTPUT);
  digitalWrite(TempoPout, LOW);
  pinMode(StartAlimPout, OUTPUT);
  digitalWrite(StartAlimPout, LOW);
  pinMode(BoutonStart, INPUT);
}

void blink1() {
  digitalWrite(StartAlimPout, LOW);
  digitalWrite(TempoPout, LOW);
  EV = 1;
}

void blink2() {
  digitalWrite(StartAlimPout, LOW);
  digitalWrite(TempoPout, LOW);
  digitalWrite(EVPout, LOW);
  EV = 0;
  AlimOn = 0;
}



void loop() {
  double Vout, Rt = 0;
  double T, Tc, Tf = 0;

  double adc = 0;
  double adc2 = 0;

  etatStart = digitalRead(3);
  etatAu = digitalRead(2);
  etatStartout = digitalRead(StartAlimPout);
  etatTempoout = digitalRead(TempoPout);
  etatEV = digitalRead(EVPout);

  adc = analogRead(ThermistorPin);
  adc2 = analogRead(smokePin1);

  Vout = adc * Vs / adcMax;
  Rt = R1 * Vout / (Vs - Vout);

  T = 1 / (1 / To + log(Rt / Ro) / Beta); // Temperature in Kelvin
  Tc = T - 273.15;                   // Celsius
  Tf = Tc * 9 / 5 + 32; // Fahrenheit
 

  if ((etatStart == 0) && (etatAu == 0)) {
    digitalWrite(EVPout, LOW);
    EV = 0;
    AlimOn = 0;
  }

  else if ((etatStart == 1) && (etatAu == 0) && (etatTempoout == 0) && (EV == 0) && (AlimOn == 0))
  {
    digitalWrite(TempoPout, HIGH);
    delay(10000);
    digitalWrite(TempoPout, LOW);
    delay(1000);
    digitalWrite(StartAlimPout, HIGH);
    AlimOn = 1;
    EV = 0;
  }

  else if ( EV == 1 || Tc > 100 || adc2 > 1000 ) {
    digitalWrite(StartAlimPout, LOW);
    digitalWrite(TempoPout, LOW);
    digitalWrite(EVPout, HIGH);
    EV = 1;
  }
  else {
    digitalWrite(EVPout, LOW);
    EV = 0;
    AlimOn = 1;
  }

  delay(500);
}

Why do you will use an interrupt function?

I use interrupt to be sure my AU button function (blink1) takes priority over everything else.
Unfortunately some part are not canceled as I explain in 1st message.

Hello yanou12

The expected realtime behaivour of the sketch is blocked by the ussage of the delay() functions.

Line  29:   delay(2000);
	Line 100:     delay(10000);
	Line 102:     delay(1000);
	Line 120:   delay(500);

I try to use millis instead of delay but I did not succeed

volatile byte EV = 0 ;

You don't need an int a byte will do, and you were told before that any variable used inside and outside an ISR should be volatile.

However I am totally unconvinced that you need an interrupt in the first place. Your argument for it is not convincing.

it would be better to focus your efforts on learning how to use millis

Thanks a lot again for your help,

So, how I should do without interrupt?

My need is:

if the Start button is HIGH (and I'm not in one of previous case), TempoPout has to be HIGH during 10s then StartAlimPout has to be HIGH.

if the AU button is HIGH, or the TC > 100 or adc2 > 1000, all pintout have to be LOW expect EVPout has to be HIGH.

In order to reset everything (and put EVPout to LOW) the button start has to be low

learn about state machine. (there is a tutorial in the French section)

See this page about state machines.

http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.