arduino freezes after 10 minutes with this program

I have always found everyone here so helpful especially for the beginners. I started out in the forum with almost no experience and now I can write my own program!!!! There is a small glitch because after about 10 minutes the program stops running (ie. no output to my relays) and will run again if I reload the program back onto the Uno. I suspect it is due to reading the analog input pins somehow. I read a similar post that said the Uno can run out of SRAM if not properly called. Any help would be so appreciated.

Mr. Moderator: I am so sorry if this was posted twice. I swear I posted Sunday but could not find it the next day.

/* 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
  PortPot = analogRead(PORTPOTPIN); //Port cylinder potentiometer
  JoyX = analogRead(JOYXPIN); //Joystick brake potentiometer
  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);
    }

  }

}

I read a similar post that said the Uno can run out of SRAM if not properly called.

Nonsense. Reading an analog pin does not use SRAM, except for the two bytes that you store the return value in.

Ok, thank you. Any help as if the software could be locking up the Uno over time in some way would be so helpful.

Any help as if the software could be locking up the Uno over time in some way would be so helpful.

Some Serial.print() statements in appropriate places will help you determine where the code appears to hang.

With, of course, Serial.begin() in setup().

When you say it freezes, you mean that it does not operate the relays ?
Is it possible it is getting stuck in an infinite loop like :

while (digitalRead(ESWITCHPIN) == LOW) {
. . .
}

How is the switch wired ? with a pull up/pull down resistor ?

Hmmm... the while loop does work and when a switch is "on" a pull up resistor has the program to just stay in the while loop, effectively working as an emergency stop for the program. But you are right the "lockup" it is as if this switch somehow is activated accidentally. (maybe there is a short somewhere???) I will add some print commands to try and debug the program and then report back!!!!!! Thank you everyone, great feed back and a place to start!!!!!

effectively working as an emergency stop for the program

An EMERGENCY STOP switch IS NOT READ BY CODE!

An emergency stop switch cuts power to the hardware!

Hand-waving about how the switch is wired doesn't cut it. A schematic is needed!