' Make it with Micromite

' Mini-Project #1

' Electronic Dice (v1.01)



Option AUTORUN ON                               ' Run code on Power-Up (type RUN at Command Prompt to trigger this)

SetPin 4,dout : SetPin 5,dout : SetPin 6,dout   ' set three correct pins as Digital OUTputs

SetPin 14,din,Pullup                            ' set Pin 14 as a Digital INput ('Spin' button)



d=500                                           ' set how long a Dice number is shown for on LEDs

s1=&b101 : s2=&b111 : s3=&b111 : ShowNum        ' At Power-Up, count througn Dice numbers 1-6 briefly

s1=&b010 : s2=&b000 : s3=&b000 : ShowNum        ' (gives a nice Power-Up demo display)

s1=&b101 : s2=&b100 : s3=&b111 : ShowNum        ' (this is pattern for '3')

s1=&b010 : s2=&b100 : s3=&b000 : ShowNum        ' '4'

s1=&b100 : s2=&b101 : s3=&b010 : ShowNum        ' '5'

s1=&b100 : s2=&b010 : s3=&b011 : ShowNum        ' '6'

Blank                                           ' finally clear the LED display

d=10000                                         ' increases time that 'Dice Pattrn' shown for (i.e. Switch-Off delay)





Do                                              ' main loop here

  Blank                                         ' ensures the LED display is cleared

  waiting:

    If Pin(14)=1 Then GoTo waiting              ' waits for the Spin button to be pressed (i.e. =0)

  Pause 20                                      ' button pressed - so delay a bit to allow for contact bounce

  Randomize Timer                               ' makes sure random number sequence doesn't repeat (after a Power-Up)

  Do While Pin(14)=0                            ' while button pressed (i.e. =0) . . .

    Spin                                        ' . . . show 'Spin effect'

  Loop

  diceNum = Int(Rnd(0)*6)+1                     ' generate a random number between 1 and 6

  spinNum = Int(Rnd(0)*5)+1                     ' generate a random number (1-5) for how long to show spin effect

  Print "Dice =";diceNum;"  :  Spin =";spinNum  ' display the random number info on the Console screen

  For x=0 To spinNum                            ' loop the spin affect for the required number of 'spins'

    spin                                        '  . . . liken this to how hard the dice is being thrown!

  Next x



  Select Case diceNum                                  ' depending on random Dice value, show correct pattern on the LEDs

    Case 1 : s1=&b101 : s2=&b111 : s3=&b111 : ShowNum  ' use upto 3 'dot-pairs' (s1, s2, s3) to make correct pattern

    Case 2 : s1=&b010 : s2=&b000 : s3=&b000 : ShowNum     ' D3+D5 ...       ...

    Case 3 : s1=&b101 : s2=&b100 : s3=&b111 : ShowNum     ' D4    ... D1+D7 ...

    Case 4 : s1=&b010 : s2=&b100 : s3=&b000 : ShowNum     ' D3+D5 ... D1+D7 ...

    Case 5 : s1=&b100 : s2=&b101 : s3=&b010 : ShowNum     ' D1+D7 ...  D4   ... D3+D5

    Case 6 : s1=&b100 : s2=&b010 : s3=&b011 : ShowNum     ' D1+D7 ... D3+D5 ... D2+D6

  End Select

Loop



Sub Blank

  Port(4,3)=&b111                               ' setting the 3 LED output pins high will switch off ALL LEDs

End Sub



Sub Spin                                        ' dimly sequence the 3 pairs of LEDs to give a nice 'Spin effect'

 Port(4,3)=&b010 : Pause 1 : Blank : Pause 50   ' Bottom-Left AND Top-Right LEDs on (D3, D5)

 Port(4,3)=&b011 : Pause 1 : Blank : Pause 50   ' Middle-Left AND Middle-Right LEDs on (D2, D6)

 Port(4,3)=&b100 : Pause 1 : Blank : Pause 50   ' Top-Left, Bottom-Right (D1, D7)

End Sub



Sub ShowNum                                     ' Rapidly cycle through the 3 'dot-pairs' to create the pattern

  For i = 1 To d                                  ' will cycle through for a set number of times

    Port(4,3)=s1 : Pause 0.1                      ' presistence of vision will make pattern appear 'static'

    Port(4,3)=s2 : Pause 0.1

    Port(4,3)=s3 : Pause 0.1

    If Pin(14)=0 Then Exit Sub                    ' continually check for Spin button being pressed. If so then Spin again

  Next i

End Sub                                           ' if here (Spin not pressed). Will return to start of Do/Loop

                                                  ' Start of Do/Loop clears LEDs and waits for next Spin button press