-1

I have two data bases that contain temperature data from an arduino...

I want to send the data to these data bases for a minute, then after of send the data in the another data base, send it to the next data base (LATER OF TEN TIMES OF THE FIRST DATA BASE)

My code below:

int count = 0;
for(int a = 1; a <= 10; a++) {
  Cayenne.run();
  delay(60000);
  count = count + 1;
}
if(count== 10) {
  ToPostWStemp();
  count = 0;
}

But doesn't send anything, and I don't know how to do. Many people said me that it's much better use millis() function, but I don't know how code that function on my Arduino.

D.P. The function "Cayenne.run" calls the first function of server, then the "ToPostWStemp" calls the second last server function.

Thank you!

Jeremy
  • 566
  • 1
  • 6
  • 25
Maclos
  • 7
  • 1
  • 3
  • There's not enough information to tell what you're doing wrong. Does the code above run in an infinite loop? Or is it a function? What is the "first function of server"? What is "last function of server"? – iheanyi Aug 18 '16 at 23:08
  • Ok, let me update the complete code – Maclos Aug 19 '16 at 02:47

1 Answers1

0

If I'm understanding the question correctly, it sounds like you want Cayenne.run() to be called once every minute, and ToPostWStemp() to be called once every 10 minutes.

To do that using millis(), you can simply keep track of the last time each function was called, and compare that against the current value of millis(), calling each function only when the elapsed time exceeds the desired interval. Something like this:

unsigned long cayenneTime = 0;
unsigned long postWSTime = 0;
void loop()
{
    if (millis() - cayenneTime >= 60000)
    {
        // Do this every 60 seconds
        Cayenne.run();

        // Keep track of the last time this code ran, so we know
        // when to run it next time
        cayenneTime = millis();
    }
    if (millis() - postWSTime >= 60000 * 10)
    {
        // Every 10 minutes, do this
        ToPostWStemp();

        // Keep track of the last time this code ran, so we know
        // when to run it next time
        postWSTime = millis();
    }

    // Do other stuff here
}

Note that millis() will overflow and reset to 0 every 4,294,967,295 milliseconds (about 49 days), so if this is to be a long-running program you'll want to take that into account and adjust for it.

Jeff Loughlin
  • 4,134
  • 2
  • 30
  • 47
  • Thanks for answer, you're cool buddy, now I know to use the function millis, thanks for your example an your time por answer, Jeff. Greetings – Maclos Aug 22 '16 at 23:37
  • Wait a moment... why do you write the const outside of loop?? – Maclos Aug 23 '16 at 03:10
  • Not sure what you mean. If you mean why did I declare `cayenneTime` and `postWSTime` outside of the `loop()` function, it's because declaring them inside of `loop()` would make them local variables, which means their values would get reset every time through `loop()`. I could also have declared them as `static` local variables inside of loop(), which would have the same effect and would probably be cleaner, but that's a matter of coding style. – Jeff Loughlin Aug 23 '16 at 12:03
  • That's was good buddy, thanks for everything! you save my life! – Maclos Aug 29 '16 at 21:09