295 th button press crashes my program and reset arduino

Hi my program uses menus and ive noticed every time on the 295th press of the button to scroll through the menus the lcd get garbled and the ardruino resets
thanks for any help
arduino uno or mega same result

That's interesting.

any ideas why i can post code and any other info if needed

Yes, that would be useful.

this is the main loop for the key press theres a lot more code and some of it is in tabs and iam not sure how to post it all in one go

#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 10 // DATA WIRE SENSOR IS PLUGGED INTO ARDRINO
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address

int LED = 12;//TEST RUNNING LED
int A = 0;
int TEMPONOFF = 1;
int PHONOFF = 1;
int LIGHTONOFF =1;
int CO2ONOFF = 1;
int CO2ONHOUR = 10;
int CO2ONMIN = 10;
int CO2OFFHOUR = 10;
int CO2OFFMIN = 10;
int LIGHTSONHOUR = 10;
int LIGHTSOFFHOUR = 10;
int LIGHTSONMIN = 10;
int LIGHTSOFFMIN = 10;
float CURRENTPH = 7; //CHANGE BACK TO 0
float HIGHPHALARM = 8;
float LOWPHALARM = 7;
float LOWALARM = 76;
float HIGHALARM = 80;
float STOREDTEMP = 78;
int MENUS = 0;
int SETHOUR = 0;
int SETMIN = 0;
int DELAY = 150;
int POINTER = 0;
int PRESSBUTTON1 = 0;
int PRESSBUTTON2 = 0;
const int BUTTON1 = 9; // the number of the pushbutton pin
const int BUTTON2 = 8; // the number of the pushbutton pin
int BUTTONSTATE1 = 0; // the current reading from the input pin
int BUTTONSTATE2 = 0; // the current reading from the input pin
int LASTBUTTONSTATE1 = LOW; // the previous reading from the input pin
int LASTBUTTONSTATE2 = LOW; // the previous reading from the input pin
long LASTDEBOUNCETIME1 = 0; // the last time the output pin was toggled
long LASTDEBOUNCETIME2 = 0; // the last time the output pin was toggled
long DEBOUNCEDELAY1 = 50; // the debounce time; increase if the output flickers
long DEBOUNCEDELAY2 = 50; // the debounce time; increase if the output flickerS

//------------------------------------------------------------------------------

void setup()
{
pinMode(LED, OUTPUT);
pinMode(BUTTON1, INPUT);
pinMode(BUTTON2, INPUT);
Serial.begin(9600);
lcd.begin(20,4);
setTime(9,12,12,1,1,2017);
SETMIN = minute();
SETHOUR = hour();
}

//------------------------------------------------------------------------------

void loop()
{
STARTDISPLAY();
}

void STARTDISPLAY()
{
MAINMENU();
}

//----------- PRESS BUTTON 1 --------------------

void PUSHBUTTON()
{

digitalWrite (LED,HIGH);//RIGHT THE TEST LIGHT ON
delay(20);
digitalWrite (LED,LOW);//RIGHT THE TEST LIGHT OFF
delay(20);
PRESSBUTTON1 = digitalRead(BUTTON1);

if (PRESSBUTTON1 != LASTBUTTONSTATE1)
{
LASTDEBOUNCETIME1 = millis();
}

if ((millis() - LASTDEBOUNCETIME1) > DEBOUNCEDELAY1)
{

if (PRESSBUTTON1 != BUTTONSTATE1)// if the button state has changed:
{
BUTTONSTATE1 = PRESSBUTTON1;

if (BUTTONSTATE1 == HIGH)
{
A=A+1;
Serial.print(A);
Serial.println();
POINTERSCROLL();
}
}
}
LASTBUTTONSTATE1 = PRESSBUTTON1;

SELECTMENU();
}

//PRESS BUTTON 2 ----------------------------------------------------------------

void SELECTMENU()
{
PRESSBUTTON2 = digitalRead(BUTTON2);

if (PRESSBUTTON2 != LASTBUTTONSTATE2)
{
LASTDEBOUNCETIME2 = millis();
}

if ((millis() - LASTDEBOUNCETIME2) > DEBOUNCEDELAY2)
{

if (PRESSBUTTON2 != BUTTONSTATE2)// if the button state has changed:
{
BUTTONSTATE2 = PRESSBUTTON2;

//------- MENU 3 CHANGE TIME --------------------------------------

if (BUTTONSTATE2 == HIGH && MENUS == 3 && POINTER == 1)
{
SETHOUR = SETHOUR + 1;
MAINMENU();
}

iam not sure how to post it

In code tags.

Show us your strings and any buffer manipulation

Difficult to be sure without more code but looks like a stack overflow to me. PUSHBUTTON() calls SELCTMENU(); SELECTMENU calls MAINMENU()..... does MAINMENU() call PUSHBUTTON() again somewhere? If so you've got a re-entrant function which will guarantee a stack overflow if there's no limit to how many times it calls.

yes mainmenu then calls pushbutton i thought it might be a stack overflow but not sure how to fix it or if i can reset it before it overflows

You probably can't fix it without totally reorganising your logic. Re-entrant functions (functions that call themselves, either directly or indirectly) should only ever be used very carefully and for particular (very rare!) purposes.
To be sure if this is the problem, you could have a global counter which increments as the first line of Pushbutton() and decrements at the last line. If this counter keeps on growing (view it with a Serial output) then that's the problem. You could do 'something' if the counter gets to more than 10, say, but what would you do? There's nowhere to go.
So you need to re-structure so that a function doesn't call itself somewhere deeper in the code.

HOTSHOT:
yes mainmenu then calls pushbutton i thought it might be a stack overflow but not sure how to fix it or if i can reset it before it overflows

Since you have the same problem with both the UNO and MEGA2560, it's probably NOT a stack problem, since the MEGA has 4X the ram of the UNO...