Arduino Uno & Mega hängt sich nach ca 20-30 Sekunden auf

hallo Forum,
ich bin ein Neuling in Arduino Sachen, und momentan fleißig am Lernen.
Ich habe mir ein Arduino Uno und ein Mega und ein Ardafruit TFT Touch Shield 2.8" gekauft.
Ich würde damit gerne einen kleinen PC-Info Display machen. Das klappt auch alles so weit, auf
dem PC (Windows) läuft ein C# Programm das mit dem USB Serial mit dem Arduino verbunden ist.
Am Anfang geht auch alles ganz gut und beide verbinden sich und geben gegenseitig Feedback, aber nach kurzer zeit (ca. 20-40 Sekunden) hängt sich das Board auf oder stürzt ab.

Ich poste euch mal den Code hier. Hab schon viel gegooglet leider nichts gefunden... Wäre lieb wenn ihr mich mal auf meine Fehler aufmerksam macht. Danke! :slight_smile:
(mehrer Tabs da übersichtlicher)

pcinfo: (hauptTab)

#include "TouchScreen.h"
#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library#
#include <SPI.h>
#include <SD.h>
#include <stdint.h>

#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0

#define LCD_RESET A4 // reset
#define YP A1  // must be an analog pin, use "An" notation!
#define XM A2  // must be an analog pin, use "An" notation!
#define YM 7   // can be a digital pin
#define XP 6   // can be a digital pin
#define MINPRESSURE 100
#define MAXPRESSURE 1000

//16 bit color value
#define LIGHTGREY 0xD69A 
#define BLACK 0x0000
#define GRAY 0x8410 
#define WHITE 0xFFFF
#define RED 0xF800
#define GREEN 0x07E0

Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET); //Libary
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

#ifdef __SAM3X8E__
  #define TS_MINX 125
  #define TS_MINY 170
  #define TS_MAXX 880
  #define TS_MAXY 940
#else
  #define TS_MINX  150
  #define TS_MINY  120
  #define TS_MAXX  920
  #define TS_MAXY  940
#endif

unsigned char maxcpu = 100,aktcpu = 0;
String sysname = "";

/***EXTERN***/
extern unsigned char hour;
extern unsigned char minute;
extern unsigned char second;
extern int milliseconds;
extern unsigned long long int lastTime;
/***********/

//BOUNDS: 10,0,260,240

void setup()
{
  tft.reset();
  uint16_t identifier = tft.readID();
  tft.begin(identifier);
  tft.fillScreen(BLACK);
  Serial.begin(9600);
  Serial.println("start");
  int wt = 0;
  while(Serial.available()== 0 && wt <= 5000){wt++;delay(1);}
  delay(5);
  if(readSerial().indexOf("ok")>=0)
  {
    writetoConsole("Connected successfull!");
    getSysInfo();
  }
  else
  {
    writetoConsole("Can't conect to System!");
  }
  lastTime = millis();
  
}

void loop()
{
  
  if(Serial.available()>0)
  {
    delay(8);
    String input = readSerial();
    if(input.indexOf("time")>=0)
    {
      while(Serial.available() == 0);
      delay(8);
      syncTime(readSerial());
    }
    else if(input.indexOf("bye")>=0)
    {
      writetoConsole("Disconected from System!");
    }
    else
    {
      writetoConsole("Unknown command: '"+input+"'");
    }
    Serial.flush();
    input = 0;
  }
 updateTime(millis());
}

String readSerial()
{
    String ret = "";
    while(Serial.available() > 0)
    {
      ret = ret+(char)Serial.read();
    }
    return ret;
    Serial.flush();
    ret = 0;
}

void getSysInfo()
{
  Serial.println("time");
  while(Serial.available() == 0);
  delay(8);
  syncTime(readSerial());
  Serial.flush();
  delay(1);
  Serial.println("name");
  while(Serial.available() == 0);
  delay(8);
  sysname = readSerial();
  drawSystemName();
  delay(1);
  Serial.flush();
  Serial.println("cpu");
  while(Serial.available() == 0);
  delay(8);
  aktcpu = readSerial().toInt();
  drawCPU(maxcpu,aktcpu);
  Serial.flush();
}

DataProvider:

extern unsigned char second;

void syncStuff()
{
  if(second%5 == 0)getCPU();
}

void getCPU()
{
  
  Serial.println("cpu");
  while(Serial.available() == 0);
  delay(8);
  aktcpu = readSerial().toInt();
  drawCPU(maxcpu,aktcpu);
  Serial.flush();
}

