...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
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 intervals Turn off TRUNK2.writeSync(0); // Turn off LEDDOOR1.writeSync(0); // Turn LEDoff DOOR2.writeSync(0); // Turn off LEDTRUNK1.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 |
Run the Code
> npm sample.js
References
Reference | URL |
---|---|
raspberrypi.org | https://www.raspberrypi.org/documentation/usage/gpio-plus-and-raspi2/README.md |
Sparkfun Tutorial | https://learn.sparkfun.com/tutorials/raspberry-gpio |
Interactive Pinout Diagram | https://pinout.xyz/ |
NodeJS - wiring-pi library | https://www.npmjs.com/package/wiring-pi |
NodeJS - wiring-pi Documentation | https://github.com/WiringPi/WiringPi-Node/blob/master/DOCUMENTATION.md |
NodeJS - onoff library (buggy) | https://www.npmjs.com/package/onoff https://www.w3schools.com/nodejs/nodejs_raspberrypi_gpio_intro.asp |
NodeJS - gpio library (Deprecated) | https://www.npmjs.com/package/gpio |
...