PLEASE NOTE: This site is no longer being actively maintained. For frequently updated BeagleBone resources please visit Beaglebone.org.

Read a Push Button Switch, Control 3 LEDs

Overview

In this tutorial you will learn how to create a simple LED multi-sequencer circuit, access Cloud9 (the BeagleBone's coding interface), load and run a script. In this project, we add digital input control to read a push-button switch to change the LED sequence. The code introduces the input  function to read the button switch and select between three sequence modes .

Note: This tutorial uses a BeagleBone set up specifically for our BeagleBone Black Tinkerer Workshop. The BeagleBone Black you will use in the Tinkerer Workshop will be set up with a Display and a Keyboard with a trackball mouse, and the JavaScript Files will be saved to the board. The BeagleBone will be running Angstrom, which come pre-loaded when you get the board. If you are not in a Tinkerer Workshop and want to do these projects, you can find a guide on how to attach a keyboard with a trackball mouse and a display here. You will also need to add the JavaScript files, which can be found here, to the following directory on the BeagleBone Black: /var/lib/cloud9/workshop

Parts List

Wiring Diagram

Project 3 - Fritzing wiring diagram

Step 1

Wire the components as shown. The LED is polarized (only works one-way)
Note: The resistor is required to limit the current to the LED.

Step 2

Plug the DC power jack into the BeagleBone Black to power the device.

Step 3

Once the system is ready, click on BEAGLEBONE-workshop icon to open the Chromium browser and then start the Cloud9 IDE BeagleBone.local:3000 (192.168.7.2:3000)

Step 4

In the Cloud9 workshop folder click on the file Project3.js to open the BoneScript program that will control the LED and make it blink.
The code should look like this:

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);
}

Step 5

Now press Run at the top of the Cloud9 interface. If this says "Debug", select "Run" from the drop down under this button.
Cloud 9 BeagleBone Black Run Button
The script should now run, and when you push the button on the circuit, the LEDs should blink.