arduino board resets often

while im working with tft screen
my arduino mega board resets within 10 seconds.
all the values and printed data gone and start freshly.

how can i recover from this ?

#define LCD_CS A3 
#define LCD_CD A2   
#define LCD_WR A1   
#define LCD_RD A0   

#define LCD_RESET A4

#define   BLACK           0x0000
#define   BLUE            0x001F
#define   RED             0xF800
#define   GREEN           0x07E0
#define   CYAN            0x07FF
#define   MAGENTA         0xF81F
#define   YELLOW          0xFFE0
#define   WHITE           0xFFFF

#include "TFTLCD.h"
#include <Keypad.h>

const byte ROWS = 5;
const byte COLS = 4;
char buff[10];
long lastUpdate = 0;
int x,i;

TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

char keys[ROWS][COLS] =
{
  {'1','6','A','D'},
  {'2','7','B','E'},
  {'3','8','C','F'},
  {'4','9','X','Y'},
  {'5','0','Z','R'},
 
};

byte rowPins[ROWS] = {40,39,38,37,36}; //connect to row pinouts
byte colPins[COLS] = {32,33,34,35}; //connect to column pinouts

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(void)
{
  Serial.begin(9600);
  Serial.println("8 Bit LCD test!");
  tft.reset();
  tft.initDisplay();

}

void loop(void)
{
  char key = keypad.getKey();

  if (key != NO_KEY) 
  {
    buff[i] = key;
    i = i+1;
    x=x+8;
   
 }
  if((millis() - lastUpdate) > 700)  // has it been >1 second since the last update?
  {
    testtext(BLACK);  // update display
    lastUpdate = millis();
   // remember the time
  }

}

void testtext(uint16_t color)
{
  tft.fillScreen(CYAN);
  tft.setCursor(50, 20);
  tft.setTextColor(RED);
  tft.setTextSize(2);
  tft.println("Enter your ");
   tft.setCursor(50, 40);
  tft.println("Student ID");
  
 
  tft.setCursor(x, 60);
  tft.setTextSize(2);
  tft.print(buff);

}
char buff[10];
...
    buff[i] = key;
    i = i+1;

You've declared buff to have 10 components, but i is never set to anything but its value plus one, so after 10 key presses, you're outside the bounds of your buffer.

yes thats a fact :slight_smile: . i will limit them

but before i implement up to this point already it behaves like this. :frowning:

Can it be that you are drawing more power, than the system can support, so it goes to reset?
What's your power source, how much drawing off it?

jackwp:
Can it be that you are drawing more power, than the system can support, so it goes to reset?
What's your power source, how much drawing off it?

Also, make sure you aren't accidently connecting the Vcc (5V) pin to any input pin directly (without a resistor) because this resets the board.

jackwp
my power source is usb from labtop.
ill try on external DC power supply

charredgrass
ive connected Vcc ( 5 v) to tft screen's Vcc without any resister.
and that is the one and only connection im using 5v.