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


Closed Thread
Results 1 to 14 of 14

Hybrid View

  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?

Members who have read this thread : 0

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