<< Back

Exercise 2.1 - Lights and Buttons

a) Make the LED blink

This is the console. All output created with writeLog() is shown here.

Press the button to make the LED blink in red.

Enter a number in milliseconds for the blinking frequency. To update the frequency, click the button again.


script.js

This is the JavaScript code behind the above application.

/* global writeLog, getInput, tf */
writeLog("Solution for exercise 2.1 b) Make the LED blink");
// Create a global variable to store the found devices
var devices;
var led;
// Initialite connected devices
tf.initDevices(initDone);
// Call this when all connections are established
function initDone(connectedDevices) {
if (connectedDevices.length === 0) {
writeLog("Oops, didn't find any devices! Make sure they are connected and refresh this page!");
}
// Store the devices on the global variable
devices = connectedDevices;
// Get the LED light
led = devices.getDeviceByIdentifier(271);
// Turn the LED off initially
led.off();
}
function blinkRed() {
// Make sure the LED is off
led.off();
// Get the frequency the user entered
var frequency = getInput("frequency");
// Make the LED blink red
led.blink(255, 0, 0, frequency);
writeLog("LED should blink red at a frequency of >" + frequency + "<!");
}
function stopBlinking() {
led.off();
}