Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- float deltaTime = 0;
- unsigned long previousMillis = 0;
- float timerA = 0;
- float sendDataTimer = 0;
- bool awaitingResponse = false;
- loop()
- {
- //First we calculate deltaTime, which is what we'll use from now on (Look down below for the UpdateDeltaTime() code)
- UpdateDeltaTime();
- // Now, say you want something to happen every 20 seconds, like sending data.
- sendDataTimer += deltaTime // every second, at 1.0 to it
- if(sendDataTimer >= 20) // can replace 20 with sendDataInterval for easier editing later
- {
- //do something every 20 seconds, like
- SendData(); //runs the SentData function below
- sendDataTimer = 0; //reset the "every 20 seconds" timer, so it can start counting up to 20 again
- }
- // Are we awaiting a response? If so, add deltaTime to the timeouttimer.
- if(awaitingResponse == true)
- {
- timeoutTimer += deltaTime;
- if(timeoutTimer >= 3) //can replace 3 with timeoutPatience or something for easier editing later
- {
- //we didn't get a response! Bugger.
- awaitingResponse = false;
- //do whatever you want to do when you waited too long for a reponse
- }
- }
- }
- UpdateDeltaTime()
- {
- unsigned long currentMillis = millis();
- deltaTime = currentMillis - previousMillis ;
- Serial.print(deltaTime); // deltaTime is how long the last loop() cycle took
- previousMillis = currentMillis;
- deltaTime *= 0.001;
- if(deltaTime < 0) //this will happen when millis() resets
- {
- // you can do a few things here, you can just set deltaTime to 0, or remember what the last good deltaTime is
- deltaTime = 0; // good enough
- }
- }
- SendData()
- {
- //send that data, and also resets the timeoutTimer;
- timeoutTimer = 0;
- awaitingResponse = true; // this allows the timeoutTimer to run
- }
Advertisement
Add Comment
Please, Sign In to add comment