RC Switch unrelaiable

Hi folks, I have a strange behaviour of my sketch running on my mega: a 433Hz transmitter is connected to arduino, controlling a 24V, 3A Water pump (outlet is rated for that inductive load).
Somehow it works as expected for some time but after some minutes (~4 loops, sometimes more, sometimes less of turning the outlet on and off) the outlet doesn’t turn off anymore. I have absolutely no clue why that is. My sketch shouldn’t be bugged. For debugging I’ve deactivated most features, only core functionality stays activated (webserver and millis switch):

#include <RTClib.h>
RTC_DS3231 rtc;

#include <SPI.h>
#include <Ethernet.h>

#include <RCSwitch.h>

#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#define DHTPIN 12
#define DHTTYPE DHT22 
DHT dht(DHTPIN, DHTTYPE);
float hum=0;
float temp=0; 

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };   //physical mac address
byte ip[] = { 192, 168, 0, 45 };                      // ip in lan (that's what you need to use in your browser. ("192.168.1.178")
byte gateway[] = { 192, 168, 0, 1 };                   // internet access via router
byte subnet[] = { 255, 255, 255, 0 };                  //subnet mask
EthernetServer server(80);                             //server port     
String readString;
RCSwitch mySwitch = RCSwitch();
// RCSwitch recieve = RCSwitch();

unsigned long preMillisDht=0;
unsigned long postMillisDht=0; 

unsigned long preMillisHPPump=0;
unsigned long postMillisHPPump=0;
unsigned long MillisHPPumpAn=0;
int state = 0;
int HPhr;
int HPmi;
int HPse;

unsigned long preMillisFan=0;
unsigned long postMillisFan=0;
unsigned long MillisFanAn=0;
int state2 = 0; 

int state3 = 0;

void setup() {
Serial.begin(9600);

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }
dht.begin();
// recieve.enableReceive(0); //Reciever Pin interrupt 0 (#2)
mySwitch.enableTransmit(5);  // Transmitter is connected to Arduino Pin #4
mySwitch.setPulseLength(351);
// mySwitch.setRepeatTransmit(15);

Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
Serial.println();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());

//preMillisDht = millis(); 
preMillisHPPump = millis();
//preMillisFan = millis();
state3 == 1;
}

void loop() {
 /* postMillisDht = millis();
  if (postMillisDht - preMillisDht > 10000){
    hum = dht.readHumidity();
    temp = dht.readTemperature();
    Serial.print("Temp:");
    Serial.println(temp);
    Serial.print("Hum:");
    Serial.println(hum);
    preMillisDht = millis();
  } */

  postMillisHPPump = millis();
  if (state == 0){
    if (postMillisHPPump - preMillisHPPump > 180000){
    funk1On();
    } else {;}
  }
  else if (state == 1){
    if (postMillisHPPump - MillisHPPumpAn > 3500){
    funk1Off();
    } else {;}
  }

/*DateTime nowTime = rtc.now();
int hou = nowTime.hour();

   postMillisFan = millis();
    if (state3 == 1 && hou >= 9 && hou < 22 && state2 == 0 && postMillisFan - preMillisFan > 20000){
      funk2On();
    }
    else if (state2 == 1 && postMillisFan - MillisFanAn > 2000){
      funk2Off();
    } */
  serv();
}
//---------------------------------Switches---------------------------------------------------   

void funk1On(){
    mySwitch.send(4294901821, 32); //outlet 1 on signal
/*  if (recieve.available()) {
    if (recieve.getReceivedValue() == 4294901821){
      Serial.println("F1on: Signal OK");    
    }
    else {
      Serial.println("F1on: bad signal"); 
      funk1On();
    }
  recieve.resetAvailable();
  } */
  Serial.println("Funk1 on (HpPump)");
 /* DateTime HPtime = rtc.now();
    HPhr = HPtime.hour();
    HPmi = HPtime.minute();
    HPse = HPtime.second();   */
  state = 1;   
 MillisHPPumpAn = millis();
}

void funk1Off(){
    mySwitch.send(4294901819, 32); /* outlet 1 off signal */
/*  if (recieve.available()) { 
    if (recieve.getReceivedValue() == 4294901819){
      Serial.println("F1off: Signal OK");    
    } 
    else {
      Serial.println("F1off: bad signal"); 
      funk1Off();
    }
  recieve.resetAvailable();
  } */

  Serial.println("Funk1 off (HpPump)");
  state = 0;
  preMillisHPPump = millis();
}    

/* void funk2On(){
  mySwitch.send(4294901877, 32);
  delay(1000);
    if (recieve.available()) { 
    if (recieve.getReceivedValue() == 4294901877){
      Serial.println("F2on: Signal OK");    
    } 
    else {
      Serial.println("F2on: bad signal"); 
      funk2On();
    }
  recieve.resetAvailable();
  } 
  Serial.println("Funk2 on (Fan)");
  state2 = 1;
  preMillisFan = millis();
}    

void funk2Off(){
  mySwitch.send(4294901875, 32); 
  delay(1000);
 if (recieve.available()) { 
    if (recieve.getReceivedValue() == 4294901875){
      Serial.println("F2off: Signal OK");    
    } 
    else {
      Serial.println("F2off: bad signal"); 
      funk2Off();
    }
  recieve.resetAvailable();
  }
  Serial.println("Funk2 off (Fan)");
  state2 = 0;
  preMillisFan = millis();
}   

void button1On(){
  state3 = 1;
}

void button1Off(){
  state3 = 0;
}
 */
//---------------------------------Switches---------------------------------------------------   

void serv() {
  Due to character limitation i had to shorten my sketch. Thatswhy the code for the webserver isn't here anymore.

As you can see, it’s a very basic logic switch but why the f*** does it behaves like that? I’ve thought of that the whole week, please someone help me, I’m so lost…

Does the line "Funk1 off (HpPump)" appear on the serial monitor? If not, it means that void funk1Off() is not even called, and the problem lies in loop().

Insert a couple of Serial.println() statements into this piece of code to check the values of all used variables:

  if (state == 0){
    if (postMillisHPPump - preMillisHPPump > 180000){
    funk1On();
    } else {;}
  }
  else if (state == 1){
    if (postMillisHPPump - MillisHPPumpAn > 3500){
    funk1Off();
    } else {;}

My guess is that preMillisHPPump or MillisHPPumpAn doesn't have the expected value.

Erik_Baas:
Does the line "Funk1 off (HpPump)" appear on the serial monitor? If not, it means that void funk1Off() is not even called, and the problem lies in loop().

Insert a couple of Serial.println() statements into this piece of code to check the values of all used variables:

  if (state == 0){

if (postMillisHPPump - preMillisHPPump > 180000){
   funk1On();
   } else {;}
 }
 else if (state == 1){
   if (postMillisHPPump - MillisHPPumpAn > 3500){
   funk1Off();
   } else {;}



My *guess* is that `preMillisHPPump` or `MillisHPPumpAn` doesn't have the expected value.

I'm not at home rn, I'll add these serial prints when I come home, thanks for that.
Funk1 off (HPPump) appears in serial even after those loops, when the outlet stops turning off again. Hence my confusement lol.

Someone else maybe any clue? Since serial prints (on and off) are present even if the ououtlet doesn't switch off anymore I don't think there's something wrong with my millis loop.
:frowning: