Limit switches

Hi, newbie alert :~

I’m trying to move a tray which has a limit switch at each end when an “O” is received over serial, my code is below - my problem is that the void trayout() section isn’t working as I want it to - I want to run the motor till the switch readings show the tray is ejected - but this isn’t happening at the moment, any advice on what I’m doing wrong? Thank you very much.

#include <AFMotor.h>

AF_DCMotor motor(3);              
const int ledPin =  13;               //LED used to indicate motor action, manually moving tray for testing to activate switches
const int trayoutPin = 4;           //pin 4 is 0 when tray is out, 1 when tray is coming in/in
const int trayinPin = 8;             //pin 8 is 0 when tray is in, 1 when tray is out/going out
int TRAYOUT = 'O';                   //we are looking for a "O" to eject tray
int trayoutState = 0;                //set initial switch values
int trayinState = 0;                  // "   "  

void setup() {
    pinMode(trayoutPin, INPUT);     
    pinMode(trayinPin, INPUT);
    pinMode(ledPin, OUTPUT);  
    Serial.begin(9600);
}

void loop() 
{
     if (Serial.available())  {
        int command = Serial.read();
        if (command == TRAYOUT)
               trayout();
     }
} 



void trayout()
{
  trayoutState = digitalRead(trayoutPin);
  Serial.print("tray out state: ");                                      //these serial printouts are just so I can see status of switches
  Serial.println(trayoutState);
  trayinState = digitalRead(trayinPin);
  Serial.print("tray in state: ");
  Serial.println(trayinState);
    if (trayoutState == 0 && trayinState == 1)
      {
      Serial.println("Tray out");
       }
    else 
      {         
      digitalWrite(ledPin, HIGH);
      Serial.println("tray ejecting");
      motor.run(FORWARD);
      motor.setSpeed(255);
      }
}

but this isn't happening at the moment

Well, now we know what isn't happening. If you now tell us what IS happening, and how that differs from what you want to happen, maybe we could offer some advice.

There are no digitalWrite() statements in the setup() function to activate the internal pullup resistors on the switch pins. The 0 and 1 conditionals (should be LOW and HIGH) imply that external pull-down resistors are wired with the switches. Are they? Do you KNOW that reading from the switches is correct?

Shouldn't there be some kind of while loop in the trayout() function, where you check the limit switches now and then, and actually shut off the motor?

Sorry, I should have given more detail - however you gave me the answer I needed which was the lack of a while loop, thanks! I do have external pull up resistors on the switches and the readings are correct - I've changed to this which works as I wish:

Thank you for your time taken to reply and set me on the right track :-)

void trayout()
{
  trayoutState = digitalRead(trayoutPin);
  trayinState = digitalRead(trayinPin);
    
    if (trayoutState == LOW && trayinState == HIGH){
      Serial.println("Tray out");
      digitalWrite(ledPin, LOW);
      motor.run(RELEASE);
      return;
    }
    
    else {
      while(trayoutState == HIGH)
      {      
        digitalWrite(ledPin, HIGH);
        Serial.println("Tray Ejecting");
        trayoutState = digitalRead(trayoutPin);
        digitalWrite(ledPin, HIGH);
        motor.run(FORWARD);
        motor.setSpeed(255);
        trayout();
       }
  }
}

Do you have any clue how many times trayout() will be called? There is absolutely no reason for trayout() to call itself.

In the function, start the tray moving. While neither limit switch is pressed, do nothing. Shut off the motor and return.