5 Minute Wire Loop Game

Introduction

This is a really quick version of the Wire Loop Game. Try and move the loop along the wire without touching it - otherwise an alarm goes off!

Because we're using the Pico it's easy to create a noise, flash lights, and to keep track of how many times the wire has been hit.

You'll Need

Wiring Up

Wiring

Wiring up is pretty simple - see the picture above:

And that's it!

Software

Now just copy and paste the following code into the right-hand side of the Web IDE and click Send to Espruino. Some comments are in the code to explain how it works:

A5.write(0); // LCD GND
A7.write(1); // LCD VCC
var LOOP = A8; // the pin we've got our loop attached to
var SPEAKER = B4; // where the speaker is attached

var g;         // the LCD's graphics
var score = 0; // current score

function drawScore() {
  g.clear();
  // draw the score
  g.setFontVector(40);
  g.drawString(score,(g.getWidth()-g.stringWidth(score))/2,0);
  // send the graphics to the display
  g.flip();
}


// flash and make a sound, and call the callback when done
function hasHit(callback) {
  // increment the score...
  score++;

  var i = 1;

  // This will make the beeping and flashing
  function siren() {
    if (i>7) {
      clearInterval(interval);
      digitalWrite(LED1, 0); // turn the LED off
      // set normal colours on LCD
      g.setColor(1);
      g.setBgColor(0);
      // turn the sound off
      digitalRead(SPEAKER);
      callback();
      return;
    }

    // every other time around the loop:
    if (i&1) {
      digitalWrite(LED1, 1); // turn red LED on
      // set inverted colours on LCD
      g.setColor(0);
      g.setBgColor(1);
      // beep high frequency
      analogWrite(SPEAKER, 0.5, {freq:1000});
    } else {
      digitalWrite(LED1, 0); // turn the LED off
      // set normal colours on LCD
      g.setColor(1);
      g.setBgColor(0);
      // beep low frequency
      analogWrite(SPEAKER, 0.5, {freq:700});
    }

    // draw the text 'hit' on the LCD
    g.clear();
    g.setFontVector(40);
    g.drawString("Hit!",(g.getWidth()-g.stringWidth("Hit!"))/2,0);
    g.flip();

    i++;
  }
  // call the siren function every so often (and call it once immediately)
  var interval = setInterval(siren, 300);
  siren();
}


function onInit() {
  // Setup SPI for LCD
  var spi = new SPI();
  spi.setup({ sck:B1, mosi:B10 });
  // Initialise the LCD
  g = require("PCD8544").connect(spi,B13,B14,B15, function() {
    // When it's initialised, draw the score and start
    drawScore();
    startWatchingLoop();
  });
}


// Add the internal pull-up resistor to the loop, so when it touches the wire (which is GND) it'll get pulled down
pinMode(LOOP, "input_pullup");
// This code watches the 'loop' to see if it has touched the wire
var loopWatch;
function startWatchingLoop() {
  // first, ensure we don't start watching the wire loop twice
  if (loopWatch) clearWatch(loopWatch);
  loopWatch = setWatch(function() {
    loopWatch = undefined;
    // now if we've touched, flash everything and make noises
    hasHit(function() {
      // When that's done, draw the score and start watching the loop again
      drawScore();
      startWatchingLoop();
    });
  }, LOOP, { repeat: false, edge: "falling" });
}

// When button is pressed, reset the game:
setWatch(function() {
  clearInterval();
  digitalRead(SPEAKER); // make sure speaker is off
  score = 0;
  drawScore();
  startWatchingLoop();
}, BTN, { repeat: true, edge: "rising", debounce: 50 });


// Finally, start everything going
onInit();

If you touch the wire as you move the loop across, the siren will sound and the score will increment. To reset everything, just touch the button and the score will be reset to 0.

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