[SOLVED]void setup() is somehow reseting

Hi everyone,

I’m running this sketch to read a txt file and then filling an array.

//Other normal stuff (that always worked)


char mixer_position[9][5];
void setup()   /*----( SETUP: RUNS ONCE )----*/
{
  Serial.begin(9600);  // Used to type in characters
  int result = memoryTest();
  Serial.print("Memory test results: ");  //RETURN 1353 bytes free
  Serial.print(result,DEC);
  Serial.println(" bytes free");
  lcd.begin(16,2);   // initialize the lcd for 16 chars 2 lines, turn on backlight
  lcd.backlight(); // finish with backlight on  
  lcd.clear();
  lcd.setCursor(0,0); 
  lcd.print("Arduibar");
  lcd.setCursor(0,1); 
  lcd.print("Initialisation");
 Serial.println("Initialisation");
  pinMode(outPin_AirSupply, OUTPUT);
  pinMode(outPin_M1_sens, OUTPUT); 
  pinMode(outPin_M1_step, OUTPUT);
  pinMode(outPin_M2_sens, OUTPUT); 
  pinMode(outPin_M2_step, OUTPUT);
  pinMode(inPin_end_rail, INPUT); 
  //Initialisation des pins des relais
  int inMin = 30; 
  int inMax = 44; 
  for(int i=inMin; i<=inMax; i++)
  {
    pinMode(i, OUTPUT);
    digitalWrite(i, HIGH);
  }

  //***************************************  RÉCUPÉRATION DES DONNÉES   *******************************
  if (Ethernet.begin(mac) == 0) {
    lcd.clear();
    lcd.setCursor(0,0); 
    lcd.print("Aucune connexion internet");
    delay(1000);
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }
  
  char c, array[20];
  int counter, counter2;
  int limiteur,id_will_be, previous_id;

  //Création de la liste nominative des bouteilles par id
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /mixerP.txt HTTP/1.1"); //download text ~
    client.println("Host: s345700206.onlinehome.us");
    client.println(); //end of get request

  } 
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }
  counter=0;
  counter2=0;
  limiteur = 0;
  id_will_be = 0;
  previous_id = 0;
  delay(5000);
  // if the file is available, read it:
  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
   // delay(5);
    c = client.read();
    Serial.print(c);
    if(c=='|')
    {
      if (counter >0)
      {
        limiteur++;
        counter2++;
        counter=0;
        if(limiteur <= 1)
        {
          //First value => array index
          id_will_be = atoi(array);
        }
        else if (limiteur == 2) // Second time a | si found
        {
          //Second value => Name of the bottle
          strcpy(mixer_position[id_will_be],array);
          limiteur = 0;      
        }
      }
    }        
    else
    {
      array[counter++] = c;
      array[counter] = 0;
    } 
  } 
  client.flush();
  client.stop();

  delay(5000);
   Serial.println("disconnecting.");
  Serial.println(mixer_position[4]);
  delay(5000);
  
  delay(5000);
  Serial.println("Alignement du plateau") ;
delay(5000);
  lcd.clear();
  lcd.setCursor(0,0); 
  lcd.print("Initialisation");
  lcd.setCursor(0,1); 
  lcd.print("Alignement du plateau");
 

  delay(5000);
   Serial.println("End setup") ;
   result = memoryTest();
  Serial.print("Memory test results: ");
  Serial.print(result,DEC);  //RETURN 1353 bytes free
  Serial.println(" bytes free");
   delay(5000);
}

Content of the txt file, nothing special, but the second value can be higher (say 99999 max) but never a float.

0|1|
1|2|
2|3|
3|4|
4|5|
5|6|
6|7|
7|8|
8|9|
9|10|

The loop is executed to the end (with and without delay) and then “reboot” to the first line of setup (or probably the first line of the script)

If I remove the Client part after “RÉCUPÉRATION DES DONNÉES” (french) everything work fine.

I’m using a Mega 2560. I just don’t see where is the wrong usage…

Thanks!

L.

char mixer_position[9][5];

Aren't you reading 10 lines?

Ah, declaring number of index and not index max…

Correct it, but still rebooting :confused:

Ethernet.begin() is suspect.

What is connected to the Arduino and how is it being powered? If the power supply is not sufficient for both the Arduino and the external device, that would probably cause a reboot.

Serial.println("Alignement du plateau") ;

Get all these strings out of RAM.

9|10|

Are you copying those strings into the array? (it sort-of looks like it).

char mixer_position[9][5];

In that case you haven’t allowed enough again. 5 doesn’t allow for the trailing null-terminator.

jremington:
Ethernet.begin() is suspect.
What is connected to the Arduino and how is it being powered? If the power supply is not sufficient for both the Arduino and the external device, that would probably cause a reboot.

Arduino is powered by a 12v 17A power supply… (I even disconnected everything else to be sure)

AWOL:

Serial.println("Alignement du plateau") ;

Get all these strings out of RAM.

Done, did see a difference in “time” but still rebooting

Good point! I change the “char” to an Int since the data will always be a Int and changed strcopy() to use atoi(). It did work for that part.

