Sketch returning to setup()

Hi,

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_0,
  club_1,
  club_2,
  club_3,
  club_4,
  club_5,
  club_6,
  club_7,
  club_8,
  club_9,
  club_10,
  club_11,
  club_12,
  club_13,
  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         
{5922.142,1040.941,20,5922.394,1040.968,21},
{5922.411,1041.005,22,5922.582,1040.963,28},
{5922.569,1040.873,22,5922.452,1040.642,11},
{5922.444,1040.561,9,5922.191,1040.374,9},
{5922.189,1040.276,18,5922.39,1040.334,17},
{5922.427,1040.366,18,5922.445,1040.501,10},
{5922.476,1040.562,9,5922.604,1040.857,23},
{5922.539,1040.885,24,5922.426,1040.89,22},
{5922.372,1040.826,18,5922.185,1040.872,18},
{5922.089,1040.909,20,5922.015,1040.869,22},
{5921.97,1040.883,28,5921.779,1040.819,28},
{5921.789,1040.943,31,5921.619,1040.979,46},
{5921.576,1040.974,55,5921.701,1040.827,33},
{5921.722,1040.7,22,5921.673,1040.594,11},
{5921.65,1040.482,6,5921.806,1040.573,20},
{5921.793,1040.498,9,5921.783,1040.162,17},
{5921.81,1040.219,13,5921.879,1040.544,11},
{5921.87,1040.593,14,5922.091,1040.856,17});

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);
 //mySerial.begin(4800);
 
// 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
 stat();
 }
 
for (int i=0;i<300;i++){       // Initialize a buffer for received data
     linea[i]=' ';
}

 lcd.setup();
 lcd.empty();
  lcd.brightness(80);
  lcd.at(1,1,"Joachim Bakke 09");
  lcd.at(2,1,"   Fore! v0.2   ");
  delay(2000);
 
GetNMEA(); //HER ER DET PROBLEMER ALLEREDE
  
} 

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

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,
Joachim

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

Hvilken Atmel µCU sitter i den? ;)

Heisann,

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

Joachim

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

Joachim