Announcement

Collapse
No announcement yet.

Best solution for creating random value? (P2K, randomly flickering some LED's)

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts


  • Best solution for creating random value? (P2K, randomly flickering some LED's)

    I build all of my escape games on the P2K platform and normally to flicker a bunch of LED's I would just use a off-board controller (BASIC Stamp 1 with an onboard ULN2803) to do some pseudo-random flickers.

    On this particular build, I only have 3 channels worth of LED's that I need to flicker, so it seems fairly silly to use a off-board controller for this.

    If you were to randomly flicker 3 outputs, how would you do it? I've done some playing using a 500ms and 1 second flasher, having one output on the normally on of the 500ms, the second on the normally off of the 500ms (so, inverse of the first) and the 3rd on the 1 second flasher. This works, but by no means is it random. I thought about trying to do some math to have it generate a random number (say, 1-6), then using compare to activate the outputs, but there is no random seed in P2K that I can find.

    How would you go about it?


  • #2
    Way back in the Dinosaur days, I needed a Random Flash of a Set of 16 Lites on an Apple ][ Computer.. The Integer Basic version not having a Random Feature, I chose to Read PROM Monitor Program Code ( The Operating System for the Apple ][ ) and one word at a time, read the program Word, and Display it to the 16 Bits of Output.. then Step to the Next Program Location and Display that.. Granted it's not a Random Feature, but it is a large bunch of Bits that appear to be in no particular order..

    At the end of the ROM memory, I just started over again..

    Can you do something like that in P2K

    Cap

    Comment



    • #3
      Create randomish numbers in excel column (say 50), then copy those values into a 1D array in P2K. Use a 1sec pulse to march through the array values.


      Excel rand() function gives random number between 0-1. Column A = RAND(). Columb 8 = A*10









      I checked and P2k does not have a MOD function in the MATH box. If it did, you could calculate a sudo random number.

      Comment



      • #4
        Have some fun learning some new logic. Google 'Linear Feedback Shift Registers'. Experiment!
        thePLCguy

        Bernie

        Comment



        • #5
          RTC Milliseconds of the clock multiplied by scan time of the PLC... ???

          Comment



          • #6
            Originally posted by g.mccormick View Post



            I checked and P2k does not have a MOD function in the MATH box. If it did, you could calculate a sudo random number.
            MOD, as in Modulo? If so, P2K has a Modulo function. It's the "%" button in the MATH instruction.

            Comment



            • #7
              Originally posted by bcarlton View Post
              Have some fun learning some new logic. Google 'Linear Feedback Shift Registers'. Experiment!
              I'm not sure how easy it would be to translate to P2000, but I wrote one for a CLCIK a while ago one might take a look at: https://forum.automationdirect.com/forum/general-applications/11324-analog-output-to-cmore-speed?p=82218#post82218

              Comment



              • #8
                This is my interpretation of a 16-bit galois lsfr. If you find errors, please complain.
                ( there is some issue, the value after 0xACE1 is supposed to be 0xE270, I see 0x5670) -corrected
                This is taken from the wiki page.
                To change the sequence, change the startstate value from 0xace1 to something else.

                In dataview write _MASK_ and startstate values to be 0xB400 and 0xACE1, respectively

                XXXXXX --- Well, I thought that I would copy the generated number sequence from the dataview and paste into excel to check for duplicates. Cannot copy data from dataview anymore.
                I used to be able to copy array values and paste into excel. Well, AD, thanks for pruning that value from the software!

                EDIT: In task G_LSFR, move rung 4 to rung 6 and change startstate tag to 0xACE1 -- I had 0xACE5
                The rung to move is the rung that copies the current iterated lsfr value to the array _LSFR_

                I do not know how to employ this LSFR generator to produce a single digit randomized number,
                but you can think of the startstate tag as a seed and _MASK_ as the feedback polynomial (?)
                Attached Files
                Last edited by kewakl; 10-05-2017, 08:19 PM.

                Comment



                • #9
                  Originally posted by Brandon_ View Post

                  MOD, as in Modulo? If so, P2K has a Modulo function. It's the "%" button in the MATH instruction.
                  Ah I did not notice that. Then perhaps
                  X_{n+1} = (a X_n + b)\, \textrm{mod}\, m

                  https://en.wikipedia.org/wiki/Random_number_generation

                  Comment



                  • #10
                    Originally posted by Tinker View Post

                    I'm not sure how easy it would be to translate to P2000, but I wrote one for a CLCIK a while ago one might take a look at: https://forum.automationdirect.com/forum/general-applications/11324-analog-output-to-cmore-speed?p=82218#post82218
                    This worked perfectly on a Click. I'm definitely going to keep this in my back pocket. I don't fully understand how it works, but I'm having fun deconstructing it to understand it!

                    Comment



                    • #11
                      kewakl and mccormick, thanks for the info and the logic to get me started. I don't have a P2K here to run code on (I really wish it had a emulator! This is starting to push me to the Do-More world!), but I'll be on site with a unit Wednesday through Friday that I can run it on. Thanks again.

                      Comment



                      • #12
                        Originally posted by Brandon_ View Post
                        kewakl and mccormick, thanks for the info and the logic to get me started. I don't have a P2K here to run code on (I really wish it had a emulator! This is starting to push me to the Do-More world!), but I'll be on site with a unit Wednesday through Friday that I can run it on. Thanks again.
                        Do-more has a random number generator, so this wouldn't have been an issue.

                        Comment



                        • #13
                          Originally posted by BobO View Post

                          Do-more has a random number generator, so this wouldn't have been an issue.
                          Oh yes, I know. Lord do I know. You're always quick to point out DoMore does <insert everything here> :-D

                          One of these days when I sit down and try to get past the interface of DMD, maybe I'll switch. I just wish the P2K guys would catch up on a instruction set like Do-More has. And PWM.

                          Comment



                          • #14
                            Originally posted by Brandon_ View Post

                            Oh yes, I know. Lord do I know. You're always quick to point out DoMore does <insert everything here> :-D
                            Sorry. Don't mean to come off that way, just trying to educate.

                            Comment



                            • #15
                              Originally posted by BobO View Post

                              Sorry. Don't mean to come off that way, just trying to educate.
                              Oh no, it didn't come off as condescending or anything like that. DoMore really looks like a "can do everything" platform and you have every right to push it. It just doesn't fit my needs from a hardware feature and cost perspective. Admittedly, I haven't looked at BRX options lately, but at the release you didn't have what I needed. Past that I'm having a really difficult learning curve with DMD, I'm just not a fan of the interface.

                              Comment

                              Working...
                              X