In total I will be reading 4 differents configuration file (created by PHP on a webpage to configure/declare the physical part of the machine).

So with only reading the two “Int” file like this (fonctionnal) :

int mixer_position[10][3];
void setup()   /*----( SETUP: RUNS ONCE )----*/
{

//See post #1 for the other code

//***************************************  RÉCUPARION DES DONNÉES   *******************************

//Création de la liste nominative des jus par id
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection
    client.println("GET /mixerL.txt HTTP/1.1"); //download text ~
    client.println("Host: s345700206.onlinehome.us");
    client.println(); //end of get request
    lcd.clear();
    lcd.setCursor(0,0); 
    lcd.print("Arduibar");
    lcd.setCursor(0,1); 
    lcd.print("Initialisation 2/4");
  } 
  else {
    lcd.clear();
    lcd.setCursor(0,0); 
    lcd.print("Connexion échoué");
  }

  counter=0;
  counter2=0;
  limiteur = 0;
  id_will_be = 0;
  previous_id = 0;
  // if the file is available, read it:
  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
   // delay(5);
    c = client.read();

    if(c=='|')
    {
      if (counter >0)
      {
        limiteur++;
        counter2++;
        counter=0;
        if(limiteur <= 1)
        {
          //First value => array index
          id_will_be = atoi(array);
          if(previous_id + 1 != id_will_be) {
            for(int j = previous_id + 1; j < id_will_be; j++){
              strcpy(mixer_name[j],0);
            }
          }
          previous_id = id_will_be;
        }
        else if (limiteur == 2) // Second time a | si found
        {
          //Second value => Name of the bottle
          strcpy(mixer_name[id_will_be],array);
          limiteur = 0;      
        }
      }
    }        
    else
    {
      array[counter++] = c;
      array[counter] = 0;
    } 
  } 
  client.stop();


}

Here is the part to get and fill the char array:

char bottle_name[20][25];
void setup()   /*----( SETUP: RUNS ONCE )----*/
{

//See post #1 for the other code

//***************************************  RÉCUPARION DES DONNÉES   *******************************
char c, array[25];
  int counter, counter2;
  int limiteur,id_will_be, previous_id;

  //Création de la liste nominative des bouteilles par id
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection

    client.println("GET /bottleL.TXT HTTP/1.1"); //download text ~
    client.println("Host: s345700206.onlinehome.us");
    client.println(); //end of get request
    lcd.clear();
    lcd.setCursor(0,0); 
    lcd.print("Arduibar");
    lcd.setCursor(0,1); 
    lcd.print("Initialisation 1/4");
  } 
  else {
    lcd.clear();
    lcd.setCursor(0,0); 
    lcd.print("Connexion échoué");
  }
  counter=0;
  counter2=0;
  limiteur = 0;
  id_will_be = 0;
  previous_id = 0;

  // if the file is available, read it:
  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    //delay(5);
    c = client.read();
    if(c=='|')
    {
      if (counter >0)
      {
        limiteur++;
        counter2++;
        counter=0;
        if(limiteur <= 1)
        {
          //First value => array index
          id_will_be = atoi(array);
          previous_id = id_will_be;
        }
        else if (limiteur == 2) // Second time a | si found
        {
          //Second value => Name of the bottle
          strcpy(bottle_name[id_will_be],array);    //Cause of the reboot when the setup reach the end, if I put it in comment, the sketch go to the main loop
          limiteur = 0;      
        }
      }
    }        
    else
    {
      array[counter++] = c;
      array[counter] = 0;
    } 

  } 
  client.stop();

//

Char file is this:

1|White Rhum|
2|Dark Rhum|
3|Vodka|
4|Maraschino|
5|Tequila|
6|Southern Comfort|
7|Gin|
8|Cognac|
9|Malibu Rhum|
10|Triple Sec|
11|Amaretto|
12|Blue Curacao|

As said in the sketch, if I remove the strcpy line everything work, so we can assume that it is the problem…

Still trying everything that i found on Google.

Thanks for the help!

L.

 client.println("GET /bottleL.TXT HTTP/1.1");

Constant strings should be in flash.

AWOL:

 client.println("GET /bottleL.TXT HTTP/1.1");

Constant strings should be in flash.

Just finish to add alll my string type with PROG_MEM, still rebooting.

Later tonight I will post all my sketch, I trying to clear it out.

Thanks again!

L.

          //Second value => Name of the bottle
          strcpy(bottle_name[id_will_be],array);

What value is in id_will_be? It may not be what you think it is.

What value is in array? It may not be what you think it is.

Is this what your SRAM memory test returns?

Serial.print("Memory test results: "); //RETURN 1353 bytes free

That isn't much left, especially since you haven't started the lcd or the ethernet services yet.

If this is a Mega, where is the other 5600 6600 bytes used?

//Other normal stuff (that always worked)

Maybe you should post all your sketch, including the "normal stuff".

Hi everyone!

I've focus myself on the RAM problem and after clearing/optimazing my sketch (and making order) everything work fine now!

Thanks again!