5 Minute Wire Loop Game

About the project

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

Project info

Difficulty: Easy

Platforms: Espruino

Estimated time: 2 hours

License: GNU General Public License, version 3 or later (GPL3+)

Items used in this project

Hardware components

2621  ARM Espruino Pico 2621 ARM Espruino Pico x 1
PCD8544 LCD driver (Nokia 5110) PCD8544 LCD driver (Nokia 5110) http://www.espruino.com/PCD8544 x 1
Speaker - 0.5w (8 Ohm) Speaker - 0.5w (8 Ohm) http://www.espruino.com/Speaker x 1
Breadboard Breadboard http://www.espruino.com/Breadboard x 1
Solid core wire (around 0.6mm/22 AWG) Solid core wire (around 0.6mm/22 AWG) x 1

Software apps and online services

Espruino  Web IDE Espruino Web IDE

Story

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

An Espruino Pico
A Nokia 5110 LCD
A Speaker
Breadboard
Solid core wire (around 0.6mm/22 


Wiring up





Wiring up is pretty simple - see the picture above:

  • Place the Espruino Pico on the breadboard with the USB connector facing left, and as near the top as possible
  • Put the LCD directly above it, aligned to the right (so the pin on the Pico nearest the USB connector should not be connected to anything)
  • Put the Piezo Speaker below the Pico, with one pin as far left as possible (so the other pin is on pin B4)
  • Take a 20cm long piece of solid code wire and strip the final 5cm off one end, and 0.5cm off the other. Fold the long end around into a loop and twist it closed, and then plug the other end into the breadboard at the bottom right of the Espruino Pico (pin A8).
  • Take a 30cm piece of solid core wire and strip the middle 20cm (you can do this carefully with a knife, or can pull all of the insulation off and then push some back on the end.

  • Strip 0.5cm off each end, and bend the wire into whatever pattern you want (leaving some insulation at each end for the loop to rest on at the beginning and end of the game).

  • Thread the loop onto the bent wire, then push it into the pin at the very bottom left of the Pico (GND), and into the right-hand side of the breadboard where it won't be connected to anything else.
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:


  1. A5.write(0); // LCD GND
  2. A7.write(1); // LCD VCC
  3. var LOOP = A8; // the pin we've got our loop attached to
  4. var SPEAKER = B4; // where the speaker is attached
  5.  
  6. var g; // the LCD's graphics
  7. var score = 0; // current score
  8.  
  9. function drawScore() {
  10. g.clear();
  11. // draw the score
  12. g.setFontVector(40);
  13. g.drawString(score,(g.getWidth()-g.stringWidth(score))/2,0);
  14. // send the graphics to the display
  15. g.flip();
  16. }
  17.  
  18.  
  19. // flash and make a sound, and call the callback when done
  20. function hasHit(callback) {
  21. // increment the score...
  22. score++;
  23.  
  24. var i = 1;
  25.  
  26. // This will make the beeping and flashing
  27. function siren() {
  28. if (i>7) {
  29. clearInterval(interval);
  30. digitalWrite(LED1, 0); // turn the LED off
  31. // set normal colours on LCD
  32. g.setColor(1);
  33. g.setBgColor(0);
  34. // turn the sound off
  35. digitalRead(SPEAKER);
  36. callback();
  37. return;
  38. }
  39.  
  40. // every other time around the loop:
  41. if (i&1) {
  42. digitalWrite(LED1, 1); // turn red LED on
  43. // set inverted colours on LCD
  44. g.setColor(0);
  45. g.setBgColor(1);
  46. // beep high frequency
  47. analogWrite(SPEAKER, 0.5, {freq:1000});
  48. } else {
  49. digitalWrite(LED1, 0); // turn the LED off
  50. // set normal colours on LCD
  51. g.setColor(1);
  52. g.setBgColor(0);
  53. // beep low frequency
  54. analogWrite(SPEAKER, 0.5, {freq:700});
  55. }
  56.  
  57. // draw the text 'hit' on the LCD
  58. g.clear();
  59. g.setFontVector(40);
  60. g.drawString("Hit!",(g.getWidth()-g.stringWidth("Hit!"))/2,0);
  61. g.flip();
  62.  
  63. i++;
  64. }
  65. // call the siren function every so often (and call it once immediately)
  66. var interval = setInterval(siren, 300);
  67. siren();
  68. }
  69.  
  70.  
  71. function onInit() {
  72. // Setup SPI for LCD
  73. var spi = new SPI();
  74. spi.setup({ sck:B1, mosi:B10 });
  75. // Initialise the LCD
  76. g = require("PCD8544").connect(spi,B13,B14,B15, function() {
  77. // When it's initialised, draw the score and start
  78. drawScore();
  79. startWatchingLoop();
  80. });
  81. }
  82.  
  83.  
  84. // Add the internal pull-up resistor to the loop, so when it touches the wire (which is GND) it'll get pulled down
  85. pinMode(LOOP, "input_pullup");
  86. // This code watches the 'loop' to see if it has touched the wire
  87. var loopWatch;
  88. function startWatchingLoop() {
  89. // first, ensure we don't start watching the wire loop twice
  90. if (loopWatch) clearWatch(loopWatch);
  91. loopWatch = setWatch(function() {
  92. loopWatch = undefined;
  93. // now if we've touched, flash everything and make noises
  94. hasHit(function() {
  95. // When that's done, draw the score and start watching the loop again
  96. drawScore();
  97. startWatchingLoop();
  98. });
  99. }, LOOP, { repeat: false, edge: "falling" });
  100. }
  101.  
  102. // When button is pressed, reset the game:
  103. setWatch(function() {
  104. clearInterval();
  105. digitalRead(SPEAKER); // make sure speaker is off
  106. score = 0;
  107. drawScore();
  108. startWatchingLoop();
  109. }, BTN, { repeat: true, edge: "rising", debounce: 50 });
  110.  
  111.  
  112. // Finally, start everything going
  113. 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.

Code

Main code

Credits

Photo of Espruino

Espruino

Espruino, Espruino Pico and Puck.js are low-power Microcontrollers that run JavaScript. Espruino is a JavaScript Interpreter for Microcontrollers that is designed to make development quick and easy. The Espruino interpreter is firmware that runs on a variety of different microcontrollers, but we also make Espruino Boards that come with the interpreter pre-installed and are the easiest devices to get started with. However Espruino itself isn't just the interpreter firmware or hardware - there's also the Web IDE, command-line tools, documentation, tutorials, and modules that form a complete solution for embedded software development.

   

Leave your feedback...