Go Down

Topic: Call millis() before setup (Read 1 time) previous topic - next topic

cantore

ok thanks
PDE
Code: [Select]

#include <nodoGenerico.h>

//IMPOSTAZIONI
#define intervalloMinimoTraCampioni 2000 //180000//ritardo fra 2 campioni in millesecondi
#define sdPin 4                          //per lo stalker 10, per ethernet shield 4
#define coordnatorAddressMsb 0x0013a200
#define coordnatorAddressLsb 0x406163FD

//include locali

#include "DS1307.h" //i2c clock
#include <SD.h>
#include <XBee.h>
#include <Wire.h>
#include "WProgram.h"


const int nTentativi=3;
const int ritardoProve=2000;
uint8_t timestamp[7];


nodoGenerico provaNodo(0x0013a200, 0x406163FD, nTentativi, ritardoProve);

void setup() {   
  Serial.begin(9600);
  Serial.println("sono nel setup");
}

void loop() {
  Serial.println("inizio un loop");
 
  delay(intervalloMinimoTraCampioni);
}



the .cpp
Code: [Select]



/******************************************************************************
* Includes
******************************************************************************/
#include "nodoGenerico.h"
#include "WProgram.h" //Arduino includes
#include <SD.h>

/******************************************************************************
* Definitions
******************************************************************************/

//#define sdPin 4 //per lo stalker 10, per ethernet shield 4

/******************************************************************************
* Constructors
******************************************************************************/

nodoGenerico::nodoGenerico(uint32_t coordAddMsb, uint32_t coordAddLsb, int nTentativiParam, int ritardoProveParam)
{
Serial.begin(9600);


xbee = XBee();

addr64 = XBeeAddress64(coordAddMsb, coordAddLsb);
txStatus = ZBTxStatusResponse();

sdScritta=false;
sdPresente=false;
nTentativi=nTentativi;
ritardoProve=ritardoProve;

xbee.begin(9600);


//Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(10, OUTPUT);


if (!SD.begin(sdPin)) {
Serial.println("initialization failed!");
sdPresente=false;
Serial.println("sono nel costruttore");
}

else{
Serial.println("initialization done.");
sdPresente=true;
//se la sd è presente guardo se è scritta
mioFile = SD.open("valori.txt");
if (mioFile) {
  if(mioFile.available()>0 )
sdScritta=true;
  mioFile.close();
}
}


}




nodoGenerico::~nodoGenerico( )
{

}




everything compiles well but at running time arduino crashes in SD.begin when reach the function millis() called by the sd library

PaulS

Quote
everything compiles well but at running time arduino crashes in SD.begin

I think that this should be a clue, right there. The HardwareSerial class has a begin() method. The SD class has a begin method. The XBee class has a begin method.

Your class needs a begin() method. Constructors should not call begin() methods or execute code that belongs in a begin() method. Anything that involves pin modes, millis(), etc. is not ready for you to use until the init() method completes. The init() method is not called until after all constructors are called.

cantore

thank you for your answer. May be the program crashes also if compiling succes because of compilation order?
I found this article interesting:
http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.14 it is about "static" but may be the logic is similar

PaulS

Quote
May be the program crashes also if compiling succes because of compilation order?

Successful compilation != successful execution.

Nick Gammon


thank you for your answer. May be the program crashes also if compiling succes because of compilation order?
I found this article interesting:
http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.14 it is about "static" but may be the logic is similar


Yes, it's a well known problem. Don't initialize stuff in static constructors is the answer. Or more accurately, don't rely upon other stuff being initialized before your stuff. It is safe to, say, move zero to a variable. It isn't safe to assume another library was initialized before yours. So, for example, you can't assume that millis() will work. You can use a begin method as PaulS suggested. Or dynamically allocate the object rather than statically allocate it.

Quote
I have not understood why I can complie without getting error but after the code execution crashes.


That's no secret. For example I can compile this:

Code: [Select]

void setup ()
{
while (true) ;  // loop forever

pinMode (13, OUTPUT);
digitalWrite (13, HIGH);
}

void loop () {}


That will compile, but not turn on pin 13. They are two separate issues.

Go Up