Hysteresis with a boolean sensor

I'm making an aquarium controller that senses water level with a float switch and controls a pump. The float switch is a simple on/off boolean switch, but I'd like to have some sort of hysteresis for it so the pump doesn't cycle on and off excessively. The only way I could think of without adding additional switches was to average readings over several seconds (see code,) so if more than 8 of the last 10 readings were high, the pump would turn off, but it wouldn't turn on until less that 3 of the last 10 readings were low.

Can anyone think of a better method?

#define floatSwitchPin 9

int floatSwitchSum;
boolean floatSwitch[10];
boolean outlet = LOW;

void setup(){
  Serial.begin(57600);
  pinMode(floatSwitchPin, INPUT_PULLUP);
}

void loop(){
  floatSwitchSum -= floatSwitch[9];  // remove the oldest value from sum
  for(int i = 9; i>0; i--){          // shift everything right
    floatSwitch[i] = floatSwitch[i-1];
  }
  floatSwitch[0] = digitalRead(floatSwitchPin); // get new value
  floatSwitchSum += floatSwitch[0];  // Add new value to sum

  if(floatSwitchSum < 3){
    outlet = HIGH;
  } else if (floatSwitchSum > 8){
    outlet = LOW;
  }
  
  Serial.print("Sum = ");
  Serial.print(floatSwitchSum);
  Serial.print("  Outlet: ");
  Serial.println(outlet);
  delay(1000);
}

It would be simpler to just keep a count of how many readings you take and another count of how many of them told you water was needed. No need for the array. When you have enough readings (which can be a larger number now you don't have to store them all), if the proportion asking for water is sufficient, run your pump and then zero your counts out.

As an additional safety feature, you might want to use millis to ensure that whatever the float switch says, you don't add water again for some number of hours.

The only float switches I have any experience of have a considerable amount of mechanical hysteresis built in. What are the actual characteristics of the switches you're using around the point that you are trying to detect i.e. what sequence of input states are you actually getting above and below the level you're trying to detect?

I haven't actually put the switch in the tank yet; just feeling it with my fingers as it moves up and down, it likely does have some mechanical hysteresis. The switch I'm using is here: http://www.bulkreefsupply.com/catalog/product/view/id/1028/ I think it's essentially a reed switch in the center with a magnet in the outer float ring. My goal was to prevent excessive cycling when the level was right at the switch point, but to also prevent cycling due to ripples/waves that may occur.

Wildbill:Thanks for your suggestions. I'm not sure if the first suggestion would work. The purpose of the switch is as a fail safe to prevent the pumps from causing overflow issues if the level rises too high; there is actually another level sensor that regulates the normal water level at a lower level. Since the controller will be running for months, there's a risk for overflow, but more importantly, wouldn't a long period of low water levels outweigh a short period of high levels? My goal was to look at an average over the last 10 seconds so a few spurious readings would cause any changes, but more consistently high readings would.

In my actual code I am using millis() instead of delay() as you suggested.

Ah, I hadn't gathered that the switch was a failsafe for the pump. I think my point still stands though - however many readings you decide to take in each sampling period, you don't need the array, just a count of how many of them said the water was too high. If the count exceeds some threshold, don't allow the pump to be on.