PDA

View Full Version : random problem



eggman
- 21st May 2009, 18:30
Hello everyone,
I have a strange problem, I wrote a little program to control a VMUSIC2 MP3 player. It plays an MP3 with morse beeps that will then switch a lamp. The user can change the MP3 file without having to mess with the program.
After playing the file the player should wait between 0 and 45 minutes randomly before playing the morse code again.
All works okay but after a while the randomness disappears and it will always wait 45 minutes. Seems something wrong with the random command, maybe the seed? any help is appreciated. Here's the code:


' 12-05-09 morse code player, reads mp3 file to control lamp

@ device pic12F683, INTRC_OSC_NOCLKOUT, wdt_off, pwrt_on, mclr_off, protect_off

include "modedefs.bas"
define OSC 4

OSCCON = %01100000 ' Set to 4MHz
CMCON0 = %00000111 ' Analog comparators off
ANSEL = %00000010 ' Set AN1 as analog, rest digital
TRISIO = %00100010
GPIO = %00010000 ' GPIO.4 hoog voor serieel, start anders niet

relay var GPIO.0
led var GPIO.2

counter var word
temp var word
time var word
audio var byte

pause 2000

loop:
SerOut GPIO.4,T2400,["V3A",13] ' play all songs
play_morse:
toggle led
adcin 1,audio
if audio > 25 then ' test sound (more than 500 mV)
high relay
else
low relay
endif
SERIN2 GPIO.5,396,20,play_morse,[wait ("Stopped")]
random temp
time = temp/24 ' between 0 and 45 seconds
for counter = 1 to time
pause 1000
toggle led
next counter
goto loop

End

tenaja
- 21st May 2009, 18:42
If you read the section on Random, you will see that your output variable is also used as the seed. Try to use Random Time instead of Random Temp. That way your seed is changed after each use. In your code Temp is unnecessary anyway.

eggman
- 21st May 2009, 19:17
I'm not so sure about that, if I use time as my seed it will always be smaller then 2730, (time / 24) Will this not affect the output of the random generator?
i.e. does the seed need to be a 16 bit number or not?

eggman
- 21st May 2009, 20:22
Okay, I did a little test:
If I try this code:



loop:
random temp
toggle LED
pause temp >> 8
goto loop


My LED flashes in a very nicely random way.

If I try this code:



loop:
random temp
temp = temp >> 8
toggle LED
pause temp
goto loop


Then it does not flash in a nicely random way.
So... the seed must not be modified.
But anyway this still does not explain why my morse code flasher doesn't work. Unfortunately I do not have the circuit at hand (it's in Paris now) so have to probably build a second one to really test.

locko
- 21st May 2009, 23:05
What about using your adcin 'audio' value as the seed - it seems like it should give you some randomness.

eggman
- 22nd May 2009, 09:10
Hmm that's a good idea, will try, thanks