Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejs
titleSample using "onoff" node js library
collapsetrue
var Gpio = require('onoff').Gpio; //include onoff to interact with the GPIO
var sleep = require('sleep');

var TRUNK_SENSOR = new Gpio(27, 'in','both');
var DOOR_SENSOR =  new Gpio(6, 'in','both');

var LEDDOOR1 = new Gpio(4, 8,'out');
var DOOR2 = new Gpio(7,'out');
var //useTRUNK1 GPIO= pinnew 4, and specify that it is output
var blinkInterval = setInterval(blinkLED, 250); //run the blinkLED function every 250ms

function blinkLED() { //function to start blinking
  if (LED.readSync() === 0) { //check the pin state, if the state is 0 (or off)
    LED.writeSync(1); //set pin state to 1 (turn LED on)
  } else {
    LED.writeSync(0); //set pin state to 0 (turn LED off)
  }
}

function endBlinkGpio(9,'out');
var TRUNK2 = new Gpio(11,'out');

DOOR_SENSOR.watch(function(err,value){
  if(err){
    console.error('There was an error', err); //output error message to console
    return;
  }

  if(value==0){
    DOOR1.writeSync(1);
    DOOR2.writeSync(0);
    sleep.sleep(10); // sleep for ten seconds
    DOOR1.writeSync(0);
    DOOR2.writeSync(0);
  }else{
    DOOR1.writeSync(0);
    DOOR2.writeSync(1);
    sleep.sleep(10); // sleep for ten seconds
    DOOR1.writeSync(0);
    DOOR2.writeSync(0);
  }
});

TRUNK_SENSOR.watch(function(err,value){
  if(err){
    console.error('There was an error', err); //output error message to console
    return;
  }
  if(value==0){
    TRUNK1.writeSync(1);
    TRUNK2.writeSync(0);
    sleep.sleep(10); // sleep for ten seconds
    TRUNK1.writeSync(0);
    TRUNK2.writeSync(0);
  }else{
    TRUNK1.writeSync(0);
    TRUNK2.writeSync(1);
    sleep.sleep(10); // sleep for ten seconds
    TRUNK1.writeSync(0);
    TRUNK2.writeSync(0);
  }
});

function unexportOnClose() { //function to run when stopexiting blinkingprogram
  clearIntervalTRUNK1.writeSync(blinkInterval0); // Stop blink intervalsTurn off
  TRUNK2.writeSync(0); // Turn off
  LEDDOOR1.writeSync(0); // Turn LED off
  LEDoff
  DOOR2.writeSync(0); // Turn off
  TRUNK1.unexport(); // Unexport GPIO to free resources
  TRUNK2.unexport(); // Unexport GPIO to free resources
  DOOR1.unexport(); // Unexport GPIO to free resources
  DOOR2.unexport(); // Unexport GPIO to free resources
  TRUNK_SENSOR.unexport();
  DOOR_SENSOR.unexport();
};

setTimeout(endBlinkprocess.on('SIGINT', 5000unexportOnClose); //stop blinking after 5 secondsfunction to run when user closes using ctrl+c


References

...