Help with Debouncing 2 limit switches

Hi,

I hope some one can help me get my head around this as I am new to Arduino/Coding.....

I am building a motorised in car mount for my tablet. The idea is the map cubby on the top of my dash opens and the mount comes out and flips up. I have hacked a broken in dash single DIN DVD player with built in screen to use the mechanism that flips the screen out etc. I have attached an Arduino and via an H-Bridge (L293D) I am able to control the mechanism the way I want (when back limit switch is HIGH and open button is pressed run the motor until the forward limit switch is triggered and vice versa etc.).
My issue now is debouncing the buttons/switches. I think I understand what I need to do for the button the operates as the open/close button, but can't think how to code the limit switches so they are debounced to. An tips greatly received!

My code is:

const int buttonPin = 13; // the number of the forward motion pushbutton pin
const int icinput1Pin = 9; // the number of output pin to L293D input pin 2
const int icinput2Pin = 8; // the number of output pin to L293D input pin 7
const int enablePin = 11; // the number of output pin to L293D input pin 1
const int fwdlimitPin = 12; // the number of the output pin to the forward limit switch
const int bwdlimitPin = 7; // the number of the output pin to the forward limit switch

// variables will change:

int buttonState; // variable for reading the pushbutton status
int fwdswitchState = 0; // variable for reading the fwdswitchState status
int bwdswitchState = 0; // variable for reading the bwdswitchState status
int lastButtonState = LOW;
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 5; // the debounce time; increase if the output flickers

void setup() {
// intializse the enable pin on L293D IC
pinMode(enablePin, OUTPUT);
// initialize the motion pins as outputs:
pinMode(icinput1Pin, OUTPUT);
pinMode(icinput2Pin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
// initialize the limit switch pins as inputs:
pinMode(fwdlimitPin, INPUT);
pinMode(bwdlimitPin, INPUT);
initon(); // Runs the init function during power up
}

void loop() {

// read the state of the pushbutton value:
int reading = digitalRead(buttonPin);
fwdswitchState = digitalRead(fwdlimitPin);
bwdswitchState = digitalRead(bwdlimitPin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:

if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}

if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:

// if the button state has changed:
if (reading != buttonState)
{
buttonState = reading;
}
}
if (buttonState == HIGH && fwdswitchState == LOW) // This means the mount is in the closed position
{
while (digitalRead(fwdlimitPin) == LOW)
{
// turn Motor on to open mount:
analogWrite(enablePin, 255);
digitalWrite(icinput1Pin, HIGH);
digitalWrite(icinput2Pin, LOW);
}
}

else if (buttonState == HIGH && fwdswitchState == HIGH) // This merans the mount is in the open position
{
while (digitalRead(bwdlimitPin) == LOW)
{
analogWrite(enablePin, 255);
digitalWrite(icinput1Pin, LOW);
digitalWrite(icinput2Pin, HIGH);
}
}
else {
digitalWrite(icinput1Pin, LOW);
digitalWrite(icinput2Pin, LOW);
}
lastButtonState = reading;
}

void initon()// This function looks to see if the power was disconnected (ignition turned off) while the mount was either opening or closing. The mount will be returned to the closed position
{
if (fwdswitchState == LOW && bwdswitchState == LOW)
{

while (digitalRead(bwdlimitPin) == LOW)
{
analogWrite(enablePin, 255);
digitalWrite(icinput1Pin, LOW);
digitalWrite(icinput2Pin, HIGH);
}
}
}

Standard practice is to wire switches to ground and an input that is set to INPUT_PULLUP with pinMode. A switch wired that way can usually be hardware debounced by putting a 0.1uf cap across the switch. When wired between ground and an input the switch will be HIGH when not pushed and LOW when pushed.

Limit switches don't need debouncing. As soon as the limit is touched, you want it to stop driving that direction.

You don't then do any other action, so if the limit switch goes off and on a few times, it doesn't matter. Due to the mehcanical nature of the thing, if you stop the motor at the first touch, it will run on a little bit further, putting the switch "definitely" on, so it's not left floating or indeterminate.

Again, when you move off the limit switch, you are driving away from it and you aren't really checking that it goes off or trying to count anything - you should be checking the limit switch at the other end of the travel.

1 Like

Thanks for the updates guys. You have both taught me something!!

Thanks again.