A0.write(1); // LCD Light A1.write(1); // LCD VCC var LOOP = C0; // the pin we've got our loop attached to var SPEAKER = B9; // where the speaker is attached var BTN_RST = C1; // where the Reset button 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 SPI1.setup({ baud: 1000000, sck:B3, mosi:B5 }); g = Graphics.createArrayBuffer(84,48,1,{vertical_byte:true}); var spi = SPI1; var dc = B6; var ce = B7; var rst = B8; digitalPulse(rst, 0, 10); // pulse reset low setTimeout(function() { digitalWrite(dc,0); // cmd spi.send( [0x21, // fnset extended 0x80 | 0x25, // setvop (experiment with 2nd val to get the right contrast) 0x14, // setbias 4 0x04 | 0x02, // temp control 0x20, // fnset normal 0x08 | 0x04], ce); // dispctl normal }, 100); g.flip = function () { for (var i=0;i<6;i++) { digitalWrite(dc,0); // cmd spi.send(0x40|i, ce); // Y addr spi.send(0x80, ce); // X addr digitalWrite(dc,1); // data spi.send(new Uint8Array(g.buffer,i*84,84), ce); } }; 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"); pinMode(BTN_RST, "input_pulldown"); // 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