Skip to content

Instantly share code, notes, and snippets.

@Jan-Bart
Created December 2, 2017 16:25
Show Gist options
  • Save Jan-Bart/c288743e46f2ef6b8175e2762bf10d16 to your computer and use it in GitHub Desktop.
Save Jan-Bart/c288743e46f2ef6b8175e2762bf10d16 to your computer and use it in GitHub Desktop.
'use strict';
const async = require('async');
const gpio = require('rpi-gpio');
const BUZZER_PIN = 5;
const TIME_ON = 150;
const TIME_OFF = 900;
const BUZZ_TIME = 20;
gpio.setMode(gpio.MODE_BCM);
gpio.setup(BUZZER_PIN, gpio.DIR_LOW, write);
function makeSound(cb) {
setTimeout(() => {
gpio.write(BUZZER_PIN, true, cb);
}, TIME_OFF);
}
function muteSound(cb) {
setTimeout(() => {
gpio.write(BUZZER_PIN, false, cb);
}, TIME_ON);
}
function highAndLow(callback) {
makeSound((err) => {
muteSound((err) => {
callback(err);
});
});
}
function write() {
let count = 0;
async.whilst(
() => { return count < BUZZ_TIME; },
(cb) => { count++; highAndLow(cb); },
function (err, n) {
console.log(err, n);
}
);
}
process.on('SIGINT', function () {
console.log("Caught interrupt signal");
muteSound(() => {
process.exit();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment