Your thoughts on my motor controller w/ limit switches

Hi everyone, this is my simple approach to a motor controller with limit switches. I have two physical switches tie to ground on one side, and set to PULLUP, controlling relays for the AC to the motor and the limit switches will be hooked to ground on one side also with the input assigned as PULLUP. Am I under thinking it. This is for a motorcycle lift table. Thanks, adown

const int upswitchPin = 2;           // pin number of up button input
const int downswitchPin = 3;               // pin number of down button input
const int uplimitPin = 4;                     // pin number of up limit switch input
const int downlimitPin = 5;                 // pin number of down limit switch input
const int goupPin = 7;                       // pin number of the up output pin
const int godownPin = 8;                   // pin number of the down output pin

int upswitchState = 0;                      // innitialize the up switch state to 0
int downswitchState = 0;                  // innitialize the down switch state to 0
int uplimitState = 0;                        // innitialize the up limit switch state to 0
int downlimitState = 0;                    // innitialize the down limit switch state to 0


void setup() {
   pinMode(goupPin, OUTPUT);          // set the pins as inputs and outputs
   pinMode(godownPin, OUTPUT);
   pinMode(upswitchPin, INPUT_PULLUP);
   pinMode(downswitchPin, INPUT_PULLUP);
   pinMode(uplimitPin, INPUT_PULLUP);
   pinMode(downlimitPin, INPUT_PULLUP);
}
void loop() {
   upswitchState = digitalRead(upswitchPin);          // read the up switch input
   downswitchState = digitalRead(downswitchPin);  // read the down switch input
   uplimitState = digitalRead(uplimitPin);              // read the up limit switch 
   downlimitState = digitalRead(downlimitPin);      // read the down limit switch
   
   if (upswitchState == LOW); {            //check to see if the up button is pressed
      if (uplimitState == HIGH);             // check to see if the up limit switch is open
       digitalWrite(goupPin, HIGH);         // power the up output pin
   }

   if (downswitchState == LOW); {         //check to see if the down button is preesed
     if (downlimitState == HIGH);          // check to see if the down limit switch is open
      digitalWrite(godownPin, HIGH);      // power the up output pin
   }
}

I don't think you have any logic to switch off power when the limit is reached.

I think I would separate the limit logic from the switch logic - something like this pseudo code

if (upLimit reached)
  power up off
else if (upSwitch ON)
  power down off // just in case
  power up on

However this approach (which is similar to your own) will turn the up motor on continuosly for a single press of the up switch. I'm not sure if that is what you want.

...R

Thanks for your reply Robin2. I am using a momentary button, so I want to go up or down only when a button is held. When I let off the button, I want it to stop. As I am holding the up button, will it not be continuously looping through the code(checking the button and limit switch) and and notice when the limit switch goes low(closed), or the button goes high(off)? Or, do I need to put "Return;" at the end. I am a beginner, and still only have a rough idea of what I'm doing.

When you do digitalWrite() it stays in that state until you change it. At the moment there is no code to cause it to stop when you take your finger off the button.

Maybe a way to do that would be like this (assuming LOW == switch pressed)

if (limitSwitch == HIGH && upSwitch == LOW) { // motor up ON } else { // motor up OFF }

I think a better way would be like this (within loop() )

motorDirection = 'S'
if (upSwitch == LOW && upLimit == HIGH) {
   motorDirection = 'U'
}
if (downSwitch == LOW && downLimit = HIGH) {
   motorDirection = 'D'
}
if (motorDirection == 'S') {
  // stop motor
}
else if (motorDirection == 'U') {
  // motor up
}
else if (motorDirection == 'D') {
  // motor down
}

...R

I like that!! I had originally written my if statements like that, but wasn't sure if that was ok.

Do I need to an "int", or "const int" (U = goupPin, HIGH) so it knows what to do with 'U'. I'm sure I need to define or initialize it or something, right?

Oh, and what does (within loop()) mean?

Thanks again, adown

adown: Oh, and what does (within loop()) mean?

It means I was too lazy to add void loop() { at the top of my code and indent all the lines ...

At the top of your code you would add

char motorDirection;

If that does not answer your question I think the best thing is for you to try to write the code so I can see what is in your mind. That way I can give a specific answer.

...R

It is a "given" that the limit switches will be wired normally closed in series with the corresponding motor control relays.

You then arrange your "sense" logic to the Arduino to determine from there when they have in fact actuated.

OK, I couldn't quite figure the char thing out, but I think the idea is to move the digitalWrite out of the void loop() to be able to detect a switch change while the motor is moving. So here is what I came up with. Thanks again for looking.

const int upswitchPin = 2;           // pin number of up button input
const int downswitchPin = 3;               // pin number of down button input
const int uplimitPin = 4;                     // pin number of up limit switch input
const int downlimitPin = 5;                 // pin number of down limit switch input
const int goupPin = 7;                       // pin number of the up output pin
const int godownPin = 8;                   // pin number of the down output pin

int upswitchState = 0;                      // innitialize the up switch state to 0
int downswitchState = 0;                  // innitialize the down switch state to 0
int uplimitState = 0;                        // innitialize the up limit switch state to 0
int downlimitState = 0;                    // innitialize the down limit switch state to 0


void setup() {
   pinMode(goupPin, OUTPUT);          // set the pins as inputs and outputs
   pinMode(godownPin, OUTPUT);
   pinMode(upswitchPin, INPUT_PULLUP);
   pinMode(downswitchPin, INPUT_PULLUP);
   pinMode(uplimitPin, INPUT_PULLUP);
   pinMode(downlimitPin, INPUT_PULLUP);
}
void loop() {
   upswitchState = digitalRead(upswitchPin);          // read the up switch input
   downswitchState = digitalRead(downswitchPin);      // read the down switch input
   uplimitState = digitalRead(uplimitPin);            // read the up limit switch 
   downlimitState = digitalRead(downlimitPin);        // read the down limit switch
   
   if (upswitchState == LOW && uplimitState == HIGH) { //check to see if the up button is pressed and the up limit switch is open
       goup();
   }   
   else if (downswitchState == LOW && downlimitState == HIGH) { //check to see if the down button is preesed and the down limit switch is open
       godown();
   }
    else {
       dontMove(); // if a button isn't pressed stop motor
    }
}
   void goup() {
      digitalWrite(goupPin, HIGH);     // power the up output pin
   }   
   void godown() {
     digitalWrite(godownPin, HIGH);   // power the down output pin
   }
   void dontMove() {
     digitalWrite(goupPin && godownPin, LOW); // write power up & down output pins low
   }

adown: but I think the idea is to move the digitalWrite out of the void loop() to be able to detect a switch change while the motor is moving. So here is what I came up with. Thanks again for looking.

That looks like it should work and it is easy to follow - which is important.

This line is wrong - it needs to be two separate digitalWrite()s

digitalWrite(goupPin && godownPin, LOW);

OK, I couldn't quite figure the char thing out,

All I had in mind was to create a char variable to record the required output - U D or S

...R

Do I need to put "return" after I digitalWrite to the output pins so it goes back to the void loop to monitor the switches.

   void goup() {
      digitalWrite(goupPin, HIGH);     // power the up output pin
   }   
   void godown() {
     digitalWrite(godownPin, HIGH);   // power the down output pin
   }
   void dontMove() {
     digitalWrite

.

adown: Do I need to put "return" after I digitalWrite to the output pins so it goes back to the void loop to monitor the switches.

No the function loop() repeats automatically.

Try it in a very simple Arduino program.

...R