0

It was so easy to my mind but it is really not. I have a float switch which goes high when water is floating. So I connect the switch to INT0 pin of MCU. So when water is not floating everything is OK. When the water is floating I send data. I declare the interrupt as follows:

attachInterrupt (digitalPinToInterrupt (switch), float, RISING);

Where float is

void float()
{
detachInterrupt(0);
digitalWrite (LED, HIGH);    
txReturn = myRN.txCnf("1");
}  

And my main loop is

attachInterrupt (digitalPinToInterrupt (BUTTON), switchPressed, RISING);
digitalWrite (LED, LOW);
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);

I can't understand why my led is not going LOW and why I interrupt is useless after the first successful time. I am aware that I must keep interrupts routines short and by txReturn.txCnf("1"); I don't but I can not understand where my logic is wrong.

  • txCnf might itself depend on interrupts. So your code will hang. Set a Boolean flag in the interrupt routine. The in the loop, if the flag is set, send the data and clear the interrupt. – Gerben Jun 7 '18 at 17:00
  • float switch which goes high .... what do you mean by goes high? .... switches usually open or close – jsotola Jun 7 '18 at 18:54
2

float is already defined for floating point number, so you cannot use it for your function that detects when the sensor is floating on water. Try calling it something else.

Also, instead of detaching and reattaching the interrupt, use a state machine. When the interrupt is triggered, you will be able to run different code in the main loop.

unsigned int state;
void setup(){
  state=1;
  attachInterrupt(0,changestate,RISING);
}
void loop(){

switch (state) {
case 1: // do something
// add code here
break;

case 2: // do something else
// add code here
break;
}
}

void changestate(){
if(state==1){state=2;} // interrupt changes state
}
  • Okay your approach makes more sense and seems to be more efficient. I' ll give it a try. – alexisicte Jun 11 '18 at 7:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.