Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. var Gpio = require('onoff').Gpio; //include onoff to interact with the GPIO
  2. var LED = new Gpio(4, 'out'); //use GPIO pin 4 as output
  3. var pushButton = new Gpio(17, 'in', 'both'); //use GPIO pin 17 as input, and 'both' button presses, and releases should be handled
  4.  
  5. pushButton.watch(function (err, value) { //Watch for hardware interrupts on pushButton GPIO, specify callback function
  6. if (err) { //if an error
  7. console.error('There was an error', err); //output error message to console
  8. return;
  9. }
  10. LED.writeSync(value); //turn LED on or off depending on the button state (0 or 1)
  11. });
  12.  
  13. function unexportOnClose() { //function to run when exiting program
  14. LED.writeSync(0); // Turn LED off
  15. LED.unexport(); // Unexport LED GPIO to free resources
  16. pushButton.unexport(); // Unexport Button GPIO to free resources
  17. };
  18.  
  19. process.on('SIGINT', unexportOnClose); //function to run when user closes using ctrl+c
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement