Home ArduinoArduino Code Random dice using a TIl311

Random dice using a TIl311

This example is a random number example using a TIl311 and a Wii nunchuk adaptor. Press the X button to generate a random number between 1 and 6.

This was a prototype of the dice example, of course its not a true dice but displays numbers between 1 and 6

 

#include <Wire.h>
#include “nunchuck_funcs.h”

//TIl311 pins
#define BLANK_INPUT 6
#define LATCH_DATA_A 2
#define LATCH_DATA_B 3
#define LATCH_DATA_C 4
#define LATCH_DATA_D 5

int randNumber;
byte zbut;
int loop_cnt=0;

void display (uint8_t value)
{
/* Send data to the latch */
digitalWrite (LATCH_DATA_A, bitRead (value, 0));
digitalWrite (LATCH_DATA_B, bitRead (value, 1));
digitalWrite (LATCH_DATA_C, bitRead (value, 2));
digitalWrite (LATCH_DATA_D, bitRead (value, 3));
}

void setup ()
{
//setup the TIL311
pinMode (BLANK_INPUT, OUTPUT);
pinMode (LATCH_DATA_A, OUTPUT);
pinMode (LATCH_DATA_B, OUTPUT);
pinMode (LATCH_DATA_C, OUTPUT);
pinMode (LATCH_DATA_D, OUTPUT);
digitalWrite (BLANK_INPUT, LOW);
//seed the random number generator
randomSeed(analogRead(0));
//wii nunchuk setup
nunchuck_setpowerpins();
nunchuck_init(); // send the initilization handshake
}

void loop ()
{
if( loop_cnt > 100 )
{
loop_cnt = 0;
nunchuck_get_data();
zbut = nunchuck_zbutton();
if (zbut == HIGH)
{
randNumber = random(1,7);
display(randNumber);
delay(100);
}
}
loop_cnt++;
delay(1);
}

You may also like