Debounce effect on Hall Effect Sensor

Hi All

I have a A1104 Hall effect sensor in a circuit that is used to sequentially switch functions within a sketch running on a Pro Mini. It all works exactly as planned, but now I need to add a push button switch in parallel with the A1104 as an alternative function change switch (all pins of the Pro Mini are either in use or are not connected (and can't be now be connected as the Pro-Mini is permanently installed in place on an 'adapter' pcb)).

The problem I have is that the sensor didn't need any debounce routine included, but I have now modified the sketch to include a debounce because of the pushbutton switch. Now the sensor doesn't work, no matter how small I make the debounce interval. I've not yet physically attached the pushbutton in circuit.

Is this going to be a coding or hardware solution do you think?

Here's the switching function that is in question. I've left the original, working, non-debounced code in but commented out. All the definitions are made in the main (not included) code and were all ok.

// Switch
             // Switch operating function - no debounce is reqd for hall
             // switch but is included to allow debouncing of the Trigger switch
             
void switchMode()     {      

  /*
   byte currentState = digitalRead (ModePin);    // Read the switch pin
   if (currentState != lastState)                // Check whether switch state has changed
   {
   lastState = currentState;                     // Note current switch state
   if (currentState == LOW)                      // If switch operated
   switch_Flag++;                                // Increment switch state
   
   // Serial.println(currentState);
   // Serial.println(switch_Flag);
   
   if (switch_Flag > 3)                           // If cancelling ALERT mode,
   switch_Flag = 0;                               // Set to STANDBY mode
   }
   }
    
   */

  byte currentState = digitalRead(ModePin);
  unsigned long currentTime = millis();
  if (currentState != lastState) {
    timeOfLastButtonEvent = currentTime;
  }
  if (currentTime - timeOfLastButtonEvent > debounceInterval) {  // If enough time has passed
    if (currentState != debouncedState) {                        // If the current switch state differs from the last stored switch state
      debouncedState = currentState;                             // Update the switch state
      if (debouncedState == LOW) {
        switch_Flag++;                                           // Increment switch state
        if (switch_Flag > 3)                                     // If cancelling ALERT mode,
          switch_Flag = 0;                                       // Set to STANDBY mode
      }
    }
  }  
}

Thanks in advance!

Bernie

    timeOfLastButtonEvent = currentTime;

The time is the time of the last switch CHANGE event. Names are important!

Is this going to be a coding or hardware solution do you think?

Yes.

A schematic is going to be necessary, as is ALL of your code.

The time is the time of the last switch CHANGE event. Names are important!

Sorry Paul, I have to admit to using a debounce routine I had used before several times, and I didn’t write it originally, but it worked so I just took the lazy man’s option of not changing names - my idleness :*.

The reason for not including all the code is that its only this code fragment that has been changed and the remaining code has no effect on the problem. Its the converse, this fragment is not returning the switch_Flag value to the rest of the unchanged code now, whereas it did before the addition of the debounce routine. I can certainly post the code you think it absolutely necessary, but looking at some of the posts here, I’d be concerned that the discussion would veer away from the problem in hand, and disappear down the rabbithole of critiquing the rest of the code (which may be naive, but works) and not concentrating on the original problem.

I didn’t included a schematic because the switch hasn’t been installed yet, but I’ve attached a sketch ofthe proposed setup

Bernie

PS The 10K resistor is totally wrong - I’m actually pulling the pin high with the internal pullup resistor - sorry for the duff info

A1104.jpg

the switch hasn't been installed yet

Then how the hell can the code that deals with the switch POSSIBLY be useful?

Have you at least declared the pinMode() correctly?

Hi Paul

I haven't installed the pushbutton switch yet as I wanted to check the effect of the debounce code on the Hall effect sensor on its own first. As the one input pin won't know whether the input is as a result of a magnet passing the sensor or the pushbutton being operated, the code needs to cater for both.

Yup, the pinMode declaration is ok, I used

const byte ModePin = 12; and pinMode (ModePin, INPUT_PULLUP);

  • the code works in its original (non-debounced) format with the sensor, but swapping out the sensor reading code for the debounced sensor reading code has stopped it.

I think as a next step I'll remove the sensor from circuit and rig just the pushbutton, just to revive my confidence in that debounce routine!

Thanks for your comments so far.

Bernie

I think as a next step I'll remove the sensor from circuit and rig just the pushbutton, just to revive my confidence in that debounce routine!

Personally, I think the next step is to wire the switch, to a [u]different[/u] pin.

I agree Paul, but I don't have an accessible pin unfortunately :(

Bernie

PS The 10K resistor is totally wrong - I’m actually pulling the pin high with the internal pullup resistor

I would recommend leaving the resistor as shown - this gives about 5x more signal strength than the internal pullup. Especially important since you’ll be combining the sensor and pushbutton in a wired-OR configuration. You could even lower this to 4.7K and be well within the max current limitation of the sensor.

Note that any debouncing code will allways apply to both the sensor and pushbuton as they share the same input. Also note that many debouncing methods only deal with the first state change of the signal (rising or falling) and the trailing edge of the signal is not dealt with. There are examples on the forum and elsewhere that can debounce both edges of the signal (if required).