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.