Timeout / Interval without While loop

Hi all!

First post on the forum, so just for the quick intro: I am Marcel Sch working from Rotterdam, the Netherlands, on interesting concepts for connected products at The Incredible Machine.

Currently I am working on a multiplayer toy / game: a slot-race track that is controlled over IP.

We’ve made this before with a combination of PHP / Processing / Arduino (http://the-incredible-machine.com/carreramash/), but now I am redoing it with the Imp to replace it all.

I am running into the following issue: If my Agent doesn’t receive any requests for 60 seconds, I want a function to be triggered. Much like the javascript equivalent:
setTimeout(function, 1000);

I’ve looked through the Squirrel-lang documentations, but the best example i found was a while-loop that compared a
time() > timeout

This is not an option for my case, because I cannot afford my agent to stall for the duration of the timeout.

If you guys can help me out, I will host a international race broadcasted with webcams.

Best,

Marcel

Marcel, that’s really cool! I have some questions about the process… You say on the website “The server registers all mashes by both teams and calculates the car speeds. The track connects to the same server and requests the speeds of the cars and responds whenever a car drove a lap.”

Does the server only speed up a car after each lap or constantly?

As I understand it, the default timeout for a request to an agent actually is 60 seconds, so if you send a request to the agent and it sends to the device, and the device doesn’t respond back to the agent for 60 seconds, the agent will timeout and reply to the initial HTTP request with a timeout error.

I don’t quite understand what you mean by the agent stalling during the timeout.

Wow, you really made me want to hack a slot car track! My nephews would love it. :slight_smile:

The imp version of setTimeout is called imp.wakeup().

Peter

Hi Jwehr,

The cars’ speed would be updated about twice a second, and the clients will push their ‘mashes-per-second’ each second.

What I mean about stalling the agent during timeout is that I cannot do this:

`
local timeout = time() + 60; // in 60 seconds we’ll reset the game

http.onrequest(function (request, res){

local requestObj = http.jsondecode(request.body);

//RESET TIMEOUT HERE!
timeout = time() + 60; //after every request postpone the reset

//fiddle with gamespeed 
local clientMashSpeed = requestObj.MashSpeed;

res.header("Access-Control-Allow-Origin","*");
server.log("GAME STATE: "+gameState);

try {
                res.send(200,http.jsonencode({
                    gameState = gameState
                    countDown = WAITING_TIME
                });

} catch (e) {
    res.send(500,"error")
}

});

while (true){
if(timeout <= time()){
//RESET THE GAME!
}

}

`

@Peter: As you see, I still want to do ‘stuff’ while waiting for the timeout to happen. Wouldn’t the imp.wakeup() function put everything in sleep?

Or… can you cancel/postpone an imp.wakeup function once initialized?

No, imp.wakeup() really does work just like setTimeout: everything else continues to run. There is a blocking version, imp.sleep().

Cancelling an imp.wakeup() is an often-requested feature, coming soon, but until then you could have a polling function (using imp.wakeup) which runs every second, or every five seconds, checking the timeout <= time() condition (if it’s not so critical that it’s exactly 60 seconds).

Peter

Hi Peter! That is a great answer! Thank you so much. I am going to try it out and … keep my promise of course!!!

Works like a charm!!