random problem


Closed Thread
Results 1 to 6 of 6

Thread: random problem

  1. #1
    Join Date
    Oct 2007
    Location
    The Netherlands
    Posts
    45

    Default random problem

    Hello everyone,
    I have a strange problem, I wrote a little program to control a VMUSIC2 MP3 player. It plays an MP3 with morse beeps that will then switch a lamp. The user can change the MP3 file without having to mess with the program.
    After playing the file the player should wait between 0 and 45 minutes randomly before playing the morse code again.
    All works okay but after a while the randomness disappears and it will always wait 45 minutes. Seems something wrong with the random command, maybe the seed? any help is appreciated. Here's the code:
    Code:
    ' 12-05-09 morse code player, reads mp3 file to control lamp
    
    @ device  pic12F683, INTRC_OSC_NOCLKOUT, wdt_off, pwrt_on, mclr_off, protect_off
    
    include "modedefs.bas"
    define  OSC 4
                               
    OSCCON     = %01100000      ' Set to 4MHz	
    CMCON0     = %00000111	    ' Analog comparators off 	
    ANSEL      = %00000010	    ' Set AN1 as analog, rest digital 	    
    TRISIO     = %00100010	                       
    GPIO       = %00010000      ' GPIO.4 hoog voor serieel, start anders niet
    
    relay       var GPIO.0
    led         var GPIO.2
    
    counter     var word
    temp        var word
    time        var word
    audio       var byte 
    
    pause 2000
    
    loop:
         SerOut GPIO.4,T2400,["V3A",13]          ' play all songs
    play_morse:
         toggle led
         adcin 1,audio
         if audio > 25 then                      ' test sound (more than 500 mV)
             high relay
         else
             low relay
         endif     
         SERIN2 GPIO.5,396,20,play_morse,[wait ("Stopped")]
         random temp
         time = temp/24                          ' between 0 and 45 seconds
         for counter = 1 to time
            pause 1000
            toggle led
         next counter
    goto loop 
        
    End
    Last edited by eggman; - 21st May 2009 at 18:34.

  2. #2
    Join Date
    Sep 2007
    Location
    USA, CA
    Posts
    271


    Did you find this post helpful? Yes | No

    Default

    If you read the section on Random, you will see that your output variable is also used as the seed. Try to use Random Time instead of Random Temp. That way your seed is changed after each use. In your code Temp is unnecessary anyway.

  3. #3
    Join Date
    Oct 2007
    Location
    The Netherlands
    Posts
    45


    Did you find this post helpful? Yes | No

    Default

    I'm not so sure about that, if I use time as my seed it will always be smaller then 2730, (time / 24) Will this not affect the output of the random generator?
    i.e. does the seed need to be a 16 bit number or not?

  4. #4
    Join Date
    Oct 2007
    Location
    The Netherlands
    Posts
    45


    Did you find this post helpful? Yes | No

    Default

    Okay, I did a little test:
    If I try this code:

    Code:
    loop:
         random temp
         toggle LED
         pause temp >> 8
    goto loop
    My LED flashes in a very nicely random way.

    If I try this code:

    Code:
    loop:
         random temp
         temp = temp >> 8
         toggle LED
         pause temp
    goto loop
    Then it does not flash in a nicely random way.
    So... the seed must not be modified.
    But anyway this still does not explain why my morse code flasher doesn't work. Unfortunately I do not have the circuit at hand (it's in Paris now) so have to probably build a second one to really test.
    Last edited by eggman; - 21st May 2009 at 20:27.

  5. #5
    Join Date
    Dec 2005
    Location
    So Cal and loving it
    Posts
    40


    Did you find this post helpful? Yes | No

    Default

    What about using your adcin 'audio' value as the seed - it seems like it should give you some randomness.
    +-------------------------------------------------------------------+
    | PBP 2.47/2.50 | MCS+ 3.0.0.5 | U2 Prog | Vista x64 | NO SLEEP!!!!!!! |
    +-------------------------------------------------------------------+

  6. #6
    Join Date
    Oct 2007
    Location
    The Netherlands
    Posts
    45


    Did you find this post helpful? Yes | No

    Default

    Hmm that's a good idea, will try, thanks

Similar Threads

  1. Random Numbers
    By BobEdge in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 18th December 2008, 08:47
  2. USART Problem , but don't know where, in pc? or in PIC?
    By precision in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 15th July 2007, 08:12
  3. Another Serin/Serout Problem!
    By jmbanales21485 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 7th June 2007, 16:26
  4. 1 slave 1 master 1 MAX232 1 problem ?
    By SuB-ZeRo in forum mel PIC BASIC Pro
    Replies: 19
    Last Post: - 31st July 2005, 22:59
  5. 6 random numbers limiting to 49 ?????
    By SuB-ZeRo in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 15th June 2005, 23:27

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