     } else {
 
       // Battery getting low
-      if ((voltage < 12.1) && inverter) {
-          inverterOff();
+      if ((voltage < v_lower) && inverter) {
+          switch_to_mains++;
+      } else {
+           switch_to_mains = 0; 
       }
-         
+
       // Battery charged and/or solar power available
-      if ((voltage > 12.5) && !inverter) {
-        inverterOn();
+      if ((voltage > v_upper) && !inverter) {
+        switch_to_battery++;
+      } else {
+         switch_to_battery = 0; 
+      }
+      
+      if (switch_to_mains > MAX_SWITCH_COUNT) {
+        inverterOff();
+      }
+      
+      if (switch_to_battery > MAX_SWITCH_COUNT) {
+        inverterOn();        
       }
      
     }
     
     Serial.print(" inverter=");
     Serial.print(inverter);
+
+    Serial.print(" switch_to_mains=");
+    Serial.print(switch_to_mains);
+    
+    Serial.print(" switch_to_battery=");
+    Serial.print(switch_to_battery);    
     
     Serial.println();
     
     sample_count = 0;
     sum = 0;
     
-    delay(4600);
+    delay(LOOP_DELAY);
 }
 
 void inverterOn() {

--------------------------------------------------------------*/

// number of analog samples to take per reading
#define NUM_SAMPLES 20

int sum = 0;                    // sum of samples taken
unsigned char sample_count = 0; // current sample number


#define REF_VOLTAGE  4.97       // Arduino 5V voltage

#define INV_REMOTE      8       // Output pin for inverter remote control
#define INV_LIVE        9       // Output pin for inverter live power
#define INV_NEUTRAL    10       // Output pin for inverter neutral power

// Voltage thresholds for switching between battery/solar and mains
float v_upper = 12.55;
float v_lower = 11.85;

// Number of samples
int switch_to_battery = 0;
int switch_to_mains = 0;

// Voltage divider A0: 12.59 / 2.11

