Bring motor to "home" position?

I’m using a sabertooth 2x60 controller. My code is for opening and closing a door, and I have it set up as a state machine with limit switches for both open and closed states of the door. I’ve tried a few different things to create a “home” position so that regardless of where the door is when the system is powered on, the first thing it does is return to the same position.
In this code, I have it written in a separate function, which seems clean, and then placed into the setup loop. How can I get this working?

#include <SabertoothSimplified.h>
#include <SoftwareSerial.h>



const int limit1 = 49;
const int sensor = 47;
const int limit2 = 43;
const int button = 39;
const int greenLED = 35;
const int ledpin = 13;
SoftwareSerial SWSerial(NOT_A_PIN,14);
SabertoothSimplified ST(SWSerial);


int phoneTrip;
int foodPressed;
int eot1;
int eot2;
/*
eot=0   Limit switch not pressed
eot=1   Limit switch pressed

trip=0  Beam uninterupted
trip=1  Beam interupted
*/

void setup() {
  pinMode(ledpin, OUTPUT);
  pinMode(sensor, INPUT_PULLUP);
  pinMode(limit1, INPUT_PULLUP);
  pinMode(limit2, INPUT_PULLUP);
  pinMode(greenLED, OUTPUT);
  pinMode(button, INPUT_PULLUP);
  SWSerial.begin(9600);


  void motorHome();
}
void loop() {
  
  phoneTrip = digitalRead(sensor);
  foodPressed = digitalRead(button);
  eot1 = digitalRead(limit1);
  eot2 = digitalRead(limit2);
  digitalWrite(ledpin, phoneTrip);
  digitalWrite(greenLED, foodPressed);
  
  static enum {CLOSED,OPENING,OPEN,CLOSING} state;

  switch (state) {
    case CLOSED:
      if (phoneTrip == 1){ //if the sensor is tripped
        state = OPENING;
      }
      break;
    case OPENING:
      ST.motor(127);
      if (eot1 == 1){
        ST.motor(0);
        state = OPEN;
      }
      break;
    case OPEN:
      if(foodPressed == 1){
        delay(5000);
        state = CLOSING;
      }
      break;
    case CLOSING:
      ST.motor(-127);
      if (eot2 == 1){
        ST.motor(0);
        state = CLOSED;
      }
      break;
  }
}

void motorHome(){
  if (eot1 == 1){
    while(eot2 != 1){
      ST.motor(-127);
    }
    ST.motor(0);
    
  }
  else{
    ST.motor(127);
    delay(1000);
    while(eot2 != 1){
      ST.motor(-127);
    }
    ST.motor(0);
  }
}

Why have you got a function prototype for motorHome in setup?

Why have you got a function prototype for motorHome in setup?

just means:

in setup just use the command:

motorHome();

without "void"

oops.
With that fixed in the code, it does start motorHome() but it didn’t respond to the inputs and instead enters the “else” part, which causes the motor to start, quickly reverse direction and then run infinitely (with no response to inputs).
I added different variables as shown below so that they are defined before running motorHome(). Any idea whats going on?

#include <SabertoothSimplified.h>
#include <SoftwareSerial.h>



const int limit1 = 49;
const int sensor = 47;
const int limit2 = 43;
const int button = 39;
const int greenLED = 35;
const int ledpin = 13;
SoftwareSerial SWSerial(NOT_A_PIN,14);
SabertoothSimplified ST(SWSerial);


int phoneTrip;
int foodPressed;
int eot1;
int eot2;
/*
eot=0   Limit switch not pressed
eot=1   Limit switch pressed

trip=0  Beam uninterupted
trip=1  Beam interupted
*/
int start1; 
int start2;

void setup() {
 pinMode(ledpin, OUTPUT);
 pinMode(sensor, INPUT_PULLUP);
 pinMode(limit1, INPUT_PULLUP);
 pinMode(limit2, INPUT_PULLUP);
 pinMode(greenLED, OUTPUT);
 pinMode(button, INPUT_PULLUP);
 SWSerial.begin(9600);

 start1 = digitalRead(limit1);
 start2 = digitalRead(limit2);
 motorHome();
}
void loop() {
 
 phoneTrip = digitalRead(sensor);
 foodPressed = digitalRead(button);
 eot1 = digitalRead(limit1);
 eot2 = digitalRead(limit2);
 digitalWrite(ledpin, phoneTrip);
 digitalWrite(greenLED, foodPressed);
 
 static enum {CLOSED,OPENING,OPEN,CLOSING} state;

 switch (state) {
   case CLOSED:
     if (phoneTrip == 1){ //if the sensor is tripped
       state = OPENING;
     }
     break;
   case OPENING:
     ST.motor(127);
     if (eot1 == 1){
       ST.motor(0);
       state = OPEN;
     }
     break;
   case OPEN:
     if(foodPressed == 1){
       delay(5000);
       state = CLOSING;
     }
     break;
   case CLOSING:
     ST.motor(-127);
     if (eot2 == 1){
       ST.motor(0);
       state = CLOSED;
     }
     break;
 }
}

void motorHome(){
 if (start1 == 1){
   while(start2 != 1){
     ST.motor(-127);
   }
   ST.motor(0);
   
 }
 else{
   ST.motor(127);
   delay(1000);
   while(start2 != 1){
     ST.motor(-127);
   }
   ST.motor(0);
 }
}

