Micro:Bit Voting

If you have a classroom of kids, one thing you might want to do is to make a voting system where children can do something with their Micro:bit and have it shown at the front of the class.

We're going to use a Pixl.js board with Espruino to receive data that is transmitted over Bluetooth by Micro:bits programmed with MakeCode and display it on the Pixl.js screen.

Because standard Eddystone Bluetooth advertising is used, multiple receivers can be made - using other Bluetooth LE Espruino devices or just a PC with special software.

Micro:Bit software

Add Package

Bluetooth Package

Bluetooth Package Warning

Your code should look like this:

Finished code

Click Download and save the file to each Micro:bit you're interested in.

Pixl.js Software

On the Pixl, we'll write code that scans for any advertised URLs that contain a # character, and use the text after the # as a vote.

For Pixl.js, follow the Getting Started Guide to get connected with the Espruino IDE.

Copy and paste the following code to the right-hand side of the IDE and click Upload, and you're done!

// List of eddystone devices
var eddystone = {};
// List of votes
var votes = {};

// Start scanning for devices
NRF.setScan(function(dev) {
  if (dev.serviceData && dev.serviceData.feaa)
    eddystone[dev.id] = dev;

/* Every so often, scan over the list of devices
we have and tally up the votes */
setInterval(function() {
  // reset votes
  votes = {};
  // tally them up
  for (var id in eddystone) {
    var dev = eddystone[id];
    if (!dev.age) dev.age=0;
    // only use votes from devices we heard from recently
    if (dev.age < 40) {
      // if the URL contains a hash, the vote is what comes after
      var url = E.toString(dev.serviceData.feaa).substr(3);
      var hash = url.lastIndexOf("#");
      if (hash) {
        var vote = url.substr(hash+1);
        if (vote in votes)
  // now display on the screen
  g.drawString(0|votes["1"], g.getWidth()/4, g.getHeight()/2);
  g.drawString(0|votes["2"], 3*g.getWidth()/4, g.getHeight()/2);
}, 500);

Type save() to write this code to Pixl.js so it'll be run automatically even after power is removed and restored.

Voting with Pixl.js

More functionality

Since anything after a # is used as a vote, you can easily have more than two items to vote with. The following code for MakeCode will vote with a number between 0 and 9 (changed by pressing the A and B buttons)

Voting 0 to 9

You can then pick up the results in the votes variable. For instance to display a bar graph instead of 2 numbers, just change the code at the end of the setInterval to:

// now display on the screen
for (var i=0;i<10;i++) {
  g.fillRect(i*12, 55-(0|votes[i])*10, i*12+11, 55);
  g.drawString(0|votes[i], i*12+5, 60);

You could also use the Arduino connectors on the back of the Pixl.js to connect extra hardware that can be controlled by the Micro:bits, for instance motors, WiFi connection, or large LED displays.

This page is auto-generated from GitHub. If you see any mistakes or have suggestions, please let us know.