need advise using a random no and incl. switch command

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BigT
    New Member
    • Apr 2008
    • 7

    need advise using a random no and incl. switch command

    I have 3 questions to the code below which is pretty messed up (newby).
    the program should pull a random number between 1-6 when i push a button and show it on an LCD (button and LCD are set up)
    1. how do i include the switch correctly
    2. how do i set up a random number counter
    3. how do read the number when i push the button and link the random no. to the LCD equivalent.

    Any help appreciated.

    Code:
    #define BUTTON	RA4
    
    #define HIGH	0	// Button is a pull down 
    #define LOW		1   // Button is a pull down
    
    #define Frequency	4000000
    #define InitDlay	12	
    
    #define	LCDTEST  0b01111111;	// Test LCD
    #define	LCD1  0b00000110;	// Bin 1
    #define	LCD2  0b01011011;	// Bin 2
    #define	LCD3  0b01001111;	// Bin 3
    #define	LCD4  0b01100110;	// Bin 4
    #define	LCD5  0b01101101;	// Bin 5
    #define	LCD6  0b01111100;	// Bin 6
    
    typedef unsigned char uint8_t;
    
    
    
    
    
    void main (void) 
    {
    	int RESULT;
    	int dlay;
    	uint8_t	i;
    // intitialize chip 
    	TRISA = 0x1F;		// Port A all inputs
    	TRISB = 0xFF;		// Port B all output
     		
    
    // Main task loop 
    	while (1) 
    	{
     	  button_state = BUTTON;	// get current button state	
    		
    	while(BUTTON == HIGH)	// wait while button isn't pressed
    	{	
    	
    	if ((button_state == LOW) && (last_button_state != HIGH))
    	{
    (((InitDlay-1)*256)*7)/(Frequency/4);	// debounce button
    						
    switch (RESULT) 
    {
    								CASE 1:
    								PORTB = LCD1;
    								break;
    
    								CASE 2:
    								PORTB = LCD2;
    								break;
    					
    								CASE 3:
    								PORTB = LCD3;
    								break;
    
    								CASE 4:
    								PORTB = LCD4;
    								break;
    
    								CASE 5:
    								PORTB = LCD5;
    								break;
    
    								CASE 6: 
    								PORTB = LCD6;
    								break;
    
    								default:
    								PORTB = LCDTEST;
    			}
    
    		}
    	}
    }
    	return;	
    }

    Thanks in advance,
    T
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    2. how do i set up a random number counter
    You can use srand() and rand() for this .But it returns double.So you may need to convert this to int.
    3. how do read the number when i push the button and link the random no. to the LCD equivalent.
    do like this int rand_num = srand()
    and rand_num will have the random number in it.

    [code=c]
    srand ( time(NULL) );
    int num = rand() %10

    [/code]

    Thanks
    Raghuram

    Comment

    • BigT
      New Member
      • Apr 2008
      • 7

      #3
      Thanks Raghuram for your answer.

      Could you give me a hint how i set the smallest no1 and the biggest to 6

      Thanks

      T

      Comment

      • gpraghuram
        Recognized Expert Top Contributor
        • Mar 2007
        • 1275

        #4
        Originally posted by BigT
        Thanks Raghuram for your answer.

        Could you give me a hint how i set the smallest no1 and the biggest to 6

        Thanks

        T
        R u asking how will u do it for the random number u are getting?
        If u want a single digit random number then try using srand48() and multiply by 10 to get a single digit random number.
        After that you can mod it by 6 to get what u want

        Thanks
        Raghuram

        Comment

        • BigT
          New Member
          • Apr 2008
          • 7

          #5
          Originally posted by gpraghuram
          R u asking how will u do it for the random number u are getting?
          If u want a single digit random number then try using srand48() and multiply by 10 to get a single digit random number.
          After that you can mod it by 6 to get what u want

          Thanks
          Raghuram

          I was trying to say to set a range of 1 trough 6 where 1 is the smallest and 6 the highest number like if you roll a die.

          Thx,
          T

          Comment

          • gpraghuram
            Recognized Expert Top Contributor
            • Mar 2007
            • 1275

            #6
            Originally posted by BigT
            I was trying to say to set a range of 1 trough 6 where 1 is the smallest and 6 the highest number like if you roll a die.

            Thx,
            T

            Do like this
            [code=c]
            srand48(time(NU LL));
            int num = ((int)(drand48( )*10))%5;
            num++;
            //num will be like rolling a dice it will return 1 to 6
            [/code]

            Thanks
            Raghuram

            Comment

            • BigT
              New Member
              • Apr 2008
              • 7

              #7
              Thank you for the help. This answers my questions.

              Thomas

              Comment

              Working...