I`ve not had much shop time over the past few days with the run up to Christmas and family stuff but I have had a little time most evening to continue with developing my understanding of coding. Now I know you may think this is off topic but infact I realise I was nieve to say that I could just build this controller right from the start. I needed to start with something easier. So none of this is off topic, I`m learning how to code so that I can develop the code on my X-axis controller and not only that, an easier alternative project will also allow me to actually build a circuit and integrate a PIC start to finish. The traffic lights posted a few days ago were too basic and useless so I started a code for an electronic dice. I`ve posted all the code at the bottom for you if anyone is interested. Sadly I left my camera at my mother-in-laws so my only pictures have been taken with my phone so bad quality but I just wanted to show you what I`d been up to.
Basically there are two counts going on both going 1 to 6. The first is a delayed count and is being displayed while the button is pressed. The second is a fast count (1 to 6 a few hundred thousand times a second) and this is displayed when the button is released. Since the fast count is literally so fast it becomes random as it is impossible to release the button in sync with an outcome of your choice. I believe it could be hundreds of times slower and it would still be impossible. For all intent and purposes it is random. I initially choose a square of 3 x 3 leds to output the number on the dev board led clusters in a pattern like a dice:
1. 2. 3. 4. 5. 6.
XXX 1XX 1XX 1 X1 1X1 1X1
X1X XXX X1X XXX X1X 1X1
XXX XX1 XX1 1X1 1X1 1X1
It worked great but I didn`t take any pictures. The video is exactly the same but the LEDs are in a row but will need to be arranged and the same config or display will be obtained. The reason I`ve changed it was that the first pic I used was a 40 pin with 4 ports. To use the square config of the leds on the dev board I needed to use the three consecutive pins on each port - A to C. The pic I`m atually going to put into this board (the 40 pin is too big and too good for the job - it would be wasted!) is an old PIC16F627 which only had two ports. To be honest, it is better the way I`m doing it anyway as it allows me to have all the LEDs on one port it just means that I have to display them as a line on the dev board because of their position.
So here is the vid:
I now need to make the circuit which will end in a bit of machining with making the case!
Here is my “paint” circuit diagram!
![](http://www.raynerd.co.uk/images/leddicejpg.GIF)
And the start of the LED cluster:
![](http://www.raynerd.co.uk/images/dice1.jpg)
The pic socket, a resistor for each led and two 5k resistors in series as I needed 10k resistance for my pull up for the switch. You will also notice the switch is a tilt switch so will be activated when you shake the dice!
![](http://www.raynerd.co.uk/images/dice3.jpg)
Since I`ve put the pic on a socket it will allow me to remove it and update the code. There are a few other things I want it to do, for example, it would be good for the shaking effect when the button is pressed to last a set time even when the button is released and slow down as it gets nearer to displaying your number...that sort of stuff. We`ll see... then I`ll need to machine the case and at last we may see some swarf.
The code for anyone interested:
// define which pins our buttons are on
#define rick PORTB.b7
// led array - binary portB state for each number zero to 6.
char ledB[7] = {0b00000000, 0b00001000, 0b01000001, 0b01001001, 0b01010101, 0b01011101, 0b01110111};
char dice1; // index into array Leds for the "real" dice throw
char dice2; // index into array Leds for the shake slow dice throw while (button)
int loop; // defines an int called loop for misc use
void main()
{
// set all the pins on PORT B to 'off' before we start
PORTB = 0;
// set the tri-state buffer. Pins set to '1' are input, '0' are output. In this case PortB bits 0-6 as output for leds and bit 7 as input for button
TRISB = 0b10000000;
// set all the pins on PORT B to 'off' before we start
PORTB = 0;
//============================================================
//=================code proper starts=========================
//============================================================
// two dice throws are actually calculated - dice proper and a dice throw to display while shaking
// calculate dice
dice1 = 1; // dice1 is set to 1
dice2 = 1; // dice2 is set to 1
while(1) // loops this entire code forever as the condition is always 1
{
while (rick) // while button is pressed
{
// ==== This is dice2 the "shaking" display ======
loop++; // loop is set as an int and can therefore store numbers upto a few thousand so will loop through starting from 1 and adding + 1
if (loop > 6000) // when loop goes above 6000 loop is set to zero and therefore comes out of the while statement (this creates a count delay)
{
loop = 0;
dice2++; // add 1 to dice2
if (dice2 > 6) // cycles dice between 1 and 6 (if it goes over 6 it returns back to 1)
dice2 = 1; // as above. reset to 1.
PORTB = ledB[dice2]; // BUT the display shows the led array for the number it is on in each loop, hence the counting shaking effect
}
// ==== This is dice1 the actual throw and is running in the background very fast===
dice1++; // dice1 + 1
if (dice1 > 6) // when dice gets to 6 take it back to 1
dice1 = 1;
}
// Now out of the rick loop so when the button is not pressed it displays the results of dice1
PORTB = ledB[dice1];
}
}