Mercury sensor bounce?

Hey guys I have a setup with 2 mercury sensors which control a solenoid when either mercury switch is activated. My problem is that the mercury switches have a lot of bounce in them and I'm not sure how to integrate a debouncer into my code. The time from the signal of the mercury switch to the firing of the solenoid is key to my application so I'm trying not to delay that firing too long. I attached my code for any guidance. I'm new at this so please bear with me.

const int  RightTilt = 2;    // the pin that the Right Mercury Sensor is attached to
const int  LeftTilt = 4;    // the pin that the Left Mercury Sensor is attached to
const int Solenoid = 12;       // the pin that the Solenoid is attached to

int FallCounter = 0;   // Counter for the number of falls presses
int RightState = 0;  // current state of the Right Sensor
int LeftState = 0;  // current state of the Left Sensor
int lastButtonStateLeft = 0;     // previous state of the Left Sensor
int lastButtonStateRight = 0;     // previous state of the Right Sensor

void setup() {
  // initialize the Sesnor pins as inputs:
  pinMode(RightTilt, INPUT);
  pinMode(LeftTilt, INPUT);
  // initialize the Solenoid as an output:
    pinMode(Solenoid, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the Sensor input pins:
  RightState = digitalRead(RightTilt);
  LeftState = digitalRead(LeftTilt);
  
  // compare the buttonState to its previous state
  if (RightState != lastButtonStateRight or LeftState != lastButtonStateLeft) {

    // if the state has changed, increment the counter
      
    if (RightState ==LOW or LeftState ==LOW) {
      // if the current state is HIGH then a Sensor
      // went from off to on:
      FallCounter++;
      digitalWrite(Solenoid, HIGH);
      delay(300);
      digitalWrite(Solenoid, LOW);
      Serial.println("ON");
      Serial.print("Number of falls:  ");
      Serial.println(FallCounter);
    } 
    else {
      // if the current state is LOW then a Sensor
      // went from on to off:
      digitalWrite(Solenoid, LOW);
      Serial.println("OFF"); 
    }
  
  // save the current state as the last state, 
  //for next time through the loop
  lastButtonStateRight = RightState;
  lastButtonStateLeft = LeftState;
  }
}

Bump!

That delay(300) should be sufficient for debounce in the if clause. Just add a delay(20) or so in the else clause.

Things to consider:

  • Does your solenoid coil have a protection diode or snubber circuit? Absense of this could create havoc on your active components due to spikes and EMI regardless of whether your code includes debouncing. Also, any relays should include the same.
  • proximity of relays and solenoids to your active components
  • scope the control signal(s) with solenoid and relays disconnected to see if it is a software issue
  • scope the control signal(s) with solenoid and relays connected to see if your hardware is causing the problem

wildbill: That delay(300) should be sufficient for debounce in the if clause. Just add a delay(20) or so in the else clause.

Correct me if I'm wrong but I thought that delay was only there to what 300ms and then turn off the solenoid. I'm looking to add code so that it would read like so: switch is on for xx ms so now send the signal to the solenoid. if less than xx Ms then dont do anything.

That is what delay does, you can not read the switch again until that delay has passed so you are getting two for the price of one.

The thing with debouncing is that there will be multiple high and low signals from the switch until it settles. Once you detect one instance of the state you're looking for, all you need to do is ignore the switch for a while. If you're using a delay (or other time consuming thing) that will take care of the switch. In this case, yes, the delay is to hold the solenoid on, but while that's going on, the debounce is taken care of. The issue is that in the else clause, there's no delay, so you'll whip right back to the top of loop and the bouncing will bite you.

Edit: verbose version of what grumpy_mike said.