Drag racing lights
Radar
Posts: 5
Hi, I'm new to the BS2 but have a good understanding of it.· I am having a problem with comming up with a ramdom number in my program.· Here is what I'm trying to get,· I'm making a 2 lane timing system for my self (for dragracing) the problem with the program is that when the two drivers stage (come to the line and are ready to race) I have·2nd switch ( in my hand) that I ush and it starts the racing program on the BS2 this works great, but as you know its all about reaction time and it the time between me puching the button and the green light is always the same the drivers have an easy time "guess" the green light.· I have tryed to hide the button but get accused of favoring a driver and telling him how long I will wait before pushing the button, so what I need is a way to get a random (everytime a diffurent number) to put into a "count down" loop in the program before the green light that will give me a way to "pause" the program for an unknown amount of time.· I guess the number is not that important as to is size but I'm looking for a 1 to 5 sec delay·before going on.· I have a simply counter in mind...
x = random number
a:
x = (x-1)
if x = 0 then b
goto a
b:
· ( and onto the program)
I have the basic stamp manual ver 2.0 and have tried the truely random number way they talk about in there, but I only have 5 pins left over for a random INH (what ever this is) and seem to get about the same delay everytime.
Thanks for info and Time [noparse]:)[/noparse]
x = random number
a:
x = (x-1)
if x = 0 then b
goto a
b:
· ( and onto the program)
I have the basic stamp manual ver 2.0 and have tried the truely random number way they talk about in there, but I only have 5 pins left over for a random INH (what ever this is) and seem to get about the same delay everytime.
Thanks for info and Time [noparse]:)[/noparse]
Comments
Jim
The method of making the most out of your stored data is to treat each bit as a seperate entity, get your seed value via the index where each bit has its own·index (which may span more than one byte) then increment the index by a mid-size prime number (no less than 13). This will span your seed table many many times before cycling. This is somewhat complex, but you'll only do it once at startup so the overall cost isn't that much.
Yet another is using a hash algorithm and simply feeding the algorithm with the index, though successive·input only differs by 1, the output will change dramatically, the rate of repeating depends on how large of an index you use (8 bit, 16 bit etc).
·' {$STAMP BS2}
INPUT 0
INPUT 1
INPUT 2
INPUT 3
INPUT 4
OUTPUT 5
OUTPUT 6
OUTPUT 7
OUTPUT 8
OUTPUT 9
OUTPUT 10
X VAR BYTE
X = INH * 256 + INH
RANDOM X
A:
IF IN0 = 1 THEN B
GOTO A
B:
OUT5 = 1
C:
X = (X-1)
IF X = 0 THEN D
GOTO C
OUT5 = 0
END
This is just a test to see what I get for a random "pause".· I have tried changing the x = to ;· X = (IN15 * 20) + (IN14 * 20) ,· but with no luck it still seams to be the same time delay its like I can't seem to get a random number as an input for X.· Isn't there a way I can just get the stamp to pick a random number from 1 to 250?· If·not is there something wrong with my "test" program?·
P.S. all the input and output's are there because thats the ones I use in the program, so the others are open for use! that means if I can use a input as a number I can, any ideas???
Thanks again,
Radar
Thanks again
Radar
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas, TX· USA
'{$stamp bs2}
(same inputs and outputs as prev.)
X VAR BYTE·· (HAVE TRIED WORD HERE TOO.)
············· (this is where I defined the other var "word")
A:
IF INO = 1 THEN B
DO WHILE
·RANDOM X
············ ( this is where I made the byte var = the word var)
LOOP
GOTO A
B:
OUT5 = 1· (TURNS ON A TIMMER SO i CAN SEE WHAT THE DELAY IS)
C:
········ (this is where I would add my multiplyer to get a bigger number, the counter would have to be changed to count down the "word" var)
X = (X - 1)
IF X = O THEN D
GOTO C
OUT5 = 0·· (TURNS OFF THE TIMER)
END
I have tried (after B[noparse]:)[/noparse] putting in a *20 ( 255* 20=5100 or a number 0 to 5100) but seem to be getting a weird output of under .010 seconds, I think this is because of the byte code thing (%00000000· 8 "places" to a byte) that the number is bigger then that but the var does not see this, I have tried adding a nother var "word" ( noted in the code above) but still get "weird" outputs of very low or very high times.· I'm not trying to be picky here but I have a problem with not having a random time of 1 to 5 seconds (rember there are 2 sleds running at the lights at this point and are both waiting for the yellow ot come on fallowed by the green) so it is kinda critical that I get a more "reliable" time output.· Also when this is in the (drag race program)·I have (while it is counting down the time) to watch the input of 2 pins (red lights) to make sure the drivers don't "jump the gun" so that it will red light them if they do, with this I can NOT put a simple pause in the program because then the chip will just wait there and not "watch" the other two inputs.· I know I might be getting picky here but that is the reason I bought the BS2, so I could program it.
Thanks again
Radar
by adding a delay between each tick (x = x -1) you can scale the variable portion of the delay to a suitable value.
You don't need to alter the index itself.
in pseudo code
X = RANDOM
DELAY 1 SECOND
(LOOP)
DELAY 0.25 SECONDS
X = X - 1
IF X > 0 GOTO LOOP
CODE AFTER DELAY LOOP
this code will produce a delay between 1.25 and ((.25 x 256) +1) =65 seconds
set your own delay values to get your desired minimum and range
Modified psuedo code
X = RANDOM
CHECK START FAULT
DELAY·.25 SECONDS
CHECK START FAULT
DELAY·.25 SECONDS
CHECK START FAULT
DELAY·.25 SECONDS
CHECK START FAULT
DELAY·.25 SECONDS
CHECK START FAULT
(LOOP)
DELAY 0.005 SECONDS
CHECK START FAULT
DELAY·0.005 SECONDS
CHECK START FAULT
DELAY·0.005 SECONDS
CHECK START FAULT
X = X - 1
IF X > 0 GOTO LOOP
CODE AFTER DELAY LOOP
This should produce a delay for 1.015 seconds to 4.84 seconds
Post Edited (Paul Baker) : 2/18/2005 5:43:34 PM GMT
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas, TX· USA
BIG Thanks again,
Radar