Random number -> Corresponding LED lit (n00besque content contained)


Closed Thread
Results 1 to 14 of 14
  1. #1
    Join Date
    Mar 2009
    Posts
    653

    Default Random number -> Corresponding LED lit (n00besque content contained)

    (& I'd search but I don't know what the search terms would be!)

    Ok, I have six LEDs, declared like thus...

    LED1 var PortC.0
    LED2 var PortA.1
    LED3 var PortA.2
    LED4 var PortC.5
    LED5 var PortA.4
    LED6 var PortA.5

    I want to get the LEDs to light randomly, so I'm gonna do a 6 number random number generator, ie something like this...

    MyWord var Word
    MyByte var Byte
    Random MyWord
    MyByte=(MyWord//6)+1

    which, if I've plagiarized correctly, should result in random number between 1 & 6..... *but* how do I get the result of the random number generator mapped to a corresponding led? (therefore if the random number is 3, I want LED3 to light up, if the random number is 6, then I want LED6 to light etc).

    Many thanks

  2. #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    LED1 = !(MyByte != 1)
    LED2 = !(MyByte != 2)
    LED3 = !(MyByte != 3)
    LED4 = !(MyByte != 4)
    LED5 = !(MyByte != 5)
    LED6 = !(MyByte != 6)
    DT

  3. #3
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Random number -> Corresponding LED lit (n00besque content contained)

    Quote Originally Posted by Darrel Taylor View Post
    LED1 = !(MyByte != 1)
    LED2 = !(MyByte != 2)
    LED3 = !(MyByte != 3)
    LED4 = !(MyByte != 4)
    LED5 = !(MyByte != 5)
    LED6 = !(MyByte != 6)
    Excellent, the top of my fishing rod is moving a touch!

    but I don't understand the response!

    eg if the random number comes out as 1, meaning I want to light LED1.....normally - if wanting to light LED1 for 200ms, I'd simply do this....
    Code:
    HIGH LED1
    PAUSE 200
    LOW LED1
    PAUSE 200
    but, how do I do this......

    Code:
    HIGH LED'result_of_random_number'
    PAUSE 200
    LOW LED'result_of_random_number'
    PAUSE 200
    Apologies if that is what you just told me Darrel, but I'm not sure how to use the info you gave me (that'll be the n00bishness that's never far away from all I do!)

  4. #4
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Change the conditions on me eh ...

    How about ...
    Code:
    ;---------------------------------------
    Main:
        Random MyWord
        MyByte=(MyWord//6)+1
        GOSUB HIGH_LED
        PAUSE 200
        GOSUB LOW_LED
        PAUSE 200
    GOTO Main
    ;---------------------------------------
     
    HIGH_LED:
      SELECT CASE MyByte
        CASE 1 : HIGH LED1
        CASE 2 : HIGH LED2
        CASE 3 : HIGH LED3
        CASE 4 : HIGH LED4
        CASE 5 : HIGH LED5
        CASE 6 : HIGH LED6
      END SELECT
    RETURN
     
    ;---------------------------------------
    LOW_LED:
      SELECT CASE MyByte
        CASE 1 : LOW LED1
        CASE 2 : LOW LED2
        CASE 3 : LOW LED3
        CASE 4 : LOW LED4
        CASE 5 : LOW LED5
        CASE 6 : LOW LED6
      END SELECT
    RETURN
    DT

  5. #5
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Random number -> Corresponding LED lit (n00besque content contained)

    Hey Darrel...that works a treat ...I've now got pretty twinkly Blue leds randomly flashing before my eyes (which strangely, makes me feel warm inside).

    I note that a fair few of the random numbers are the same as the previous (I guess when you've only six to choose from, then that's bound to happen!), so I'll sit down for a few weeks & try & code this out!

    Many thanks!

    Hank.

    Edit: Actually, dialing out the successive repeats only took a couple of minutes...

    Code:
    MyWord var Word
    MyByte var Byte
    previous_random var Byte
    ;---------------------------------------
    Main:
        Random MyWord
        MyByte=(MyWord//6)+1
        if previous_random = MyByte then goto Main
        GOSUB HIGH_LED
        PAUSE 50
        GOSUB LOW_LED
        PAUSE 50
        previous_random = MyByte
    GOTO Main
    ;---------------------------------------
     
    HIGH_LED:
      SELECT CASE MyByte
        CASE 1 : HIGH LED1
        CASE 2 : HIGH LED2
        CASE 3 : HIGH LED3
        CASE 4 : HIGH LED4
        CASE 5 : HIGH LED5
        CASE 6 : HIGH LED6
      END SELECT
    RETURN
     
    ;---------------------------------------
    LOW_LED:
      SELECT CASE MyByte
        CASE 1 : LOW LED1
        CASE 2 : LOW LED2
        CASE 3 : LOW LED3
        CASE 4 : LOW LED4
        CASE 5 : LOW LED5
        CASE 6 : LOW LED6
      END SELECT
    Last edited by HankMcSpank; - 22nd April 2011 at 22:48.

  6. #6
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Random number -> Corresponding LED lit (n00besque content contained)

    Just had a sleep on this - I've never previously used case, and now I fresh look at it, to my eyes it's just a load of IFs?

    For example....

    Code:
    HIGH_LED:
      SELECT CASE MyByte
        CASE 1 : HIGH LED1
        CASE 2 : HIGH LED2
        CASE 3 : HIGH LED3
        CASE 4 : HIGH LED4
        CASE 5 : HIGH LED5
        CASE 6 : HIGH LED6
      END SELECT
    RETURN
    could be written as...

    Code:
    IF MYBYTE = 1 THEN HIGH LED1
    IF MYBYTE = 2 THEN HIGH LED2
    IF MYBYTE = 3 THEN HIGH LED3
    IF MYBYTE = 4 THEN HIGH LED4
    IF MYBYTE = 5 THEN HIGH LED5
    IF MYBYTE = 6 THEN HIGH LED6
    So when to use case & when to use if?

    Going back 20 years to when I used to write simple VMS DCL programs, I could construct code like thus...

    HIGH "LED"+'MyByte'

    the stuff inbetween " " was text & stuff in the ' ' was the value held in a variable

    therefore the above would actually run as.....

    HIGH LED1 (assuming MyByte held 1) ......presumably no similar ways of constucting in Picbasic?

  7. #7
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default Re: Random number -> Corresponding LED lit (n00besque content contained)

    Select Case is a lot like a "Load of IF's" with a few differences.
    1. It removes some of the (n00besque content contained) look and feel.
    2. Only the first "CASE" that evaluates TRUE will execute, then it exits the SELECT block.
      In your list of IF's, if the first one is true, it still goes on and evaluates all the other IF's too.
      This allows more complex logic, where more than one test can be true, but only the first one is acted on.
    The same functionality using IF statements would look like this ...
    Code:
    IF MYBYTE = 1 THEN
        HIGH LED1
    ELSE
        IF MYBYTE = 2 THEN
            HIGH LED2
        ELSE
            IF MYBYTE = 3 THEN
                HIGH LED3
            ELSE
                IF MYBYTE = 4 THEN
                    HIGH LED4
                ELSE
                    IF MYBYTE = 5 THEN
                        HIGH LED5
                    ELSE
                        IF MYBYTE = 6 THEN
                            HIGH LED6
                        ENDIF
                    ENDIF
                ENDIF
            ENDIF
        ENDIF
    ENDIF
    OR, if you have PBP 2.60 you could do it like this ...
    Code:
    IF MYBYTE = 1 THEN 
        HIGH LED1
    ELSEIF MYBYTE = 2 THEN 
        HIGH LED2
    ELSEIF MYBYTE = 3 THEN 
        HIGH LED3
    ELSEIF MYBYTE = 4 THEN 
        HIGH LED4
    ELSEIF MYBYTE = 5 THEN 
        HIGH LED5
    ELSEIF MYBYTE = 6 THEN 
        HIGH LED6
    ENDIF
    It may not seem like much difference with this simple example, but with large SELECT CASE blocks using more complex comparisons, it can save a boat load of execution time not having to evaluate every condition.

    * SELECT CASE is also much easier to read. Especially when it's not your program.

    PicBasic Pro, was written to generate the Smallest and Fastest code possible. As it is the HIGH/LOW statements only use a couple of instruction cycles. If you add in the abilities to use them like array's and access pins non-contiguously, it would make EVERY HIGH/LOW statement Bigger and Slower, breaking everyone's code, and causing more complaints than not having those abilities produces.
    Last edited by Darrel Taylor; - 23rd April 2011 at 16:29.
    DT

  8. #8
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Random number -> Corresponding LED lit (n00besque content contained)

    Many thanks Darrel....crystal clear now

  9. #9
    Join Date
    Sep 2010
    Location
    Las Vegas, NV
    Posts
    305


    Did you find this post helpful? Yes | No

    Default Re: Random number -> Corresponding LED lit (n00besque content contained)

    I've tried to randomize 20 LEDs hooked up as shown in the attachment with the following code:
    (Sorry, I've had to retype it. Long story.)
    Code:
    clear
    define osc4
    ansel=0
    comcon0=7
    adcon0=0
     
    delay var word
    myword var word
    mybyte var byte
    previous_random var byte
    gpiostate var byte
    tristate var byte
     
    delay = 250
     
    main:
    random myword
    mybyte = (myword//20)
    if previous_random = mybyte then goto main
    gosub high_led
    pause delay
    gpio = %000000
    pause delay
    previous_random= mybyte
    goto main
     
    high led:
    lookup mybyte,[%000001, etc for 20 LEDs], gpiostate
    gpio = gpiostate
    lookup mybyte,[%111100, etc for 20 LEDs], tristate
    trisio = tristate
    return
    The problem I'm having is two fold.

    First, LED0 never lights. I've watched it for a about 3 minutes several times and that first LED never lights. Any ideas why?

    Secondly, although I implement the previous_random scheme shown previously in the thread, I often get an LED that will light repeatedly before moving on. Any ideas why?

    As always, most of your thoughts are appreciated.
    Attached Images Attached Images
    Last edited by Archangel; - 14th June 2011 at 04:57. Reason: CODE TAGS

  10. #10
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Re: Random number -> Corresponding LED lit (n00besque content contained)

    We really need to see the entire code esp. the lookup tables.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  11. #11
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Random number -> Corresponding LED lit (n00besque content contained)

    It worked perfectly for me ....any reason you have not copied the code verbatim?

    Re LED1 never lighting, what happens if you comment out the random aspect & simple make mybyte = 1 (where 1 is the LED you're not seeing lighting up etc)...this should light the suspect LED up all the time & proves both the wiring/LED at least.

    Re the same LED lighting in succession, I'd approach this by getting some debug/hserout info of 'mybyte' onscreen....else you're grasping in the dark. (no pun intended!)

  12. #12
    Join Date
    Sep 2010
    Location
    Las Vegas, NV
    Posts
    305


    Did you find this post helpful? Yes | No

    Default Re: Random number -> Corresponding LED lit (n00besque content contained)

    I did run another program to ensure all the LEDs are hooked up and working. They all work using the cut and pasted look up tables provided.

    Hank, when you say it worked perfectly did you use the schematic I provided? The last time I chased a problem with these LEDs, it turned out to be the circuit not the code. If you look here http://www.picbasic.co.uk/forum/show...ght=wood+chuck you'll see what I mean.

    Here are the look up tables.

    lookup mybyte,[%000001, %000010, %000001, %000100, %000001, %010000, %000001, %100000, %000010, %000100, %000010, %010000, %000010, %100000, %000100, %010000, %000100, %100000, %010000, %100000], gpiostate
    gpio= gpiostate
    lookup mybyte,[%111110, %111100, %111010, %111010, %101110, %101110, %011110, %011110, %111001, %111001, %101101, %101101, %011101, %011101, %101011, %101011, %011011, %011011, %001111, %001111],tristate
    trisio= tristate

    I don't currently have the knowledge to display my number on screen but if the LED is lit by another program, proving it does work, then looking at a number will only tell me that the LEDs number never comes up.

  13. #13
    Join Date
    Feb 2008
    Location
    Michigan, USA
    Posts
    231


    Did you find this post helpful? Yes | No

    Default Re: Random number -> Corresponding LED lit (n00besque content contained)

    I wonder if someone could explain the
    "MyByte=(MyWord//6)+1" part? // = Remainder (Modulus)? It isn't clear to me what this is doing and how it is extracting the 6 variables.

    I need to randomize 8 LEDs and would like to understand it better.

    Thanks
    Bo

  14. #14
    Join Date
    Sep 2010
    Location
    Las Vegas, NV
    Posts
    305


    Did you find this post helpful? Yes | No

    Default Re: Random number -> Corresponding LED lit (n00besque content contained)

    If you do the math with a calculator you'll, in my opinion, never understand modulus. Use paper and pencil for some small numbers and you'll see how modulus works.

    I believe to randomize 8 LEDs all you need to do is change the 6 to an 8. Like
    Random myword
    mybyte=(myword//8)

    Depending on how you use mybyte depends on whether or not you need to +1 after myword//8.

    I'm sure someone will chime in and say I've messed up but there you have it.

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts