Home ArduinoArduino Tutorials Dice game using LOL Shield and a Wii Nunchuk

Dice game using LOL Shield and a Wii Nunchuk

We have a few examples of using the LOL Shield to produce some pretty patterns but how about a basic game, in this case a dice throwing game. I used a Wii Nunchuk for the button pressing (I was experimenting with this), you press the X button and the LOL Shield will display a random dice image from 1 to 6.

You could use a different input mechanism such as a button.

Requirements

A LOL Shield to display the dice images
An Arduino , I used an UNO
A WII Nunchcuk adaptor

I soldered header pins to A0 – A5 on the LOL shield and connected the nunchuk adaptor from A2 to A5 using wires. You can see this in the images below.

Pictures

lol shield and connector

lol shield and connector

nunchuk adaptor

nunchuk adaptor

Code

You will need the LOL Shield library and the WII Nunchuk libraries, download and extract them and put into the Arduino libraries folder

Here are the links for them

LoL Shield Library 

Wii Nunchuk library

Now for the code for the basic game

#include <Charliplexing.h> //Imports the library, which needs to be
#include <Wire.h>
#include “nunchuck_funcs.h”

byte line = 0; //Row counter
char buffer[10];
int value;
int randNumber;
byte zbut;
int loop_cnt=0;

void setup()
{
LedSign::Init(); //Initializes the screen
randomSeed(analogRead(0));
nunchuck_setpowerpins();
nunchuck_init(); // send the initilization handshake
//Serial.begin(19200);
}

void loop()
{
if( loop_cnt > 100 )
{ // every 100 msecs get new data
loop_cnt = 0;
nunchuck_get_data();
zbut = nunchuck_zbutton();
//Serial.print(“\tzbut: “); Serial.print((byte)zbut,DEC);
if (zbut == HIGH)
{
randNumber = random(1,7);
throwDice(randNumber);
delay(100);
}
}
loop_cnt++;
delay(1);
}

void DisplayBitMap(int lineint)
{
//int data[9] = {95, 247, 123, 511, 255, 1, 5, 31, 15};

//for(line = 0; line < 9; line++) {
for (byte led=0; led<14; ++led) {
if (lineint & (1<<led)) {
LedSign::Set(led, line, 1);
} else {
LedSign::Set(led, line, 0);
}
}

line++;
if(line >= 9) line = 0;
}

void throwDice(int Number)
{
switch(Number)
{
case 1: delay(1000);
DisplayBitMap(4088);
DisplayBitMap(2056);
DisplayBitMap(2056);
DisplayBitMap(2056);
DisplayBitMap(2184);
DisplayBitMap(2056);
DisplayBitMap(2056);
DisplayBitMap(2056);
DisplayBitMap(4088);
break;
case 2: delay(1000);
DisplayBitMap(4088);
DisplayBitMap(2056);
DisplayBitMap(2088);
DisplayBitMap(2056);
DisplayBitMap(2056);
DisplayBitMap(2056);
DisplayBitMap(2568);
DisplayBitMap(2056);
DisplayBitMap(4088);
break;
case 3: delay(1000);
DisplayBitMap(4088);
DisplayBitMap(2056);
DisplayBitMap(2088);
DisplayBitMap(2056);
DisplayBitMap(2184);
DisplayBitMap(2056);
DisplayBitMap(2568);
DisplayBitMap(2056);
DisplayBitMap(4088);
break;
case 4: delay(1000);
DisplayBitMap(4088);
DisplayBitMap(2056);
DisplayBitMap(2600);
DisplayBitMap(2056);
DisplayBitMap(2056);
DisplayBitMap(2056);
DisplayBitMap(2600);
DisplayBitMap(2056);
DisplayBitMap(4088);
break;
case 5: delay(1000);
DisplayBitMap(4088);
DisplayBitMap(2056);
DisplayBitMap(2600);
DisplayBitMap(2056);
DisplayBitMap(2184);
DisplayBitMap(2056);
DisplayBitMap(2600);
DisplayBitMap(2056);
DisplayBitMap(4088);
break;
case 6: delay(1000);
DisplayBitMap(4088);
DisplayBitMap(2056);
DisplayBitMap(2600);
DisplayBitMap(2056);
DisplayBitMap(2600);
DisplayBitMap(2056);
DisplayBitMap(2600);
DisplayBitMap(2056);
DisplayBitMap(4088);
break;
default: delay(1000);
}
}

 

Lets see it in action

lol shield dice image

lol shield dice image

Links
US Links

LOL Shield on Ebay US
Nintendo Wii adapter

UK Links

LOL Shield on Ebay UK
Nintendo Wii adaptor

You may also like