LED DICE PROJECT

on Friday, 18 October 2013
In the 'Intermediate Project' section this is our 2nd project.

WHAT YOU WILL LEARN FROM THIS PROJECT?

  1. Generating Random Numbers
  2. Generating Led's simple pattern
  3. Using Functions in mikroC
  4. Using single dimensional array
AIM: Simple Dice project based on LEDs and Switch.

Explanation: 

block diagram led dice
Whenever push button is pressed then Led pattern will be generated. When button is pressed then a random number will be generated, depending on the number leds will glow.To indicate 1 only one led will glow and for 2 then 2 leds will glow, so on... Look at the pattern below
led dice numbers
Circuit Diagram:

led dice circuit
LEDs D1 to D7 are connected to PortC from Rc0 to Rc6 respectively.
To show 1, we need to glow D4 led i.e Rc3 should be '1' and other should be '0'
To show 2, we need to glow D2 and D6 i.e pins Rc1 and Rc5 should be '1' ,
so on...
Look at the below table to know which pin should be high for corresponding number.


Number
Rc0 pin
Rc1 pin
Rc2 Pin
Rc3 Pin
Rc4 Pin
Rc5 Pin
Rc6 Pin
Hex value
1
0
0
0
1
0
0
0
0x8
2
0
1
0
0
0
1
0
0x22
3
0
1
0
1
0
1
0
0x2A
4
1
0
1
0
1
0
1
0x55
5
1
0
1
1
1
0
1
0x5D
6
1
1
1
0
1
1
1
0x77



We generated hex values from the binary values. For example to generate 1 , PortC should be 0b0001000 if we convert this to hex we get 8 , so on..

Algorithm:

Continuously check switch
if switch is pressed
then generate any random number from 1 to 6
fed its hex value to portC
delay for 5 sec(to show the value for 5sec on dice)
again back jump to check switch

Program Code: 

//Function to generate random integer number between 1 and 'Limits'
unsigned char gen_numb(int Limits, int S)
{
   unsigned char result ;
   static unsigned int S;
   S=(S*32719 +3)%32749;
   result=((S%Limits)+1);
   return result;
}

//Main Program
void main()
{
    unsigned char k,pattern,seed=1; //set seed value depending upon your requirement
    unsigned char hex_value[]={0,0x08,0x22,0x2A,0x55,0x5D,0x77};
    TRISC=0;//Output PortC
    TRISB=1;//Input PortB
    PortC=0;// All leds off
    while(1)// infinite loop
    {
        if(Portb.f2= = 0)//If switch connected at portb.f2 is pressed
        {
           k=gen_numb(6,seed);//jump to function 'gen_numb' to generate random number
           pattern=hex_value[k];//generated number's hex value save in 'pattern' vairable
           PortC=pattern;//show result on leds which are connected at portc
           Delay_ms(5000);// wait for 5sec i.e dice result will stay for 5sec
           PortC=0;//after 5sec make all Leds off
        }
    }
}

AUTHOR: YOUSTRON SIC


0 comments:

Post a Comment