Shop OBEX P1 Docs P2 Docs Learn Events
Clay Pigeon Flush Controller, help required. — Parallax Forums

Clay Pigeon Flush Controller, help required.

paulmorelandpaulmoreland Posts: 4
edited 2021-01-20 13:36 in BASIC Stamp
I'm looking for some assistance with the code below as I've gone code blind.

The basic premise is i want to generate and random number between 1-5 to represent how many times a relay will be fired in a "cycle". Then based upon this how many I need to generate another random number between 0-4 (though it never seems to generate a 4?) to represent which relay will fire for each of iterations. So if this time how many was 3 I would need 3 numbers between 0-4. The problem I have is that these need to be unique.

Each trap relay will send 2 birds and the whole program is supposed to deliver 250 birds hence the outer loop and the line birdsThisTime=numTraps*2, this seems to work OK.

The main bit I'm missing is the whichTrap duplication, I write the values into an array as I wanted the logic to 1. check if whichTrap is already in the array trapNos and if it is generate another and if it's not add to the array.

Is there a smarter way to avoid duplication as that's the only reason I have the array as I believe that was the easiest way managing it.
' {$STAMP BS2sx}
' {$PBASIC 2.5}
i VAR Nib
x VAR Nib
numTraps VAR Nib
whichTrap VAR Nib
numBirdsTOT VAR Byte
trapNos VAR Byte(10)' Create a 10-byte array
result VAR Word
resultTrap VAR Word
birdsThisTime VAR Nib
numBirdsTOT = 0
LOW 15
i=0
DO WHILE (numBirdsTOT < 250) ' sets up outer loop to count 250 birds
  'first get how many of the 5 traps to fire
  numTraps=(result//5+1)
  RANDOM result' generate random number
  DEBUG "**********************************"
  DEBUG CR
  DEBUG DEC ? numTraps
  DEBUG CR
  whichTrap=0
  i=0
  FOR i=0 TO numTraps-1
    whichTrap=(result//4) ' issue with not generating a 4
    RANDOM result' generate random number
    DEBUG DEC ? whichTrap
    DEBUG CR
    'before writing to the array need to check if whichTrap already there, this is what's missing
    trapNos(i) = whichTrap
  NEXT
  FOR i=0 TO numTraps-1
    DEBUG "trapNos("
    DEBUG DEC i
    DEBUG ")="
    DEBUG DEC trapNos(i)
    DEBUG CR
    HIGH i
    PAUSE 200
    LOW i
  NEXT
  'set birdsThisTime
  birdsThisTime=numTraps*2 ' each relay will fire 2 traps
  'need to check if birdsthis time would exceed max nuBirdsTot of 250
  IF birdsThisTime + numBirdsTOT > 250 THEN
    numBirdsTOT = numBirdsTOT - birdsThisTime
  ELSE
    numBirdsTOT = numBirdsTOT + birdsThisTime
  ENDIF
  birdsThisTime=0
  numTraps=0
LOOP
HIGH 15
DEBUG CR
DEBUG "End of sequence"
numBirdsTOT=0

Any help appreciated.

Comments

  • One of the problems you'll face with a microcontroller like the BASIC Stamp is that random isn't truly random; it's psuedo-random, that is, an algorithm is used to create what *appears* random. But, it is an algorithm, so given the same seed, you'll get the same result.

    One way to improve this is to add some element of actual randomness. For example, a human having to press a button. While the program is waiting for the human it its manipulating the seed. As the human will react at a random time, the seed will be randomized.
      whichTrap = (result // 4)
    
    This code will return a value between 0 and 3. Why? The // (modulus) operator divides by four and returns the remainder -- if you're dividing by 4 the remainder can only be 0 to 3. Change the 4 to a 5 and you'll get 0..4. Like you do in the earlier statement where you add one and bump the result to 1..5.

    When posting listings you really should use the Code tags. Press the C on the toolbar above the edit field and then paste your code in between the tags. This will retain the formatting.

  • paulmorelandpaulmoreland Posts: 4
    edited 2021-01-20 16:08
    Hi Jon, thanks for taking the time to respond and for the modulus & code tag advice.
  • paulmorelandpaulmoreland Posts: 4
    edited 2021-01-20 16:35
    trying to delete this discussion as now obsolete in favor of new topic.
This discussion has been closed.