Elevator and Conveyor System

Greetings,
My first Arduino project, I think I took on an extra large challenge here.
I’ve built an elevator to transport pallets of products from 1st floor to 2cd floor, automatically off load the
pallets onto a conveyor, which automatically activates using sensors. The conveyor is 100 feet long and will
hold a complete container load of pallets.(40 pallets) At the end of the conveyor is a manually operated turn
table for turning the pallets in a position that a forklift can pick them and stock them in the racking.
The sketch I have so far works fine on the bread board. But now I have to add 4 more relays and three more
sensors, obviously I’m out of pins. I’ve been reading and studying the 74HC595 for more output or the CD4021B
for more input. But, I’m not sure how to fit it into my code. Could someone point me in the right direction.
Maybe a short snip of code or something.
This is what I got. It’s messy but it’s working. Had to leave out some code, over 9500 characters

/*..
  Labrada Distributors Elevator and Conveyor Control Unit
  created 2012
  by T.Hait 
..*/

/*..Constants won't change..*/

const int buttonPin[] = {2,3,4,5,6,7,8,9,10,11,12,14}; // pushbutton and limit switch pins

/*..Integer variables will change..*/

int buttonState = 0;// variable for reading the pushbutton status

  #define RELAY_ON 0 //For Opto Isolated Relay Module
  #define RELAY_OFF 1
  
// 2 - 8 Relay 10A Cap. Modules are operating 16 125V .8A Relays  
  #define Relay1  15 //Main Pump
  #define Relay2  16 //Pressure Release Valve
  #define Relay3  17 //Tractor Feed Motor
  #define Relay4  18 //Swing Pump
  #define Relay5  19 //Swing left solenoid Valve
  #define Relay6  20 //Swing Right Solenoid Valve
  #define Relay7  21 //Conveyor Motor #1
  #define Relay8  22 //Conveyor Motor #2
  #define Relay9  23 //Conveyor Motor #3
  #define Relay10  24 //Conveyor Motor #4
  #define Relay11  25 //Conveyor Motor #5
  #define Relay12  26 //Conveyor Motor #6
  #define Relay13  27 //Conveyor Motor #7
  #define Relay14  28 //Conveyor Motor #8
  #define Relay15  29 //Conveyor Motor #9
  #define Relay16  30 //Main Power Shut Down
   //Need to add 4 more relays here
   
/*..Pallet and Conveyor Control..*/
  //Sensors -- E18-D80NK -- 5V 

  #define sensor1  31  //Conveyor 1
  #define sensor2  32
  #define sensor3  33 //Conveyor 2 
  #define sensor4  34
  #define sensor5  35  //Conveyor 3
  #define sensor6  36
  #define sensor7  37 //Conveyor 4 
  #define sensor8  38
  #define sensor9  39 //Conveyor 5 
  #define sensor10  40
  #define sensor11  41  //Conveyor 6
  #define sensor12  42
  #define sensor13  43  //Conveyor 7 
  #define sensor14  44
  #define sensor15  45  //Conveyor 8 
  #define sensor16  46
  #define sensor17  47  //Conveyor 9
  #define sensor18  48
  // Need to add 3 more sensors here
  #define sensor19  49  //Turn Table Sensor, When activated all Conveyor Motors Stop, Conveyor is full


void setup()
{
  
/*..Make sure all relays are inactive at Reset..*/

  digitalWrite(Relay1, RELAY_OFF);
  digitalWrite(Relay2, RELAY_OFF);
  digitalWrite(Relay3, RELAY_OFF);
  digitalWrite(Relay4, RELAY_OFF);
  digitalWrite(Relay5, RELAY_OFF);
  digitalWrite(Relay6, RELAY_OFF);
  digitalWrite(Relay7, RELAY_OFF);
  digitalWrite(Relay8, RELAY_OFF);
  digitalWrite(Relay9, RELAY_OFF);
  digitalWrite(Relay10, RELAY_OFF);
  digitalWrite(Relay11, RELAY_OFF);
  digitalWrite(Relay12, RELAY_OFF);
  digitalWrite(Relay13, RELAY_OFF);
  digitalWrite(Relay14, RELAY_OFF);
  digitalWrite(Relay15, RELAY_OFF);
  digitalWrite(Relay16, RELAY_OFF);
  delay(4000);

/*..Define Relay Pins..*/

  pinMode(Relay1, OUTPUT);
  pinMode(Relay2, OUTPUT);
  pinMode(Relay3, OUTPUT);
  pinMode(Relay4, OUTPUT);
  pinMode(Relay5, OUTPUT);
  pinMode(Relay6, OUTPUT);
  pinMode(Relay7, OUTPUT);
  pinMode(Relay8, OUTPUT);
  pinMode(Relay9, OUTPUT);
  pinMode(Relay10, OUTPUT);
  pinMode(Relay12, OUTPUT);
  pinMode(Relay13, OUTPUT);
  pinMode(Relay14, OUTPUT);
  pinMode(Relay15, OUTPUT);
  pinMode(Relay16, OUTPUT); 
  
/*..Define Sensor Pins..*/

  pinMode(sensor1, INPUT); 
  pinMode(sensor2, INPUT);
  pinMode(sensor3, INPUT);
  pinMode(sensor4, INPUT);
  pinMode(sensor5, INPUT);
  pinMode(sensor6, INPUT);
  pinMode(sensor7, INPUT);
  pinMode(sensor8, INPUT);
  pinMode(sensor9, INPUT);
  pinMode(sensor10, INPUT);
  pinMode(sensor11, INPUT);
  pinMode(sensor12, INPUT);
  pinMode(sensor13, INPUT);
  pinMode(sensor14, INPUT);
  pinMode(sensor15, INPUT);
  pinMode(sensor16, INPUT);
  pinMode(sensor17, INPUT);
  pinMode(sensor18, INPUT);
  pinMode(sensor19, INPUT);

/*..Initialize the pushbutton pins as an input..*/

  for(int x=0; x<14; x++)
    {
    pinMode(buttonPin[x], INPUT);
    }
    
 }

