trying to set up motor with limit switches

Hello, I'm fairly new to all this, basically I am trying to control a garage door opener keeping the hardware more or less the way it is. For this i will have a relay for motor up and down, as well as to control the fan, which will run when the it is at the bottom limit. I've written some simple code and seem to have found a dead end. any help is appreciated!

const int motorUp = 2;
const int motorDown = 3;
const int fanPin = 4;

const int topSwitch = 5;
const int bottomSwitch = 6;
const int switchPin = 7;

void setup() {
// put your setup code here, to run once:
pinMode(motorUp, OUTPUT);
pinMode(motorDown, OUTPUT);
pinMode(fanPin, OUTPUT);

pinMode(topSwitch, INPUT);
pinMode(bottomSwitch, INPUT);
pinMode(switchPin, INPUT);

}

void loop() {
// put your main code here, to run repeatedly:

int switchPos = digitalRead(switchPin);
int topLimit = digitalRead(topSwitch);
int bottomLimit = digitalRead(bottomSwitch);

if ((switchPos) == HIGH)
{
Serial.write("button pooshed");
if ((topLimit) == HIGH)
{
digitalWrite(motorDown, HIGH);
if ((bottomLimit) == HIGH)
{
digitalWrite(motorDown, LOW);
}
}

if ((bottomLimit) == HIGH)
{
digitalWrite(motorUp, HIGH);
if ((topLimit) == HIGH)
{
digitalWrite(motorUp, LOW);
}
}
else
{
digitalWrite(motorUp, HIGH);
if ((topLimit) == HIGH)
{
digitalWrite(motorUp, LOW);
}
}
}

if ((bottomLimit) == HIGH)
{
digitalWrite(fanPin, HIGH);
}
else if ((bottomLimit) == LOW)
{
digitalWrite(fanPin, LOW);
}

}

Please read "How to use this Forum", especially how to attach code.
What does the code do and do You want it to do?

Your code appears to rely on the user keeping the button pressed while the door is moving. If they don’t, the motor will run forever.

Also, as you’re not using INPUT_PULLUP, you’ll need external pulldown resistors. How is everything wired up?

I do have pulldown resistors installed, as of right now its just hooked up to buttons for inputs and leds for outputs so i can troubleshoot it. what I want is to be able to push the button, and if it is on the bottom limit, run up to the top limit, and vice versa. if it is stuck in the middle, i'd like it to run up to the top.

I re-added the debounce I originally had in it, it still seems that "motorUp" is constantly running, what am I missing?

const int motorUp = 2;
const int motorDown = 3;
const int fanPin = 4;

const int topSwitch = 5;
const int bottomSwitch = 6;
const int switchPin = 7;

bool lastButton = false;
bool currentButton = false;

void setup() {
  // put your setup code here, to run once:
  pinMode(motorUp, OUTPUT);
  pinMode(motorDown, OUTPUT);
  pinMode(fanPin, OUTPUT);

  pinMode(topSwitch, INPUT);
  pinMode(bottomSwitch, INPUT);
  pinMode(switchPin, INPUT);
}

boolean debounce (boolean last)
{
  boolean current = digitalRead(switchPin);
  if (last != current);
  {
    delay(5);
    digitalRead(switchPin);
  }
  return (current);
}

void loop() {
  // put your main code here, to run repeatedly:

  int switchPos = digitalRead(switchPin);
  int topLimit = digitalRead(topSwitch);
  int bottomLimit = digitalRead(bottomSwitch);

  currentButton = debounce(lastButton);
  if (lastButton == LOW && currentButton == HIGH)
  {
    Serial.write("button pooshed");
    if ((topLimit) == HIGH)
    {
      digitalWrite(motorDown, HIGH);
      if ((bottomLimit) == HIGH)
      {
        digitalWrite(motorDown, LOW);
      }
    }

    if ((bottomLimit) == HIGH)
    {
      digitalWrite(motorUp, HIGH);
      if ((topLimit) == HIGH)
      {
        digitalWrite(motorUp, LOW);
      }
    }
    else
    {
      digitalWrite(motorUp, HIGH);
      if ((topLimit) == HIGH)
      {
        digitalWrite(motorUp, LOW);
      }
    }
  }

  if ((bottomLimit) == HIGH)
  {
    digitalWrite(fanPin, HIGH);
  }
  else if ((bottomLimit) == LOW)
  {
    digitalWrite(fanPin, LOW);
  }
  
  lastButton = currentButton;
}

Same issue. The code to turn the motor off is inside the code that checks that a button was pressed. Now that you've added the debounce and are looking for a rising transition you've made it worse.

I suggest that you move the motor off stuff out of that if and check on every iteration of loop if one of the limit switches has been hit. Then turn the motors off. You may want a little delay when you start the motors to ensure that the limit switches are released.

Personally I would hard wire the limit switches into the motor circuit so the motor isn’t stalled in the event of a program crash .

You could actually then just have up and down buttons driving relays and not bother with the Arduino !

Best I could find. :roll_eyes:

What are you using for limit switches? What is the motor’s full load current (lifting the door)?