How to override interrupt to receive RX

I am using a Mega with a Duinotech Lora Module.
The programme is designed to turn on a 240V water pump and send back the progressive flow of liquids to a Master unit

The interrupt in Pin 4 monitoring the water flow works just fine and the info is transmitted back to a Master unit.

My problems arise when I send a command to turn off the pump.

It seems that I cannot get the programme to acknowledge the "Turn OFF" command as long as the data is being received via the interrupt from the Water Flow Sensor.

How can I break the cycle to acknowledge receipt of the "Turn Off " command.


int sensorPin = 3;
volatile long pulse;
int long Volume = 0;
int long TotalVolumeML = 0;
int long TotalVolumeLITRES = 0;
int long aaa = 0;
//...................................   WATER FLOW METER   ............
int long sendbacK = 0;
int FlowRate = 0;
int long OldTotalVolumeML = 0;
//..................................................................   RX  RECEIVER  ..........................................
#include <SPI.h>
#include <LoRa.h>

byte localAddress = 0xBB;
byte destinationAddress = 0xAA;
//.................................................................   VARIABLES   ............................................RX  RECEIVER



int reC  = 0;
int statioN  = 0;
int onofF    = 0;
int incom = reC;


int staToNOFF = 0; //  Even number is OFF   odd number is ON
int relay = 0;

void setup() {
  Serial.begin(9600);
  pinMode(sensorPin, INPUT);
  //.........................................................................   WATER FLOW SENSOR   .......................
  attachInterrupt(digitalPinToInterrupt(sensorPin), increase, RISING);

  //.................................................................   RELAYS AND LED'S   ............................................RX  RECEIVER

  pinMode(6, OUTPUT); digitalWrite(6, LOW); //   Relay  HIGH is ON    LOW  is OFF

  //................................................................   STARTING LORA   ....................................RX  RECEIVER
  Serial.println("Start LoRa duplex RX Receiver");
  if (!LoRa.begin(915E6)) {
    Serial.println("LoRa init failed. Check your connections.");
    while (true) {}
  }
}
//...................................................................   VOID OUTGOING DATA   .....................................RX  RECEIVER
void sendMessage(String outgoing) {
  LoRa.beginPacket();
  LoRa.write(destinationAddress);
  LoRa.write(localAddress);
  LoRa.write(outgoing.length());
  LoRa.print(outgoing);
  LoRa.endPacket();
  digitalWrite(4, HIGH); delay(1000); digitalWrite(4, LOW); //    .......... GREEN LED
  Serial.print("                           sending back   " ); Serial.println(outgoing);
}

//...................................................................   VOID INCOMING DATA   STATION ONLY............................................RX  RECEIVER
void receiveMessage(int packetSize) {
  if (packetSize == 0) return;
  int recipient = LoRa.read();
  byte sender = LoRa.read();
  byte incomingLength = LoRa.read();
  String incoming = "";
  while (LoRa.available()) {
    incoming += (char)LoRa.read();
    reC = incoming.toInt();
  }
  if (reC == 220 || reC == 221) {

    digitalWrite(3, HIGH); delay(1000); digitalWrite(3, LOW); //    .......... RED LED

    Serial.print("   Received  "); Serial.println(reC);
    //......    SEND BACK  ..............


    String sendbacK = String(reC);
    sendMessage(sendbacK);
   // Serial.print("   Sentback       "); Serial.println(sendbacK);

    station22();
  }
}

void station22() {
  if (reC == 220) {
    Serial.print (" Station  "); Serial.print(reC / 10); Serial.println("  is ON");
    digitalWrite(5, HIGH); //   ... STATION  ON
    digitalWrite(6, HIGH); //   ... RELAY ON
  }
  if (reC == 221) {
   // Serial.print (" Station  "); Serial.print(reC / 10); Serial.println("  is OFF");
    digitalWrite (5, LOW); //   ...  STATION OFF
    digitalWrite(6, LOW);  //   ...  RELAY OFF
  }
}

void loop() {


  receiveMessage(LoRa.parsePacket()); //    RECEIVE MESSAGE


  //............................................  FLOW METER  .............................
  if (pulse > 1) {
    waterflowsensor();
  }


}

void waterflowsensor() {

  Volume = pulse * 1.43;
  //...  Correction factor for 30mL pm = /.795
  Volume = (Volume / .795);
  FlowRate = Volume / 2;
  TotalVolumeML = TotalVolumeML + Volume;
  pulse = 0;
  // Serial.print(" Pulse is  ");Serial.print(pulse);
  // Serial.print(" Flow Rate is  ");Serial.print(FlowRate);
  // Serial.print(" mL pSecond     ");Serial.print(" Total Volume = ");
  // Serial.print(TotalVolume);Serial.println(" mL");

  //   ..... ..................................... Only send back if Total Volume changes up

  if (OldTotalVolumeML < TotalVolumeML) {

    //Serial.print(" OLD Total////  Total Volume    ");Serial.print(OldTotalVolume);Serial.print("..../");Serial.println(TotalVolume);
    //String sendbacK1 = String(-22);//  Needs be "-" so Tx knows its a Water flow Station number
    //sendMessage(sendbacK1);


    //String sendbacK2 = String(TotalVolume);
    //sendMessage(sendbacK2);
    //Serial.print("  Sent back   STATION  ");Serial.print(sendbacK1);Serial.print("  TOTAL VOLUME  ");Serial.print(sendbacK2);Serial.println (" mL");
  TotalVolumeLITRES = (TotalVolumeML / 1000);
   // Serial.print("    TOTAL VOLUME  = "); Serial.print(TotalVolumeML); Serial.println(" MilliLitres");
   // Serial.print("    TOTAL VOLUME  = "); Serial.print(TotalVolumeLITRES); Serial.println(" Litres");
    sendbacK = ((TotalVolumeLITRES * 1000) + 22);
    if (sendbacK > 1000) {
      //Serial.print("    SENDBACK +==  = "); Serial.println(sendbacK);
      String returN = String(sendbacK);
      //Serial.print("    SENDBACK  = "); Serial.println(returN);
      sendMessage(returN);//   ..... ..................................... Only send back if Total Volume changes up
      OldTotalVolumeML = TotalVolumeML;
    }
  }





}
void increase() {

  pulse++;
}

Thanks.

Then using the String type is very dangerous. Try to rewrite your code to use C strings (char[]).

Why do you even bother with an interrupt? Just check for incoming trigger in loop.

How often are you expecting the interrupt to trigger?

Maybe in your function

`void increase() {
  pulse++;
} '

put the "pulse++" command somewhere else, and just put a boolen flag variable within the interrupt routine that you can check elsewhere in your code, then you can code some logic to check status, e.g if flag == true AND "some other condition != true (e.g. I haven't pressed the big red stop button) then go about business as normal`.

It depends on the specific requirements and constraints of your project , but in general, using a hardware interrupt for a sensor can be beneficial, Timing accuracy: Hardware interrupts are triggered immediately when the sensor detects an event, regardless of what the rest of the code is doing. This is maybe why you cant stop it?.
2. Efficiency: Using a hardware interrupt allows the microcontroller to "sleep" when it's not actively processing sensor data, which can help reduce power consumption in battery-powered applications.

However, using a hardware interrupt also has some downsides to consider, Code complexity: Using interrupts can make your code more complex and harder to debug, and harder to break into.

Thanks to you all for your reply.
Have decided to ditch the " interrupt" and go with just continually reading the Pin and count the pulses. This seems to work just fine.

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