int freeRam() {
  extern int __heap_start, *__brkval; 
  int v; 
  return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval); 
}

ScreenConsole:

String consoleMessages[5] = {"","","","",""};

void writetoConsole(String text)
{
  orderConsoleMessages();
  consoleMessages[0] = "> "+text;
  drawConsoleMessages();
}

void errorMessage(String text)
{
  tft.fillScreen(BLACK);
  tft.setRotation(1);
  tft.setCursor(15,100);
  tft.setTextSize(2);
  tft.setTextColor(WHITE);
  tft.println(text);
}

void orderConsoleMessages()
{
  for(int i = 4; i >= 0; i--)
  {
    consoleMessages[i+1] = consoleMessages[i];
  }
}

void drawConsoleMessages()
{
  if(tft.getRotation() != 0)tft.setRotation(0);
  tft.fillRect(0,260,240,60,BLACK);
  tft.setTextSize(1);
  for(int i = 4; i >= 0; i--)
  {
    tft.setCursor(5,265+(11*i));
    tft.println(consoleMessages[4-i]);
  }
}

drawinfo:

void drawSystemName()
{
  tft.setCursor(12,20);
  tft.setTextSize(2);
  tft.println(sysname);
}

void drawCPU(int maxv, int minv)
{
  tft.fillRect(11,44,80,10,BLACK);
  tft.setCursor(12,45);
  tft.setTextSize(1);
  tft.println("CPU: "+((String)(minv))+"/"+((String)(maxv)));
  tft.fillRect(20,55,(220/100)*maxv,15,GREEN);
  tft.fillRect(20,55,(220/100)*minv,15,RED);
  delay(3);
  writetoConsole("CPU usage updated: "+(String)minv+"/"+(String)maxv);
}

time:

unsigned char hour = 12;
unsigned char minute = 0;
unsigned char second = 0;
int milliseconds = 0;
unsigned long long int lastTime = 0;

void updateTime(int akttime)
{
  long int delaytime = akttime-lastTime;
  milliseconds+=delaytime;
  boolean updated = false;
  while(milliseconds > 1000)
  {
    second++;
    milliseconds-=1000;
    if(second > 59)
    {
      minute++;
      second-=60;
      if(minute > 59)
      {
        hour++;
        minute-=60;
        if(hour == 24)
        {
          hour = 0;
        }
      }
    }
    updated = true;
  }
  if(updated == true)drawTime();
  lastTime = akttime;
  syncStuff();
}

void drawTime()
{
  if(tft.getRotation() != 0)tft.setRotation(0);
  tft.fillRect(0,0,240,10,BLACK);
  tft.setCursor(1,1);
  tft.setTextSize(1);
  tft.setTextColor(WHITE);
  tft.println(((String)(int)hour)+":"+((String)(int)minute)+":"+((String)(int)second));
  tft.setCursor(160,1);
  tft.println("RAM: "+(String)freeRam());
}

void syncTime(String time)
{
  int in1 = time.indexOf(":");
  int in2 = time.indexOf(":",in1+1);
  hour = time.substring(0,in1).toInt();
  minute = time.substring(in1+1,in2).toInt();
  second = time.substring(in2+1).toInt();
  writetoConsole(getTimeString()+" - Time synced!");
  delay(2);
  updateTime(millis());
}

String getTimeString()
{
  return ((String)(int)hour)+":"+((String)(int)minute)+":"+((String)(int)second);
}

schönen Abend und Danke :slight_smile:

Marcel2508:

String consoleMessages[5] = {"","","","",""};

void orderConsoleMessages()
{
 for(int i = 4; i >= 0; i--)
 {
   consoleMessages[i+1] = consoleMessages[i];
 }
}

Offensichtlicher Indexfehler beim Zugriff auf das Array.

Bei i==4 greifst Du auf consoleMessages[4+1] zu, und das liegt offensichtlich um eins hinter Deinem deklarierten Array im Nirvana. Bzw. dort, wo im RAM irgendwas anderes wichtiges liegt.

Zugriffe auf Array-Indizes außerhalb der Gültigkeit sind eine gute Methode, um ein Programm abzuschießen.

Haha Ich bin sooo ein Hampel :astonished: :smiley:
sorry das ich das übersehen habe xD

hab jetzt die Zeilen so geändert:

void orderConsoleMessages()
{
  for(int i = 3; i >= 0; i--)
  {
    consoleMessages[i+1] = consoleMessages[i];
  }
}

schein zu funktionieren, wenn noch was ist meld ich mich :smiley: :slight_smile:

DANKE!