Workshop Guide
This guide gives an overview of the workshop and step by step instructions for each of the 5 projects.View Workshop Guide - Using a BreadBoard or Workshop Cape Version
Code
Find all the code for our five Workshop Projects below.Project 1 - Blink an LED
var b = require('bonescript'); // Read library var ledPin = "P9_14"; // Select pin var state = 1; // Set LED state var interval = 200; // Set interval (in ms) b.pinMode(ledPin, b.OUTPUT); // Set pin to output function toggleLED() { // Switch LED on or off state = state ? 0 : 1; b.digitalWrite(ledPin, state); } setInterval(toggleLED, interval); // Call function at set intervalDownload Project 1 .js File
Project 2 - Red Yellow and Green LEDs
var b = require('bonescript'); // Read library var ledPin1 = "P9_14"; // Select pins var ledPin2 = "P9_16"; var ledPin3 = "P8_19"; b.pinMode(ledPin1, b.OUTPUT); // Enable pins and set to output b.pinMode(ledPin2, b.OUTPUT); b.pinMode(ledPin3, b.OUTPUT); setInterval(random, 300); // Call function 'random' every 300ms function random(){ // Define function 'random' b.digitalWrite(ledPin1, Math.round(Math.random())); b.digitalWrite(ledPin2, Math.round(Math.random())); b.digitalWrite(ledPin3, Math.round(Math.random())); }Download Project 2 .js File
Project 3 - Read a Push Button Switch, Control 3 LEDs
var b = require('bonescript'); var ledPin1 = "P9_14"; var ledPin2 = "P9_16"; var ledPin3 = "P8_19"; var switchPin = "P8_26"; var state = 0; var mode = 0; b.pinMode(ledPin1, b.OUTPUT); b.pinMode(ledPin2, b.OUTPUT); b.pinMode(ledPin3, b.OUTPUT); b.pinMode(switchPin, b.INPUT); blank(); // switch all LED's off setInterval (inputHandler, 200); function inputHandler() { // detect switch push b.digitalRead(switchPin, activate); function activate(x){ if (x.value == '0') mode = mode+1; if (mode > 2) mode = 0; console.log (mode); } switch (mode){ case 0: blank(); break; case 1: sequence(); break; case 2: random(); break; } } function sequence() { if(state == '1') { b.digitalWrite(ledPin1, b.HIGH); b.digitalWrite(ledPin2, b.LOW); b.digitalWrite(ledPin3, b.LOW); } if(state == '2') { b.digitalWrite(ledPin1, b.LOW); b.digitalWrite(ledPin2, b.HIGH); b.digitalWrite(ledPin3, b.LOW); } if(state == '3') { b.digitalWrite(ledPin1, b.LOW); b.digitalWrite(ledPin2, b.LOW); b.digitalWrite(ledPin3, b.HIGH); } state = state + 1; if (state > 3) state = 1; } function random(){ b.digitalWrite(ledPin1, Math.round(Math.random())); b.digitalWrite(ledPin2, Math.round(Math.random())); b.digitalWrite(ledPin3, Math.round(Math.random())); } function blank(){ b.digitalWrite(ledPin1, 0); b.digitalWrite(ledPin2, 0); b.digitalWrite(ledPin3, 0); }Download Project 3 .js File
Project 4 - Read a Push Button, Control a Buzzer
var b = require('bonescript'); var buzzerPin = "P8_13"; var switchPin = "P8_26"; var state = 0; var switchState = 0; var volume = 0.2; // configure pins b.pinMode(buzzerPin, b.OUTPUT); b.pinMode(switchPin, b.INPUT); setInterval (inputHandler, 100); setInterval (toggle, 500); function inputHandler() { // detect switch push b.digitalRead(switchPin, activate); function activate(x){ if (x.value == '0') switchState = !switchState; } } function toggle() { // switch buzzer on/off state =!state; if(state == 1) b.analogWrite(buzzerPin, b.LOW); else if (switchState == 1) b.analogWrite(buzzerPin, volume); }Download Project 4 .js File
Project 5 - Read Temperature and Control a Buzzer
var b = require('bonescript'); var analogPin = "P9_36"; var buzzerPin = "P8_13"; var ledRed = "P9_14"; var ledGreen = "P8_19"; var ledYellow = "P9_16"; var volume = "0.2"; var value = 0; // configure pins b.pinMode(buzzerPin, b.OUTPUT); b.pinMode(ledRed, b.OUTPUT); b.pinMode(ledGreen, b.OUTPUT); b.pinMode(ledYellow, b.OUTPUT); setInterval (inputHandler, 500); function inputHandler() { // read thermistor value value = b.analogRead(analogPin); value = Math.round(((value*10)-4.5)*4000); if (value < 100) value = 100; console.log (value); } loop(); //beep at interval of thermistor value function loop() { b.analogWrite(buzzerPin, volume); setTimeout(function(){b.analogWrite(buzzerPin, b.LOW);}, 50); setTimeout(loop, value); if (value > 1000) b.digitalWrite(ledGreen,b.HIGH); else b.digitalWrite(ledGreen,b.LOW); if (value > 200 && value < 1000) b.digitalWrite(ledYellow,b.HIGH); else b.digitalWrite(ledYellow,b.LOW); if (value < 200) b.digitalWrite(ledRed,b.HIGH); b.digitalWrite(ledRed,b.LOW); }Download Project 5 .js File