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.


On Tuesday, August 18, 2015 at 5:49:40 PM UTC+2, jps birdzbeez wrote:
>
> I am not a programmer and never claimed to be one.. I just read and read 
> and read.. trying to learn and understand.. but for some reason I am not 
> sure I am understanding how this works...  if someone doesn't mind I need 
> help to point me in the correct direction to get my project going.. What I 
> am able to do is make it work in python but I like the idea of node.js over 
> python..  
>
>
> The project is to use my raspberry pi to raise or lower a door (not a 
> garage door). At the top and bottom are Hall Effect sensors. Before raising 
> the door I want to verify the door is closed. If it is closed then raise 
> the door till it reaches the opendoor state. I have not had any luck with 
> node.js programming..  this is what I have come up with so far.. 
>
> var gpio = require('rpi-gpio');
> var doorclosed = 31; //hall effect sensor when door is closed
> var dooropen = 33; //hall effect sensor when door is open
> var opendoor = 35; //cause door to open
> var closedoor = 37; //cause door to close
>
>
> function raisedoor(callback) {
>   //verifydoorclosed(function verified door closed on gpio 31)
>   gpio.read(doorclosed, gpio.DIR_IN, readInput);
>   //if doorclosed is = true then opendoor till dooropen is true
>     if dooclosed = true
>     gpio.write(opendoor, 1, function(err){
>       gpio.read(dooropen, true, function(err){
>         gpio.write(opendoor, 0, function(err){
>           callback();
>         });
>       });
>     });
>   }
>
> I don't mind reading if someone has a link that explains what I am 
> missing.. thank you
>

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to nodejs+unsubscr...@googlegroups.com.
To post to this group, send email to nodejs@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/ddfaf3e1-4ddb-4209-9e87-83f69d92a8b8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to