This is my first program and I would love criticism. The Uno also appears to freeze after 10-15 minutes. The UNO will only work if I reload the program. Can I be running out of SRAM somehow??? Any suggestions for debugging??? Thank you for any help.
/* This program uses an Arduino Uno at compares operator input from a potentiometer joystick to control the position of air cylinders via PWM proPortPotional valves with
  a positive feedback loop from potentiometers on the air cylinders.
*/
const byte AFTPOTPIN=A0;
const byte PORTPOTPIN=A1;
const byte JOYXPIN=A2;
const byte JOYYPIN=A3;
const byte ESWITCHPIN=4;
const byte VALVEPIN1=5;
const byte VALVEPIN2=6;
const byte RELAYPIN1=10;
const byte RELAYPIN2=11;
const byte RELAYPIN3=12;
const byte RELAYPIN4=13;
int AftPot = 0;
int PortPot = 0;
int PotDiff = 0;
int PotDiff1 = 0;
int JoyX = 0;
int JoyY = 0;
unsigned long previousMillis = 0;
void setup() {
 pinMode(ESWITCHPIN, INPUT); //estop switch
 pinMode(VALVEPIN1, OUTPUT);//PWM Valve#1
 pinMode(VALVEPIN2, OUTPUT);//PWM Valve#2
 pinMode(RELAYPIN1, OUTPUT);//relay#1
 pinMode(RELAYPIN2, OUTPUT);//relay#2
 pinMode(RELAYPIN3, OUTPUT);//relay#3
 pinMode(RELAYPIN4, OUTPUT);//relay#4
 //these are not needed only for reference
 // pinMode(AFTPOTPIN,INPUT);//AftPot cylinder potentiometer Input
 //pinMode(PORTPOTPIN,INPUT); //PortPot cylinder potentiometer Input
 //pinMode(JOYXPIN,INPUT); /JoyX joystick control input
 //pinMode(JOYYPIN,INPUT);//JoyY joystick control input
 digitalWrite(RELAYPIN1, HIGH); //High Turns off 4 channel relays - LOW to turn on
 digitalWrite(RELAYPIN2, HIGH);
 digitalWrite(RELAYPIN3, HIGH);
 digitalWrite(RELAYPIN4, HIGH);
}
void loop() {
 while (digitalRead(ESWITCHPIN) == LOW) {
  digitalWrite(RELAYPIN1, HIGH); //High Turns off 4 chananel relays - LOW to turn on
  digitalWrite(RELAYPIN2, HIGH);
  digitalWrite(RELAYPIN3, HIGH);
  digitalWrite(RELAYPIN4, HIGH);
 }//if switch is low do nothing but set relays to OFF
 analogRead(AFTPOTPIN);     //read first analog input potentiometer twice to settle DAC
 AftPot = analogRead(AFTPOTPIN); //read first analog input potentiometer twice to settle DAC
 delay(2);
 PortPot = analogRead(PORTPOTPIN); //Port cylinder potentiometer
 delay(2);
 JoyX = analogRead(JOYXPIN); //Joystick brake potentiometer
 delay(2);
 JoyY = analogRead(JOYYPIN); //Joystick throttle potentiometer
Â
 /*JoyX pot goes 0-850
  JoyY joystick 728-849
  gas joystick 700-850
  AftPot pot pneumatic cylinder 300-900
  PortPot pot penumatic cylinder 389-975
 */
 PortPot = constrain(PortPot, 266, 982); //needed to constrain actual max & min numbers come from the pots
 PortPot = map(PortPot, 266, 982, 0, 500);
 JoyX = constrain(JoyX, 530, 680);
 JoyX = map(JoyX, 530, 680, 0, 500);
 AftPot = constrain(AftPot, 400, 1000);
 AftPot = map(AftPot, 400, 1000, 0, 500);
 JoyY = constrain(JoyY, 428, 637);
 JoyY = map(JoyY, 428, 637, 0, 500);
 unsigned long currentMillis = millis();//this is a timing loop because the relays should fire no more than 20x/s.
 if (currentMillis - previousMillis >= 50) {
  previousMillis = currentMillis;
  /*below we take the difference from where we are (cylinder pots) to where we want to be (joystick pots) and
   send it to the PWM valves. The valves are proPortPotional so the bigger the
   difference the wider the PWM valves open. It is a non-linear equation so writing it
   with multiple if statements is easier to adjust in the field.
   /////////////////////////Left and Right//////////////////////////////
  */
  PotDiff = (PortPot - JoyX);
  if (PotDiff > 200) {
   digitalWrite(RELAYPIN3, HIGH);
   digitalWrite(RELAYPIN4, LOW);
   analogWrite(VALVEPIN2, 155);
  }
  if (PotDiff > 100 && PotDiff <= 200) {
   digitalWrite(RELAYPIN3, HIGH);
   digitalWrite(RELAYPIN4, LOW);
   analogWrite(VALVEPIN2, 120);
  }
  if (PotDiff > 20 && PotDiff <= 100) {
   digitalWrite(RELAYPIN3, HIGH);
   digitalWrite(RELAYPIN4, LOW);
   analogWrite(VALVEPIN2, 100);
  }
  if (PotDiff <= 20 && PotDiff >= -20) {
   digitalWrite(RELAYPIN3, HIGH);
   digitalWrite(RELAYPIN4, HIGH);
   analogWrite(VALVEPIN2, 0);
  }
  if (PotDiff < -20 && PotDiff >= -100) {
   digitalWrite(RELAYPIN3, LOW);
   digitalWrite(RELAYPIN4, HIGH);
   analogWrite(VALVEPIN2, 100);
  }
  if (PotDiff < -100 && PotDiff >= -200) {
   digitalWrite(RELAYPIN3, LOW);
   digitalWrite(RELAYPIN4, HIGH);
   analogWrite(VALVEPIN2, 130);
  }
  if (PotDiff < -200) {
   digitalWrite(RELAYPIN3, LOW);
   digitalWrite(RELAYPIN4, HIGH);
   analogWrite(VALVEPIN2, 145);
  }
  /////////////////AftPot- JoyY//////////////////////////////////////////////////
  PotDiff1 = (AftPot - JoyY );//)
  if (PotDiff1 > 200) {
   digitalWrite(RELAYPIN1, HIGH);
   digitalWrite(RELAYPIN2, LOW);
   analogWrite(VALVEPIN1, 125);
  }
  if (PotDiff1 > 100 && PotDiff1 <= 200) {
   digitalWrite(RELAYPIN1, HIGH);
   digitalWrite(RELAYPIN2, LOW);
   analogWrite(VALVEPIN1, 90);
  }
  if (PotDiff1 > 20 && PotDiff1 <= 100) {
   digitalWrite(RELAYPIN1, HIGH); //extend or JoyY (recover from go fast)
   digitalWrite(RELAYPIN2, LOW);
   analogWrite(VALVEPIN1, 80);
  }
  if (PotDiff1 <= 20 && PotDiff1 >= -20) {
   digitalWrite(RELAYPIN1, HIGH);
   digitalWrite(RELAYPIN2, HIGH);
   analogWrite(VALVEPIN1, 0);
  }
  if (PotDiff1 < -20 && PotDiff1 >= -100) {
   digitalWrite(RELAYPIN1, LOW);
   digitalWrite(RELAYPIN2, HIGH);
   analogWrite(VALVEPIN1, 80);
  }
  if (PotDiff1 < -100 && PotDiff1 >= -200) {
   digitalWrite(RELAYPIN1, LOW); //retract or go fast
   digitalWrite(RELAYPIN2, HIGH);
   analogWrite(VALVEPIN1, 100);
  }
  if (PotDiff1 < -200) {
   digitalWrite(RELAYPIN1, LOW); //retract or go fast
   digitalWrite(RELAYPIN2, HIGH);
   analogWrite(VALVEPIN1, 140);
  }
 }
}