Remote motor control with automatic off

Hi,

I’m a complete n00b at all this so please have patience and hear me out :slight_smile:

I’m trying to control two motors connected to the imp remotely. So far I’ve managed to get this working using http requests sent from an arduino Uno. I can reliably switch the motors on. I can also switch them off, by sending a second request, though this is a little flakey.

What I’d really like to be able to do is switch the motors on for a set period (just a few seconds) and then have them automatically stop, and stay stopped, until I trigger them to switch on again. I figure this should be straight forward enough, but realise that I probably need to send a subsequent HTTP request from the planner to reset the switch to value to 0, or ‘off’, if I’m to avoid having to do this manually.

This is where I’m stumped: I can start a motor; I can set the delay; but I can’t figure out how to send the request to turn the switch value back to 0. Until I do the motor stays on until I manually send another HTTP request.

I’ve tried getting back to basics using the Blinkomatic example on the devWiki. Again I can reliable start and stop an LED blinking using HTTP requests, but I’m still confused as to how to make the blinking stop automatically after a delay and reset the ‘Inhibit’ value to make sure it stays off.

Thoughts, tips, sympathy appreciated.

Dean

@Fluxx: do you have some code you could share with us :slight_smile:

You can just use: imp.wakeup(2000, function() { /* code to switch off motor */} ) after the code that switches the motor on.

The only catch here is that if you receive a second http request to switch on the motor before it is switched off after the timeout, the first wakeup call is not cancelled, so it will be turned off prematurely by this first call.
It’s better to use a global timer to indicate when the motor should be turned off. Like so:
`
motor_timer <- 0

// switch motor on for specified number of milliseconds
function motor_on(ms) {
// switch motor on here
motor_timer = hardware.millis() + ms
imp.wakeup(ms, motor_off)
}

function motor_off() {
if(hardware.millis() >= motor_timer) {
// switch motor off here
}
}
`