Program freezes/ inputs stop working no compiling errors in code

Well, Im stumped, Generally I can figure out most arduino issues by googling around a bit but I’ve stumped myself on this one, I’ve got a large project that I’m working on, hardware consists of:

Mega 1280
8 hobby servos
adafruit i2c servo board
2x sparkfun Hbridge dc motor drivers
4x 6v dc motors
4 input slide pots
8 input buttons (2 red and 2 black on each side L and R)

1 i2c LCD screen
1 small i2c Oled screen
All driven off a 12 motorcycle battery stepped down to 6v with a voltage converter

So the code below is a bit verbose, I had written out a version using arrays and for loops but this felt better for isolating the issues, essentially the program will run great for about 1-minutes then the 4x20 LCD shows some funky overlayed characters and all inputs and outputs stop, whatever state the button was in (1 or 0) that character stays on the LCD. I figured I may have setup one of the arrays wrong and was overwriting into some other part of the memory, but this version has all the arrays removed. Everything compiles fine, but the code wont run. I’ve tried changing the end delay, other than that, I’m stumped. I’m not particularly good with programming on arduino boards in general so it may be something obvious I’ve missed

I figure the problem is somewhere in this if statement, but I’ve attached the full file too

int LR1 = digitalRead(LRED1); //read left red button one

  if ((LR1 == LOW) && (state1 == 0) && (currentMillis - previousMillis1 >= 500)) {
    state1 = 1;

    motor1.drive(-254);
    previousMillis1 = currentMillis; // freeze counter previousmillis1
   }

  else if (( state1 == 1) && (currentMillis - previousMillis1 < 500)) {

    motor1.drive(-254);
    }

  else if (( state1 == 1) && (currentMillis - previousMillis1 >= 500)) {
 
    motor1.brake();
    state1 = 2;
    }

   else if ((LR1 == HIGH) && (state1 == 2) && (currentMillis - previousMillis2 >= 500)) {
 
     motor1.drive(254);
    previousMillis2 = currentMillis;//Start second counter (opening)
    state1 = 0;
    }

  else if ((LR1 == HIGH) && (state1 == 0) && (currentMillis - previousMillis2 < 500)) {

    motor1.drive(254);
    }

  else if ((LR1 == HIGH) && (currentMillis - previousMillis2 >= 1000) && (currentMillis - previousMillis2 < 2000) ) {
    motor1.brake();
    }

  else {
    }

Full code is too long to paste here so file is attached

Octo_Testing_Code_V1.6.ino.ino (13 KB)

Are the servos powered from the Mega 5V pins or directly from the external battery ?

Servos are all from the 6v converter and go directly into the servo shield, (this is the shield I'm using: LINK ) same with the led backlight on the lcd and the motor input for the hbridge.

the arduino is powered via the Vin pin on the shield

The 5v output on the arduino only powers the little OLED display

Useless white space like this makes me want to stop reading

  else {
    }











  int LB1 = digitalRead(LBLACK1);

You declare and never use this. What kind of flakes are you displaying?

#define NUMFLAKES 10

What do previousMillis1 and currentMillis mean to you? They mean nothing to me.

state1? Less than useless.

int readpot0;   //Left middle finger
int readpot1;    //Left index finger

Are these pin number or values read from some pin? Why not use meaningful names so you don't need the comments?

int middleFingerValue; // No comment needed.

middleFingerValue = analogRead(middleFingerPin); // Completely obvious

Better yet, don't use global variables when local variables will suffice.

int state1 = 0;  //Right lower state and so on,  stores position of the claws
int state2 = 0;  //Right lower state and so on,  stores position of the claws
int state3 = 0;  //Right lower state and so on,  stores position of the claws
int state4 = 0;  //Right lower state and so on,  stores position of the claws

4 variables for the same thing?

Why is the position stored in a variable called state? Do you store the state of switches in variables called position?

  int RR1 = digitalRead(RRED1); //read right red button one




  int RR2 = digitalRead(RRED2); //read right red button 2

One, 2, Three, 4, Five?

Yes, some of the comments and other bits are leftover from other segments that have been removed. I suppose I forgot to mention: the code is a little rough around the edges, naming is not intuitive and there are some redundancies. If the comments and naming are too confusing to diagnose any problem at this stage then I will clean things up and repost. Thank you for the assistance.

If the comments and naming are too confusing to diagnose any problem at this stage then I will clean things up and repost.

Personally, I think that would be a vary good idea. Perhaps in cleaning stuff up, you'll discover what the problem really is. For now, it is hard to follow your code without spending a lot of time studying it.

Code should be relatively easy to follow. You will, someday, look back at old code an say "what the hell was that idiot thinking? Oh, wait, I was that idiot". Make that a long time in the future, if you can.

Is the Mega power coming from 12V (OK) or 6V (no good)?