Skip to content

Instantly share code, notes, and snippets.

@ekolker
Created May 29, 2014 14:29
Show Gist options
  • Save ekolker/610f36066e8dd0b3d729 to your computer and use it in GitHub Desktop.
Save ekolker/610f36066e8dd0b3d729 to your computer and use it in GitHub Desktop.
Take a selfie with Tessel using the Camera and Ambient modules
// Any copyright is dedicated to the Public Domain.
// http://creativecommons.org/publicdomain/zero/1.0/
var tessel = require('tessel');
var camera = require('camera-vc0706').use(tessel.port['B']);
var ambient = require('ambient-attx4').use(tessel.port['A']);
var notificationLED = tessel.led[1]; // Set up an LED to notify when we're taking a picture
// Sound trigger level. Clap/snap/shout for a Tesselfie!
var triggerLevel = 0.25;
// Wait for the modules to be ready
camera.on('ready', function() {
console.log('Camera ready');
ambient.on('ready', function () {
console.log('Ambient ready');
// Set trigger level
// The trigger value is a float between zero to 1
ambient.setSoundTrigger(triggerLevel);
ambient.on('sound-trigger', function(data) {
console.log('Let\'s take a Tesselfie!');
// Clear the trigger, turn on the light
ambient.clearSoundTrigger();
notificationLED.high();
// Give you a little time to look at the camera
setTimeout(function () {
// Take the picture
camera.takePicture(function(err, image) {
if (err) {
console.log('error taking image', err);
} else {
// Name the image
var name = 'picture-' + Math.floor(Date.now()*1000) + '.jpg';
// Save the image
console.log('Picture saving as', name, '...');
process.sendfile(name, image);
console.log('Image saved!');
// Indicator light off
notificationLED.low();
}
// Reset the sound trigger
setTimeout(function () {
ambient.setSoundTrigger(triggerLevel);
console.log('Ready to take another Tesselfie!')
}, 500);
});
}, 100);
});
});
});
camera.on('error', function(err) {
console.error(err);
});
ambient.on('error', function (err) {
console.log(err)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment