Shop OBEX P1 Docs P2 Docs Learn Events
Need Some Help With Real random Class.. and random timer. — Parallax Forums

Need Some Help With Real random Class.. and random timer.

AnubisbotAnubisbot Posts: 112
edited 2009-05-11 17:15 in Propeller 1
Hi i need to trigger 4 Pub's at different random times.
I tried the random object form the OBEX
but dint had to much luck with it. i need to start the Pub's randomly so the time span i need is from 30 sec. to 300 sec.

PRI Main_Loop   
dira[noparse][[/noparse]11..0]~~
RR.Start

Time:= RR.Random    
 

Time2:= RR.Random


Time3:= RR.Random  

 
Time4:= RR.Random  
  

repeat
      If Time == 0
        Small_1_Left
        repeat until time > 5000000
         Time:= RR.Random
      If Time2 == 0
        Small_2_Left
        Time2:= RR.Random  
      If Time3 == 0
        Small_3_Left
        Time3:= RR.Random    
 Time--
 Time2--
 Time3--
 Time4--




I hope some one has a good idea how to solve this, maybe i am blind
but i searched for it and count find any thing

Best regards Anubisbot

Comments

  • LeonLeon Posts: 7,620
    edited 2009-05-11 14:36
    You'd be better off with a pseudo-RNG.

    Leon

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Amateur radio callsign: G1HSM
    Suzuki SV1000S motorcycle
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-05-11 17:15
    I suppose you're decrementing the time variables inside of the loop?!

    The random object generates a number in a range from 0 to ~4billion. As you do a lot of SPIN stuff inside of your loop this can take a while and I don't see any code that ensures the time being between 30 and 300s.

    Time1:=30 + RR.Random & $FF···· ' would be a fast solution where the range is between 30 and 285 if that's fine for you
    Time2:=RR.Random
    Time2:=30 + Time2 & $FF + (Time2 & $F00) >> 8··· ' is randomly adding another 15, so the range is between 30 and 300, but it's a bit slower ... but hey, we talk about seconds ;o)

    Now you sould use waitcnt to wait for seconds:
    time:=0
    CntToWaitFor:=cnt 
    repeat
      CntToWaitFor+=clkfreq
      waitcnt( CntToWaitFor )
      time++
      case time
        Time1:
           small_1
        Time2:
           small_2
        Time3:
           small_3
        Time4:
           small_4
    
    

    ·As I used the case statement you should make sure that not two times have the same value.
Sign In or Register to comment.