Sketch returning to setup()


I have written a sketch that I would like to debug and get to work. But I have encountered a strange problem. The setup() runs once like it’s supposed to, and the loop() starts, but then we are back to setup() again.

The setup is so far only an lcd screen on serial (d7), but eventually we will read serial from d0 and process gps data. Can the board reset due to a disturbance from something in the code?

here’s an extract:

/*Fore! A program created by Joachim Bakke with the help of the Arduino forum and many programs with functionalities included here. 
* Based on a csv file on an SD card, positions for the tees and greens are loaded.
* Current time, position and altitude is collected from strings of NMEA from a GPS connected to the arduino
* Distance to green (middle), last shot distance (and which club) is calculated and displayed.
* The shot lengths are recorded to the SD card for statistical analysis.

*Serial input from NMEA is on d4
*Serial output for NMEA is on d6
*Potmeter on d5
*Mark button is on d2 (interrupt 0)
*Next hole button on d3 (interrupt 1)
*SD card powered from d8?? Check if necessary, can be powered directly
*LCD screen on pin d7

#include <SoftwareSerial.h>  //For connecting the display to any digital pin
#include <SparkFun_SerLCD.h> //for the display
#include <EEPROM.h>          //for writing brightness value to EEPROM
#include <string.h>          //These two are for Checksum calculatino for the NMEA output
#include <ctype.h>
#include <avr/pgmspace.h>
#include <Flash.h>           //In order to use table for tee and green positions for each hole
#include <microfat2.h>        //These three are for writing to and reading from an SD card
#include <mmc.h>             //
#include <SDuFAT.h>          //
#include <wprogram.h>
#include "SDWriter.h"

//#define rxPin 4             //NMEA input pin for distance and RTC (SoftwareSerial)
//#define txPin 3             //not in use currently
#define ledPin 13
//SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
byte pinState = 0;

int byteNMEA=-1;                  // a byte of the NMEA stream cominig in on serial port
 char linea[300] = "";             // a string buffer that we write each byteNMEA to
int count=0;
 int check1 = 0;          //check for gga sentence
 int check2 = 0;          //check for rmc sentence
 int counta=0;
 int index[10];
 char HdrStr1[5] = "GGA";            // the header of the sentence we are trying to find to convert
 char HdrStr2[5] = "RMC";              // This nmea string includes tha date, which we need
 char Str1[10] = "";
 char Str2[10] = "";
 char Str3[10] = "";
 char Str4[10] = "";
 char Str5[10] = "";
 char Str6[10] = "";
// char Str7[10] = "";     
 //char Str8[10] = "";
 char Str9[10] = "";
 char Str10[10] = "";    //We use this for date

int FullDate = 0;
int FullTime = 0;    
int TimeZone = 2;    //UTC + 1 and daylight savings time
double RawLat = 0;
double RawLong = 0;
boolean ValidFix = false;

double TeeLat = 0;      //positions are stored as DDMM.mmm
double TeeLong = 0;
int TeeAlt = 0;
double LastPosLat = 0;  //For example on a par 5, it can be used to measure transport shots
double LastPosLong = 0;
int LastAlt = 0;
double CurrentPosLat = 5910.300;
double CurrentPosLong = 1050.100;
int CurrentAltitude = 0;
double GreenLat = 0;
double GreenLong = 0;
int GreenAlt = 0;
int dist = 0;

int CurrentHole = 0;
int ClubVicinity = 10000 ;    //Threshold for being in the vicinity of the golf course
int TeeVicinity = 20;         //Threshold for being close enough to the next tee to change hole

int potpin = 5;    // potmeter pin to dim lcd backlight
int potvalue;             // variable to keep the actual potmeter value 
int club = 0;

int MarkButtonState = 0;        //Variable to show if the "mark" button has been pressed
int ShotCount = 0;              
int NextButtonState = 0;        //Variable to show if the "next" button has been pressed

int StatButton = 0;            //Rocker switch for statistics/measurements
int statState = 0;

prog_char club_0[] PROGMEM = "Dr";
prog_char club_1[] PROGMEM = "3W";
prog_char club_2[] PROGMEM = "Hyb";
prog_char club_3[] PROGMEM = "3I";
prog_char club_4[] PROGMEM = "4I";
prog_char club_5[] PROGMEM = "5I";
prog_char club_6[] PROGMEM = "6I";
prog_char club_7[] PROGMEM = "7I";
prog_char club_8[] PROGMEM = "8I";
prog_char club_9[] PROGMEM = "9I";
prog_char club_10[] PROGMEM = "PW";
prog_char club_11[] PROGMEM = "GW";
prog_char club_12[] PROGMEM = "LW";
prog_char club_13[] PROGMEM = "N/A";
prog_char club_14[] PROGMEM = "N/A";

PROGMEM const char *string_table [] =
  club_14 };
char ClubSelection[30];  

//int bright = 0;              // Brightness of LCD Screen in %
int value = 0;               // Temporary variable for EEPROM

sf_LCD lcd = sf_LCD(7);       // desired LCD pin

//int addr = 0;   // The address to read/write in EEPROM (if needed)

FLASH_TABLE(double, hole_table, 6,    //Tee latitude,Tee longitude,Tee elevation,Green latitude,Green longitude,green elevation
{5922.119,1040.898,20,0,0,0},                   //"HOLE 0" is the club house         

SDWriter sd; // create an SDWriter

void setup()

 Serial.begin(4800); //in SD exapmple, the rate is 19200
 Serial.println("Start of Setup");
 //pinMode(rxPin, INPUT);    //NMEA input
 //pinMode(txPin, OUTPUT);
// attachInterrupt(0, mark, RISING);    //meaning the function mark() will be run once the mark button is pressed (d2)
// attachInterrupt(1, next, RISING);    //d3 button will change to next hole
 pinMode(potpin, INPUT);    //Potmeter is input
 //pinMode(MarkButton, INPUT);//re-written as interrupt
 //sd.init(PSTR("EVJESTAT CSV")); //open file evjestat.csv, 20KB should be plenty for this
 if(StatButton == HIGH){    //if the rocker is set to Statistics, move to loop
for (int i=0;i<300;i++){       // Initialize a buffer for received data
     linea[i]=' ';

  lcd.brightness(80);,1,"Joachim Bakke 09");,1,"   Fore! v0.2   ");

void loop()
  boolean ValidFix = false;//for testing
   if (ValidFix == false){
    lcd.empty();,1,"Joachim Bakke 09");,1,"Fore! v0.2 NoGPS");
  lcd.empty();,1,"Joachim Bakke 09");
  int UtcHour = FullTime / 10000;    //no need for seconds
  int minute = (FullTime - UtcHour)/100;
  int hour = UtcHour + TimeZone;,1,"Fore! v0.2 ");
  if (hour<=9){
   if (minute<=9){

if (FullDate== 0){    //Only need to run this once every time the arduino is powered, in order to get the date.
  FullDate = GetNMEADate();}
GetNMEA();    //Must search for close tee as long as the loop is run, in order to enter loop once it is near a tee.
 int distToClubHouse = 200;
  while (/*int*/ distToClubHouse /*= distancePyth(CurrentPosLat,hole_table[0][0],CurrentPosLong,hole_table[0][1])*/ <= ClubVicinity) {
   //Will kick out of the loop if not even close to club house
    for (int i= 1; i >= 18; i++){
      if (int dist = distancePyth(CurrentPosLat,hole_table[i][0],CurrentPosLong,hole_table[i][1]) >= TeeVicinity) {  
        CurrentHole = i; //

no more space to put code, sorry for the mess…

Best regards,

You are using a lot of RAM. Which Arduino do you have?

Hvilken Atmel µCU sitter i den? ;)


It's a 328, can the ram usage be the problem?


can the ram usage be the problem?

Yes. The Arduino will behave very erratically (like possibly rebooting) if you exceed available memory. From what I can tell, there's no "you used too much memory" check in the IDE. Which leaves it to you to ensure you haven't used too much memory.

In my case, I exceeded available RAM (data and stack). I was able to fix the problem by moving some things to PROGMEM.

Bear in mind there are other reasons why setup would be called a second time.

Good luck, Brian

Thanks guys (?),

It seems like RAM was indeed the reason for the setup “loop”. I halved the buffer and now the problems lie elsewhere.

Thanks again