Program to read from push button
Write the following code in Cloud9 and save it as pushButton.js
. Run the program, keep the button pressing and releasing. You should see Button is pressed printed when you press the button and Button is released when the button is released. The program will exit after a minute automatically.
var b = require('bonescript');var loopTime = 1000; var exitTime = 60000; var button = 'P8_16'; b.pinMode(button, b.INPUT); var loopTimer = setInterval(check,loopTime); var exitTimer = setTimeout(exitProgram,exitTime); function check() { b.digitalRead(button, checkButton); } function checkButton(pinObj) { if(pinObj.value == b.HIGH) { console.log("Button is pressed"); } else { console.log("Button is released"); } } function exitProgram() { clearInterval(loopTimer); console.log("Program exiting"); }
Explanation
We first declared that we are going to use the P8_16
pin as the input. That means we will only read from it and not write on it. We set...