void loop()
{
  
/*..Read the state of the pushbuttons value..*/

  for(int x=0; x<14; x++)
  {
      {
      buttonState = digitalRead(buttonPin[x]);
      }
      
/*..If button is pressed then buttonState is HIGH..*/
   
    if (buttonState == HIGH && buttonPin[x] == 2)//Up Button press
      {    
      digitalWrite(Relay1, LOW); //Activate Main Pump UP
      digitalWrite(Relay2, HIGH);//Pressure Release Valve OFF
      }
      
/*..Limit Switch Up-Stop..*/

    if (buttonState == HIGH && buttonPin[x] == 3)//Up Stop
      { 
      digitalWrite(Relay1, HIGH);//Limit Switch Main Pump Off
      digitalWrite(Relay2, HIGH);//Pressure Release Valve OFF
      }
    
    //Down Button press
    if (buttonState == HIGH && buttonPin[x] == 4)//Down
      {
      digitalWrite(Relay2, LOW);//Pressure Release Valve ON
      digitalWrite(Relay1, HIGH);// Main Pump Off
      }
      
/*..Limit Switch Down-Stop..*/
    
    if (buttonState == HIGH && buttonPin[x] == 5)//Down Stop
      {
      digitalWrite(Relay2, HIGH);//Pressure Release Valve OFF
      digitalWrite(Relay1, HIGH);// Main Pump Off
      }
      
 /*..Emergency Stop..*/
    
    if (buttonState == HIGH && buttonPin[x] ==14)//Emergency Stop
      {
      digitalWrite(Relay1, HIGH);//Main Pump Off
      digitalWrite(Relay2, HIGH);// Pressure Release Valve OFF
      }     
      
      
    
     
/*..Tractor Feed..*/
   
    if (buttonState == HIGH && buttonPin[x] == 6)//Tractor Feed Limit Switch mounted on elevator mast - Motor Start
      {
      delay(2000);  
      digitalWrite(Relay3, LOW);//Activate Tractor Feed Motor
      } 
    if (buttonState == HIGH && buttonPin[x] == 7)//Limit Switch Motor Stop, mounted on Tractor Feed drive chain
      {
      digitalWrite(Relay3, HIGH); //De-activate Tractor Feed Motor
      delay(10000);//10 second delay before sending elevator down
      digitalWrite(Relay2, LOW);//Pressure Release Valve ON - Down
      }
/*..Pallet Swing table..*/ 

  //Swing Left........
  
    if (buttonState == HIGH && buttonPin[x] == 8)//Swing Left Button press
      {
      digitalWrite(Relay4, LOW );//Swing Pump - ON
      digitalWrite(Relay5, LOW );//Swing Left Solenoid Valve - ON
      }
    if (buttonState == HIGH && buttonPin[x] == 9)//Swing left limit switch - Stop
      {
      digitalWrite(Relay4, HIGH);//Swing Pump - OFF
      digitalWrite(Relay5, HIGH);//Swing Left Solenoid Valve - OFF
      }
      
   //Swing Right..
    
     if (buttonState == HIGH && buttonPin[x] == 10)//Swing Right Button
      {
      digitalWrite(Relay4, LOW);//Swing Pump - ON
      digitalWrite(Relay6, LOW);//Swing Right Solenoid Valve - ON  
      }
     if (buttonState == HIGH && buttonPin[x] == 11)//Swing Right limit switch - stop
      {
      digitalWrite(Relay4, HIGH);//
      digitalWrite(Relay6, HIGH);//
      }
      
/*..Move Pallets one Space..*/

     if (buttonState == HIGH && buttonPin[x] == 12)//Move Next Space Button
      {
      digitalWrite(Relay16, LOW);//Connected to all Motor relays
      }
      else 
      { 
      digitalWrite(Relay16, HIGH);//
      }
 
/*==Sensors are placed 4" apart for proper pallet spacing on the conveyor==*/

/*..Conveyor #1 Sensor 1-2..*/

  if (digitalRead(sensor1) == HIGH  && digitalRead(sensor2) == HIGH)
     {
     digitalWrite(Relay7, HIGH);
     }
    else 
     {
     digitalWrite(Relay7, LOW);
     }
     

  }
}

I think it may be better to use something like this instead:

http://ww1.microchip.com/downloads/en/devicedoc/21952b.pdf

https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library

http://forums.adafruit.com/viewtopic.php?f=25&t=28254

I would either use an IO expander like the MCP23017/8 or get a bigger Arduino.

The conveyor is 100 feet long

Will you have sensors 100 feet away?


Rob

Thank you guys for your response, No, The control box is mounted in the center, longest run maybe 60' for the sensors. Motor relays maybe 2'.