random numbers 1-4


Closed Thread
Results 1 to 4 of 4
  1. #1
    tekdavid's Avatar
    tekdavid Guest

    Question random numbers 1-4

    in picbasic standard how would you generate a random number 1 - 4 then make leds 1 -4 light up in that order i have been unable to get it working just right i though some on out the could give me some new thoughts on how to do that.

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Everytime you use Random, you in effect seed a new value in the word variable RandomValue. You only want the bottom two bits (to give you your four possible values) so extract them... Use those bits to illuminate your LED's... simple...

    Code:
    	LEDA var PortB.0
    	LEDB var PortB.1
    	LEDC var PortB.2
    	LEDD var PortB.3
    
    	RandomValue var WORD
    	LEDValue var BYTE
    
    Start:
    	Low LEDA
    	Low LEDB
    	Low LEDC
    	Low LEDD
    
    	Random RandomValue
    	LEDValue=RandomValue & $03
    
    	If LEDValue=0 then High LEDA
    	If LEDValue=1 then High LEDB
    	If LEDValue=2 then High LEDC
    	If LEDValue=3 then High LEDD
    
    	Pause 1000
    	Goto Start
    This will give you one-of-four Random selection forever. I haven't got PBC but I'm sure you could convert it simply...

  4. #4
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    OK... looking at the PBC manual... here's the conversion... sombody can correct me if I've made a mistake...

    Code:
    	' LEDA is on PortB.0
    	' LEDB is on PortB.1
    	' LEDC is on PortB.2
    	' LEDD is on PortB.3
    
    	' RandomValue is variable W0
    	' LEDValue is variable B2
    
    Start:
    	Low 0
    	Low 1
    	Low 2
    	Low 3
    
    	Random W0
    	B2=W0 & $03
    
    	If B2=0 then High 0
    	If B2=1 then High 1
    	If B2=2 then High 2
    	If B2=3 then High 3
    
    	Pause 1000
    	Goto Start
    Well, actually, you could probably reduce it further...

    Code:
    Start:
    	Low 0
    	Low 1
    	Low 2
    	Low 3
    
    	Random W0
    	B2=W0 & $03
    
    	High B2
    
    	Pause 1000
    	Goto Start

Similar Threads

  1. Thermo 7 segments - little problem
    By fratello in forum mel PIC BASIC Pro
    Replies: 49
    Last Post: - 27th July 2013, 07:31
  2. RS485 bus - starting probem
    By wurm in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 27th January 2010, 13:35
  3. 32 bit square root
    By Charles Linquis in forum mel PIC BASIC Pro
    Replies: 20
    Last Post: - 6th May 2009, 03:37
  4. one line led light make image
    By bioul in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 13th February 2008, 12:19
  5. HSERIN doesn´t work
    By wurm in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 29th July 2007, 14:23

Members who have read this thread : 2

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