Thread: Random function confusion

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    4

    Random function confusion

    Hello all,

    I hope everybody is having a wonderful January day.

    Ok, so I wrote this thread a few minutes ago, but for some reason my browser refreshed the page while I was writing it and I got logged out, so my thread was all gone. I apologize if I leave out specific details pertaining to my problem, but here goes round 2 for writing this thing.

    I'm using a PIC18F4520 green board built by my school with MPLABX IDE v1.95 (C18 compiler). For a particular assignment (mission #1 if you read the comments in the code), we students are supposed to program the green board so that when RB0 (button) is pressed, a "random" LED (RC0, RC1, RC2, or RC3) is lit up. Upon releasing RB0, all LEDs are off.

    When I run my code (see below) and press RB0, only RC0 lights up. When RB0 is released, the code works as it should and all LEDs are off.

    I have attempted putting a watch on the variable num (which I set as the value returned from the rand function), pausing the debugger, and trying to step through the code with RB0 pressed and not pressed at different times. No matter what, the value of num is always 0. My perception of the way a random function works is that when it is called, literally a random number is generated within a certain range (in this case, [0, 32767]). My plan was to use if statements to divide up this range and see which section of the range the value of num ends up in. Depending on which section it fell into, a specific LED would light up. I understand I have some magic numbers involved in my if statements. Those are the sections of the range I was referring to.

    One potential cause for this problem is the magic numbers themselves. I'm not sure if the if statements are seeing them as chars, ints, or longs, so they may be out of scope for the conditional statements. I don't think this is the problem though because the num variable always has a value of 0. It's as though the rand function isn't even working (or, that it's not working the way I think it'd work).

    One stipulation for this assignment, we are not supposed to use the srand() function and I don't think we are supposed to include <timers.h>. I have done some research before this and seen people using the srand() function and <timers.h>, but no examples of code that doesn't use either of those. Another thing I've noticed that commonly occurs with this random function, the value returned from the random function is almost always manipulated with the % operator. I understand this returns the remainder of a simple division, but I don't understand why its effect on the value returned by the rand function is so important. I would sincerely appreciate any advice or explanations as to what I've missed in my code. Thank you in advance for your help.

    Code:
    /********************************************************************
     * FileName:        LEDs following buttons example.c
     * Compiler:        MPLAB C18 v.3.06
     *
     * This program monitors Button RB0 (ie Port B bit 0)
     *   when it is pressed LED RC1 comes on.
     *
     * Your mission:
     *   #1 - Make RB0 turn on a random LED from RC3 to RC0
     *   #2 - Make RB1 turn on RC0 on the first press, then RC1 on
     *        next press, RC2 on the next, RC3 next, then RC0, . . .
     *
     * Author               Date        Comment
     *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    // 
    
    /** Processor Header Files *****************************************/
    #include <p18f4520.h>
    #include <stdlib.h>
    
    /** Define Constants Here ******************************************/
    #define PRESSED 0
    #define UNPRESSED 1
    char recentButtonState = UNPRESSED;
    volatile int num;
    
    /** Local Function Prototypes **************************************/
    int rand(void);
    
    // ============================================================
    // Configuration Bits 
    // ============================================================
    #pragma config OSC = INTIO67
    #pragma config WDT = OFF
    #pragma config LVP = OFF
    #pragma config BOREN = OFF
    #pragma config XINST = OFF
    /** Declarations *************************************************/
    
    /*****************************************************************
     * Function:        void main(void)
     *
     ******************************************************************/
    #pragma code
    void main(void) {
        ADCON1 = 0x0F; // Make sure the pins are digital
        TRISBbits.TRISB0 = 1; // Makes RB0 an input
        TRISC = 0b11110000; // Makes Port C bits RC0-RC3 output
        PORTC = 0x00; // Start with all of the lights off
        while (1) {
            num = rand();
    
    
            if ((PORTBbits.RB0 == PRESSED) && (recentButtonState == UNPRESSED)) {
                recentButtonState = PRESSED;
            }
            else if ((PORTBbits.RB0 == PRESSED) && (recentButtonState == PRESSED)) {
            }
            else {
                recentButtonState = UNPRESSED;
            }
    
    
            if ((PORTBbits.RB0 == 0) && (num >= 0) && (num <= 8191)) {
                PORTCbits.RC0 = 1;
            }
            else if ((PORTBbits.RB0 == 0) && (num > 8191) && (num <= 16382)) {
                PORTCbits.RC1 = 1;
            }
            else if ((PORTBbits.RB0 == 0) && (num > 16382) && (num <= 24573)) {
                PORTCbits.RC2 = 1;
            }
            else if ((PORTBbits.RB0 == 0) && (num > 24573) && (num <= 32767)) {
                PORTCbits.RC3 = 1;
            }
            else {
                PORTCbits.RC0 = 0, PORTCbits.RC1 = 0, PORTCbits.RC2 = 0, PORTCbits.RC3 = 0;
            }
        }
    }
    /*****************************************************************
     * Function:        int rand(void)
     *
     ******************************************************************/
    int rand(void) {
    }
    Last edited by MandySVO; 01-27-2014 at 01:02 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. while function with scanf function confusion
    By yukapuka in forum C Programming
    Replies: 7
    Last Post: 08-08-2012, 03:49 AM
  2. confusion over use of a function
    By flyingants in forum C Programming
    Replies: 8
    Last Post: 11-12-2010, 02:26 PM
  3. Elevator Function confusion
    By applescruff in forum C++ Programming
    Replies: 5
    Last Post: 12-16-2004, 10:14 AM
  4. Recursive function confusion
    By Sereby in forum C Programming
    Replies: 6
    Last Post: 07-22-2004, 12:45 PM
  5. Newbie function confusion...
    By funkydude9 in forum C++ Programming
    Replies: 9
    Last Post: 12-15-2002, 02:15 AM