I am having several issues with some code I have written to control my garage door. Since the door button just reverses the door direction from the last time, I want to ensure that an end switch is closed after 30 seconds so I call doorClose from within the doorClose function via imp.wakeup(30.0, doorClose). the program doesn’t seem to wait for the wakeup time (30 sec) to pass before calling itself again. Full code and devise log below:
`// create a global variable called door,
// and assign pin1 to it
door ← hardware.pin1;
endSwitch ← hardware.pin2;
// configure door to be a digital output
door.configure(DIGITAL_OUT);
// function to “push” door button
function buttonPush() {
server.log(“button pushed”);
door.write(1);
}
// function to release door button
function buttonRelease() {
server.log(“button released”);
door.write(0);
}
// function to push and release door button
function buttonToggle(anything) {
buttonPush();
imp.wakeup(1.0, buttonRelease);
}
//create a variable to store the endSwitch Status
local switchStatus = 3;
//function to test endswitch status
function endSwitchStatus() {
switchStatus = endSwitch.read();
server.log("switch Statuss: ");
}
// function to open the door
function openDoor(anything) {
server.log(“open button pushed”);
switchStatus = endSwitch.read();
server.log("switch Status: " + switchStatus);
// if switch closed (pin LOW), toggle the door button
if (switchStatus == 0) {
buttonToggle(1);
// wait 30 seconds, call function again to test to make sure end switch is open
imp.wakeup(30.0, openDoor(1))
}
}
// function to close door
function closeDoor(anything) {
server.log(“close button pushed”);
switchStatus = endSwitch.read();
//server.log("switch Status: ", + switchStatus);
// if switch open (pin HIGH), toggle the door button
if (switchStatus == 1) {
buttonToggle(1);
// wait 30 seconds, call function again to test to make sure end switch is closed
imp.wakeup(30, closeDoor(1))
}
}
// configure endSwitch to be a digital input
endSwitch.configure(DIGITAL_IN);
// register a handler for “button” messages from the agent
agent.on(“close”, closeDoor);
agent.on(“open”, openDoor);
agent.on(“button”, buttonToggle)`
Devise log:
2014-11-09 19:19:36 UTC-8 [Status] Downloading new code; 3.93% program storage used 2014-11-09 20:19:07 UTC-8 [Device] close button pushed 2014-11-09 20:19:07 UTC-8 [Device] switch Status: 1 2014-11-09 20:19:07 UTC-8 [Device] button pushed 2014-11-09 20:19:07 UTC-8 [Device] close button pushed 2014-11-09 20:19:07 UTC-8 [Device] switch Status: 1 2014-11-09 20:19:07 UTC-8 [Device] button pushed 2014-11-09 20:19:07 UTC-8 [Device] close button pushed 2014-11-09 20:19:07 UTC-8 [Device] switch Status: 1 2014-11-09 20:19:07 UTC-8 [Device] button pushed . . . . 2014-11-09 20:19:09 UTC-8 [Device] close button pushed 2014-11-09 20:19:09 UTC-8 [Device] switch Status: 1 2014-11-09 20:19:09 UTC-8 [Device] button pushed 2014-11-09 20:19:09 UTC-8 [Device] close button pushed 2014-11-09 20:19:10 UTC-8 [Device] switch Status: 1 2014-11-09 20:19:10 UTC-8 [Device] button pushed 2014-11-09 20:19:10 UTC-8 [Device] close button pushed 2014-11-09 20:19:10 UTC-8 [Device] switch Status: 1 2014-11-09 20:19:17 UTC-8 [Exit Code] imp restarted, reason: out of memory 2014-11-09 20:19:17 UTC-8 [Status] Device Booting; 3.99% program storage used
What is going on?
Also I have added the “anything” parameter because it seems that the agent devise.send() wants functions to have parameters. Is that right?