Your code doesn't seem correct (nevermind the syntax errors). You also didn't post any potential results.
These kinds of questions would be better off at
stackoverflow.com, but here are a few pointers.
The thing to realize here is that gpio.read and gpio.write are async functions, and your first call
gpio.read(doorclosed, gpio.DIR_IN, readInput); seems synchronous, so our check (if (doorclosed === true){} ) will always fail.
You also seem to have too many params (according to documentation at https://www.npmjs.com/package/rpi-gpio) at further gpio.read calls, you only need PIN number and callback. The second thing is that your second check (whether the door is closed) should most likely be polling, say, every second or so, because this way it'll make a check right away after starting the doorOpen, and it'll probably be too fast..
An example of a raiseDoor function would be as:
function raiseDoor() {
// first check that the door is closed.
// the callback we pass in is a function which will receive (err, value) arguments - err if there was a problem during this call, value if not.
gpio.read(DOORCLOSED, doorClosedCallback);
}
Two things - I wrote the doorclosed in CAPS, to signal the developer (be it yourself or somebody else) that the this variable is constant, we don't plan to change it. I suggest doing this for gpio-pin numbers for code clarity.
Second thing, now we need to write this doorClosedCallback. It will be called when that gpio.read above reads the sensor value.
function doorClosedCallback (err, value) {
// check for errors
if (err) {
console.log('Error checking if the door was open', err);
return;
}
// if no errors, we can see the door position.
if (value === true) {
// door is open, skip.
console.log('Door open, ending.');
return;
} else {
// door is not open. We need to do two things:
// 1. kick off the door motor (write to 'opendoor' PIN)
// 2. start polling - check every second if the door is now closed, and when it is, stop the door motor.
gpio.write(OPENDOOR, true, doorInMotion);
}
}
As you can see, I'm writing true, and again offloading this check of the work complete to another function - that way it's easier to reason about those simple functions with simplified jobs.
So let's write it:
function doorInMotion (err) {
// it will only receive an error if our gpio.write(OPENDOOR) failed - meaning, if we failed to start the door motor.
if (err) {
console.log('Door start failed.', err);
return;
}
// now is the trick. We want to check every second if the door is open. If yes, write a zero to opendoor pin, so that we stop the engine.
// we do this every second, and we save this interval.
var interval = setInterval(function() { // this inner function runs every second, until somebody calls the interval.cancel();
// every second, check the door sensor.
gpio.read(DOOROPEN, function(err, value) {
if (err) {
console.log('Error reading door-open value.', err);
return; // also could be a good idea to stop the engine here, because due to error, we don't know if the door is open or not, maybe sensor has failed.
}
// anyway, if the sensor didn't fail, we can check the DOOROPEN pin value:
if (value !== true) {
console.log('Door not yet open, keep the motor running.');
} else {
console.log('Door is finally open.');
// now stop the door
gpio.write(OPENDOOR, false, function(err) {
if (err) {console.log('Error stopping the door motor.', err);
});
}
});
}, 1000);
}
This is just one, basic solution example, it can probably be made better and simpler. But I think it should at least get you started.