[SOLVED] strange problem with millis()


  • Hero Member

    It has been a long time but i am back... however, I must have lost it!

    Millis now return 4 digits results and I can find the reason. It might be the update of the libraries the arduino IDE asked me to do. Anyways, here is the code:

    #define MY_DEBUG
    #define MY_RADIO_NRF24
    
    #include <SPI.h>
    #include <MySensors.h>                       // Mysensor network
    
    void setup() {
      // put your setup code here, to run once:
    
    }
    
    void loop() {
        unsigned long now;
    
        Serial.println("entering loop");
        now=millis();
        Serial.print("now=");
        Serial.println((unsigned long)now);
          sleep(5000);
    
    
    }
    

    couldn't be simpler....

    now the result:

    x00
    x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x000 MCO:BGN:INIT NODE,CP=RNNNA--,VER=2.1.1
    3 TSM:INIT
    4 TSF:WUR:MS=0
    11 TSM:INIT:TSP OK
    13 TSF:SID:OK,ID=28
    14 TSM:FPAR
    33 TSF:MSG:SEND,28-28-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
    272 TSF:MSG:READ,0-0-28,s=255,c=3,t=8,pt=1,l=1,sg=0:0
    277 TSF:MSG:FPAR OK,ID=0,D=1
    2040 TSM:FPAR:OK
    2041 TSM:ID
    2042 TSM:ID:OK
    2044 TSM:UPL
    2046 TSF:MSG:SEND,28-28-0-0,s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=OK:1
    2054 TSF:MSG:READ,0-0-28,s=255,c=3,t=25,pt=1,l=1,sg=0:1
    2059 TSF:MSG:PONG RECV,HP=1
    2061 TSM:UPL:OK
    2063 TSM:READY:ID=28,PAR=0,DIS=1
    2067 TSF:MSG:SEND,28-28-0-0,s=255,c=3,t=15,pt=6,l=2,sg=0,ft=0,st=OK:0100
    2073 TSF:MSG:READ,0-0-28,s=255,c=3,t=15,pt=6,l=2,sg=0:0100
    2080 TSF:MSG:SEND,28-28-0-0,s=255,c=0,t=17,pt=0,l=5,sg=0,ft=0,st=OK:2.1.1
    2088 TSF:MSG:SEND,28-28-0-0,s=255,c=3,t=6,pt=1,l=1,sg=0,ft=0,st=OK:0
    2095 TSF:MSG:READ,0-0-28,s=255,c=3,t=6,pt=0,l=1,sg=0:M
    2100 MCO:REG:REQ
    2103 TSF:MSG:SEND,28-28-0-0,s=255,c=3,t=26,pt=1,l=1,sg=0,ft=0,st=OK:2
    2109 TSF:MSG:READ,0-0-28,s=255,c=3,t=27,pt=1,l=1,sg=0:1
    2114 MCO:PIM:NODE REG=1
    2116 MCO:BGN:STP
    2118 MCO:BGN:INIT OK,TSP=1
    entering loop
    now=2120
    2121 MCO:SLP:MS=5000,SMS=0,I1=255,M1=255,I2=255,M2=255
    2127 MCO:SLP:TPD
    2129 MCO:SLP:WUP=-1
    entering loop
    now=2131
    2131 MCO:SLP:MS=5000,SMS=0,I1=255,M1=255,I2=255,M2=255
    2138 MCO:SLP:TPD
    2140 MCO:SLP:WUP=-1
    entering loop
    now=2142
    2142 MCO:SLP:MS=5000,SMS=0,I1=255,M1=255,I2=255,M2=255
    2148 MCO:SLP:TPD
    

    I have been staring at this for the last two hours... Any help will be appreciated. I am sure it is something ultra stupid

    Regards,
    Moshe


  • Hero Member

    @Moshe-Livne 🙂 Maybe you slept for 2 hours? Try to define now as static or make it global. Now it is initialized every loop ..


  • Hero Member

    @AWI as it should.... but - it should show a very long number.... not a 4 digit one. and not the same values each time i run the code.

    this is a test code, just to show the problem. in the original code there was static var, etc and the difference was checked.


  • Hero Member

    @Moshe-Livne You are right, I was too quick. 😊. Another look: sleep() also sleeps the millis() timer. As the value only slowly increases it won't show more digits i the first hours..


  • Hero Member

    @AWI Darn you are right!!!!! Thanks! Have been dealing with rpi and upwards lately so assumed it has some kind of internal clock.
    Thanks again!


Log in to reply
 

Suggested Topics

  • 1
  • 10
  • 5
  • 3
  • 2
  • 2

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts