PDA

View Full Version : Drag racing lights



Radar
02-18-2005, 06:14 AM
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 :)

Jim McCorison
02-18-2005, 06:30 AM
Have you tried using the RANDOM verb of PBASIC? Check the help documentation. Pay special attention to the issue of a seed value. If you use the same seed value, the RANDOM verb will always give you the same number. There is sample code to get around the issue by having a user press a button to generate a more randomized value.

Jim

Paul Baker
02-18-2005, 07:11 AM
an alternate method of random seed generation is to get a string of random bytes from www.random.org (http://www.random.org) and storing it in a table. And storing an index in eeprom, the simplest method is incrementing the counter each time and using that value as the seed the seed will repeat when you use all the values.

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).

Radar
02-18-2005, 07:25 AM
I have set up a test of the counter ( I hope this helps to see what I'm trying)

·' {$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
D:
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

JKtech
02-18-2005, 08:46 AM
Here is a thought. You can use yourself to get a random number. You can also keep you counter counting between 1 and 5 over and over while you have your button pressed.

Radar
02-18-2005, 09:34 AM
Thanks for the info, it has got me thinking.· I like the idea about having the counter count from 1 to 5 while I hold the button down, but I'm drawing blanks on how to do this.· Do you think you could give me a "test" program to try?· I have looked at the count and button commands in the manual and don't understand how they work, but with the 1 to 5 count idea it gives me an idea that if I can have it count (say based off of an internal clock, if this can be done) then multiply that number by 1000 this will give me a number form 1000 to 5000 or 1 to 5 sec (great) and I can work off of that, and because the number is based off of a counter (if I can base it off of an internal clock) then there is no way I can "cheet" or have an effect on the time before the green light comes on, because the speed of the clock has to be many many "ticks" per sec.· There for making it impossiable to know the output.

Thanks again
Radar

Jon Williams
02-18-2005, 10:49 AM
I frequenty "stir" the RANDOM generator while wait for a user input. You can do something as simple as this:



DO WHILE (StartButton = Up)
RANDOM randVal
LOOP

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas, TX· USA

Radar
02-19-2005, 01:08 AM
Thanks for the info Jon, I can now get a very random number, but there is a problem if I give the var a byte name it is a number 0 to 255 and if I give it a word name I get a number 0 to 65535.· With a simple (x-1) code this gives me 0 to 2 seconds or 0 to 6 seconds, this is not the numbers I'm looking for. I have tryed to add math to these to see if I can change the number but have had no luck, with byte I still can not get over 255 (thats the limit) and with word I seem to get very weird outputs of time (anything form 0 to about 12 seconds).··This is what I have·been using·as a "test code"·so let me know if I'm doing something wrong or if there is a better way to do it

'{$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
D:
OUT5 = 0·· (TURNS OFF THE TIMER)
END

I have tried (after B:) 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

Paul Baker
02-19-2005, 01:24 AM
adding a delay at the beginning will make a minimum delay
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

Paul Baker
02-19-2005, 01:35 AM
Ok, shoulda read your complete post, you can subdivide the delay·you insert to check inputs (delay .1 seconds, check input, delay .1 seconds,...) you can do this by creating an inner loop, creates more code but it allows your stamp to periodically check input.

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
02-19-2005, 03:43 AM
Have a look at the attached program.· It shakes the random number generator until a button is pressed, then takes the seed and creates a 1 to 5 value.· Pretty simple, and you should be able to incorporate the code into your project.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas, TX· USA

Radar
02-19-2005, 10:57 AM
Once again thanks again everyone, jon the code worked great!!· My lights are up and running thanks to all of your info and help.



BIG Thanks again,

Radar