Slot Machine

Introduction

We'll make a simple Slot Machine with 4 LEDs. When the button is pressed, they'll each 'spin', changing colour until they slow down.

You'll Need

Wiring Up

Final wiring

Now the tricky part... The LED lights need wiring up to GND and VCC, and also need their data pins connected together.

APA106 pinout

Now check the wiring is correct and that the pins of the LEDs are not shorted out (see the picture below). If 5v and GND are the wrong way around, you could destroy your LEDs.

Wiring up

Software

Ok, now we've got that sorted we can experiment with sending data to the LEDs. When you plug the board into USB the 4 lights should light up blue. If not, check that they're properly seated in the breadboard.

On the left-hand side of the Web IDE, paste the following:

var leds = new Uint8ClampedArray(4*3);
SPI2.setup({baud:3200000, mosi:B15});
function updateLEDs() { SPI2.send4bit(leds, 0b0001, 0b0011); }
updateLEDs();

This creates a blank array of data for the 4 LEDs, sets up SPI, and then sends the data to them (turning them off). There are 4 LEDs, each of which have a byte for red, green, and blue (so 12 elements in total).

Try changing what's in the array, and re-sending the data:

leds[0] = 255; // 1st red
leds[3+1] = 255; // 2nd green
leds[6+2] = 255; // 3rd blue
updateLEDs();

So now we've got the LEDs working, we'll make the slow machine. To do it, we'll create a class for each 'wheel' of the slot machine. You can either copy and paste this into the right-hand side of Espruino, and click 'Send to Espruino'.

var leds = new Uint8ClampedArray(4*3);
SPI2.setup({baud:3200000, mosi:B15});
function updateLEDs() { SPI2.send4bit(leds, 0b0001, 0b0011); }


function Wheel(ledOffset) {
  this.ledOffset = ledOffset;
  // velocity
  this.vel = 0;
  // position
  this.pos = Math.random();
}

// Give the wheel a random speed
Wheel.prototype.spin = function() {
  this.vel = 1+Math.random()*10;
};

/* each step of animation, decrease the wheel's
velocity and move it onwards */
Wheel.prototype.step = function() {
  this.vel *= 0.98;
  this.pos += this.vel*0.02;
  // make sure the position is always between 0 and 1
  this.pos -= Math.floor(this.pos);
  // update the colour to one of six. We don't use 255
  // here because we'd basically blind ourselves!
  var col = [
    [63,0,0],
    [47,47,0],
    [0,63,0],
    [0,47,47],
    [0,0,63],
    [47,0,47]][Math.floor(this.pos*6)];
  // set the colour in the LEDs array
  leds.set(col, this.ledOffset);
};

var wheels = [
  new Wheel(0*3),
  new Wheel(1*3),
  new Wheel(2*3),
  new Wheel(3*3),
  ];


function step() {
  // update the movement and set up the LEDs array
  for (var i in wheels)
    wheels[i].step();
  // send to the LEDs
  updateLEDs();
}

function spin() {
  for (var i in wheels)
    wheels[i].spin();
}

// update the LEDs 50 times a second
setInterval(step, 20);
// when the button is pressed, spin!
setWatch(spin, BTN, {repeat:true, edge:"rising", debounce: 100});

Now you can just press the button to 'spin' the Slot machine.

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