Why don't you get your sketch to tell you what's going on, by adding some prints?

sorry, I'm not sure what you mean by that. could you elaborate?

You can determine if your program flow is working as designed or prove a variable is what it sould be by adding some Serial.prints at strategic locations.

. . . Serial.print("I got this far in the code, myVariable = "); Serial.println(myVariable); . . .

.

Better still

Serial.print(F("I got this far in the code, myVariable = "));
Serial.println(myVariable);
. . .

That way, there's less chance of introducing unexpected and even harder-to-find bugs.

Also:
If you have a spare LED, you can turn it on or toggle it.
Monitoring this LED with your eye or a scope/logic probe can tell you if a section of code has been reached.

And then there are Debug Macros:

// DebugMacros.h

/* Example of use:
   #define DEBUG          // <-----<<<< this line must appear before the include line
   #include <DebugMacros.h>
*/

//If you comment the line:  #define DEBUG
//The Macro lines are defined as blank, thus would be ignored by the compiler.
//If the line is NOT commented, these macros will be included in the sketch.
// examples:
// This  converts to         >>>>----->         This OR a Blank Line. 
//---–----------------------------------------------------------------------------------------
// DPRINTLN("Testing123");   >>>>----->     Serial.println("Testing123"); 
// DPRINTLN(0xC0FFEEul,DEC); >>>>----->     Serial.println(0xC0FFEEul,DEC);
// DPRINTLN(12648430ul,HEX); >>>------>     Serial.println(12648430ul,HEX);
// DPRINTLNF("This text came from flash");  Serial.println(F("This text came from flash"));
// DPRINT(myVariable);       >>>>----->     Serial.print(myVariable);
// DELAY(100);               >>>>----->     delay(100);
// SERIALBEGIN(9600);        >>>>----->     Serial.begin(9600);
// PINMODE(13,OUTPUT);       >>>>----->     pinMode(13,OUTPUT);
// TOGGLEd13;                >>>>----->     PINB = 0x20;  // D13 Toggle,for UNO ONLY
 
#ifdef DEBUG
//#define DPRINT(args...)  Serial.print(args)             //OR use the following syntax:
#define SERIALBEGIN(...)   Serial.begin(__VA_ARGS__)
#define DPRINT(...)        Serial.print(__VA_ARGS__)
#define DPRINTLN(...)      Serial.println(__VA_ARGS__)
#define DRINTF(...)        Serial.print(F(__VA_ARGS__))
#define DPRINTLNF(...)     Serial.println(F(__VA_ARGS__)) //Printing text using the F macro
#define DELAY(...)         delay(__VA_ARGS__)
#define PINMODE(...)       pinMode(__VA_ARGS__)
#define TOGGLEd13          PINB = 0x20                    //For the UNO only
 
#else
#define SERIALBEGIN(...)   //blank line
#define DPRINT(...)        //blank line
#define DPRINTLN(...)      //blank line
#define DPRINTF(...)       //blank line
#define DPRINTLNF(...)     //blank line
#define DELAY(...)         //blank line
#define PINMODE(...)       //blank line
#define TOGGLEd13          //blank line 
 
#endif
//***************************************************************

.

Ok thank you. I don't have my setup next to my computer, so I will try the LED technique

Your motorHome() function isn't reading any pins (obvious, there are no calls to digitalRead() in it!)

I can't figure out why this doesn't work. Every logic I've tried, it seems to get stuck in the setup(). I thought maybe having a while condition would help nudge it into the loop() but the limit switches don't do anything when its set up like this for some reason.

void motorHome(){
   digitalWrite(greenLED2, 1);
   start1 = digitalRead(limit1);
   start2 = digitalRead(limit2);
  while(start2 == 0){ //backwards slowly until it hits the switch and stops
    ST.motor(-15);
    }
  ST.motor(0);

}
while(start2 == 0){ //backwards slowly until it hits the switch and stops
    ST.motor(-15);
    }

How can you ever exit that loop?

You're still not reading the pin... Just lose start1 and start2 completely, and read the pin....

AWOL, shouldn't it exit the loop once limit switch is hit? I want the motor to run continuously until the switch is hit, which is why i'm not using if statements, and is why I have it set up as a state machine in the loop().

Mark, digitalRead() is reading the pin, i have the pin identified at the beginning of the sketch.

You're not reading the pin in the loop.

I changed the loop to while(digitalRead(limit2 == 0) and now it just seems to skip the motorHome completely.

No.
Check your parentheses.

I rewrote the home procedure a bit more clearly, and it reads better, but I think your logic is still flawed:

boolean at_limit1 () { return digitalRead (limit1) ; }
boolean at_limit2 () { return digitalRead (limit2) ; }

void motorHome()
{
 if (at_limit1())
 {
   while(! at_limit2())
     ST.motor(-127);

   ST.motor(0);   
 }
 else
 {
   ST.motor(127);
   delay(1000);

   while(! at_limit2())
     ST.motor(-127);

   ST.motor(0);
 }
}

Shouldn't you only look at one of the iimits to home? If already at the limit, back out first, otherwise move towards it till there? So perhaps you only need at_limit2 ?

Yes Mark that is good. Thanks very much to both of you, apologies for my ineptitude.