In my main.cpp i call the constructor of a class.In the constructor i set the variables of his class from some static functions. All the problem is about setting time from NTP server.
The static functions which are a getters use a while loop in order to check a flag.The flag is false if time is not set and true if at least one time the NTP has responded.If the flag is false the while loop has to try to get time forever (cause there is no reason to continue without time)and if it is true it returns the time.
The constructor sets the variables as i said by calling the getters.
When i declare the object of that class in main.The program does not run cause i get nothing on serial print.If i commented the declaration it works.Also it does not act like stucking inside the while cause i placed a serial print inside there and it did not appear also.I give the code below:
the constructor:
#import "DayTime.h"
#include "NTP.h"
DayTime::DayTime(){
hour = NTP::getLCThour();
minutes = NTP::getMin();
seconds = NTP::getSec();
}
the main.cpp
#include <Arduino.h>
#include <SPI.h>
#include "NTP.h"
#include "DayTime.h"
#include "DayNumber.h"
DayTime DT;
DayNumber DN;
int main(void)
{
init();
setup();
for (;;)
loop();
return 0;
}
void setup() {
Serial.begin(9600);
NTP::startEthernetAndUdp();
}
void loop() {
NTP::getTime();
// Serial.println(DT.hour);
// Serial.println(DT.getTimeToDec());
delay(3000);
here the getTime function works if i comment the declaration of the DayTime object named DT.
The NTP.cpp parts:
- the first getter:
int NTP::getLCThour(){
while(timeNTPSet==false){
Serial.print("kolaw");
getTime();
}
return lcthour;
}
- the declaration of the flag on header:
static bool timeNTPSet;
this is public variable
3)its definition at the start of the .cpp file
bool NTP::timeNTPSet= false;
this is set in .cpp after the include.
And at last part of the function that sets the flag to true:
NTP NTP::getTime(){
if (testNtpServer()){
timeNTPSet = true;
// We've received a packet, read the data from it
Udp.read((unsigned char*)packetBuffer,NTP_PACKET_SIZE);
the testNTPServer function just test 3-4 different NTP servers in order to take time from at least from one.Also it works fine if i comment the declaration of the DayTime object as i said before.
The getTime function inside loop has serial prints and this function has nothing to do with the DayTime object.So i should run normally.
If i declare the object inside the loop like this
DayTime DT=DayTime();
the program works fine but i do not want to create an object in every loop.
Any help?