PDA

View Full Version : quick question



peterdeco1
- 8th November 2016, 11:13
I have 4 identical circuits. If I put at the top of each program:

X var word
pause x

and power up all 4 at the same time, will X be a random number on each of them? The idea here is to make all 4 flash an LED but I don't want them to do it at the same time. Will this work or should I generate a random number for X in a more conventional way? Thanks

Dave
- 8th November 2016, 14:09
Peter, I would personally start one of the 16 bit timers (if it has 1) and then read it and use it as the SEED for the random command whoes output would be scalled and used as the PAUSE variable.

HenrikOlsson
- 8th November 2016, 15:15
x will most likely be 0 or 65535 when you apply power. Getting a truly random number is actually quite hard. Perhaps a long wire or PCB trace acting as an antenna feeding an analog input and sample it on power on.

/Henrik.

Art
- 12th November 2016, 14:14
One way to get a “sort of random number” is to start a timer, and then let the watchdog timer do a reset which will not ruin the timer values.
Then you can read them and you get different values each time in some limited range that you could multiply or divide.
The next time the program starts after the reset, the timer values will be non zero before you started the timer. Then service the watchdog timer from then on.

I left an example here at some stage that allowed a pic to determine from a list of specific values, what speed crystal is running the pic.

Another way might be to let the POT command do what it does, and time the discharge of a capacitor through a resistor.
You should be able to fiddle with resistor and capacitor values to make the POT command return inconsistent readings.

Demon
- 13th November 2016, 19:02
Can you add a pot?

I'd connect it to an analog input, read the value, establish a pause or loop on that value then start main loop.

Robert