Maker Pro
Maker Pro

Simon Says random generator for PIC 16F84

Oatamelian

Mar 9, 2013
21
Joined
Mar 9, 2013
Messages
21
Hello,

I'm looking to program Simon says into a PIC chip.

The chip we must use is a 16F84, I would happily program the exact same game into Simon and just sit there for hours writing hundreds and hundreds of lines of code, the frustration is driving me in this direction.

I would like to make it easier by having the program randomly generate the 4 colour pattern on its own and respond to the users input.

BUT

I have no idea how to get the chip to generate a random number from 1-4.

I want 4 ports on PORTA to turn the 4 different coloured LED's on and off.

I want 4 ports on PORTB to accept inputs from the user based on the four LED's

I want 2-3 ports on PORTB to link up to 2 4017's - a bargraph display to display the score. Where the first input would be for the clock, the second for the reset and the third for the enable (if I require it)

So there is 1 port left on PORTA and the interrupt (RB0) on PORTB.

What can I do to get this random generator? just to decide the next coloured LED to light up.

It can be anything, not necessarily a random generator but the closest thing that at least sort of appears random, to the user anyway.

Please, if anyone can help, I'm desperate.
 

CocaCola

Apr 7, 2012
3,635
Joined
Apr 7, 2012
Messages
3,635
One thing to note, the 'random' pattern will be the same with each reboot unless you use an external 'random' or 'changing' seed... Micros like the PIC will generate the same exact random pattern with each reboot unless you include an outside value that changes... Since you have user interaction you can generate this seed by timing the duration of a button press or the duration between button presses as an example... Save this ever changing seed in memory and use it to generate the random number between cycles and even between reboots...

Example

Save first button duration in memory m01
Save second button duration in memory m02
Save third button duration in memory m03

And roll the forth button duration back to m01 so that you always have a set of 3 user interaction values that are rolling...

Now do a quick and dirty formula to generate a seed, something like m01 + (5 * m02) + (3 * m03) = Your seed

Since these numbers are saved in the EEPROM even when you reboot you will have a more random seed to start with, not the same predictable pattern...
 
Last edited:

Oatamelian

Mar 9, 2013
21
Joined
Mar 9, 2013
Messages
21
Sorry, I forgot to add. I do not have a wide knowledge on the subject, so although I appreciate the help I only understand the commands and what they do as well as the way a couple of these circuits work.

Is there some code that the chip would recognise that I could add to my program and call everytime I needed to generate a random number (psuedo)?

OR

some hardware solution I could get the PIC to recognise a 3bit random binary number and choose an LED based on the result?
 

Oatamelian

Mar 9, 2013
21
Joined
Mar 9, 2013
Messages
21

I'm not completely Tech-Impaired, unlike the majority of individuals I can use Google but I've spent hours researching this and could not understand the 'jargon' people were talking about; I even tried using "Yenka PIC" but it was plagued with performance bugs and I found that halfway through my flowchart program the PC was spending almost 2 solid minutes processing the next box.

The only time I've ever heard the term "Seed" (in a semi-similar context) was when my friend showed me MC.

I've read through it and found an idea that could work, something involving a 3bit ADC and some sort of variable resistor effected by its surroundings, e.g. a thermistor or an LDR. Could this work? Any ideas how to develop the idea or is it a no go? Could I get the PIC to read a 3 bit number through a single pin?

Thanks for posting the link at least :D
 

BobK

Jan 5, 2010
7,682
Joined
Jan 5, 2010
Messages
7,682
You are making this way too complicated. Use a pseudo-random number generator with a long sequence, say 32-bits worth. This will, for all practical purposes never repeat. Store the last number generated in eeprom, and read it on boot up.

Bob
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,700
Joined
Nov 17, 2011
Messages
13,700
could not understand the 'jargon' people were talking about;
In that case you could have stated your question more precisely. This would have given us a chance to answer in a fitting way.

A pseudo-random number generator does produce a sequuence of numbers that seem random. Randomness is asserted by applying statistical tools to the sequence and looking for signs of non-randomness. Howeve, since the sequence is based on an algorithm, the same sequence will be produced over and again if the starting value for the algorithm is the same CocaCola's remark). You therefore change the starting value to obtain different sequences. This is new starting value is called the seed (because the sequence of numbers grows from that seed like a plant from a natural seed).
Any random value can serve as a seed, e.g. the curent time, the duration of a keystroke, the intervall between two keystrokes etc.
Or you can use a non-volatile memory (EEPROM) to store the state of the pseudo-random number generator's algorithm between reboots. In that case the stored state is used instead of a new seed.

Creating "good" pseudorandom number generators is not trivial. To many are the pitfalls. Use a well tested adn documented generator. The internet offfers plenty of choices. Have a look at this thread.
 
Last edited:

Oatamelian

Mar 9, 2013
21
Joined
Mar 9, 2013
Messages
21
In that case you could have stated your question more precisely. This would have given us a chance to answer in a fitting way.

I'm sorry for not making it more precise, but at the time of writing I was using an iPad to type it out and wanted it to be short cause it lags real bad. Quite literally I had to type it up using notes cause safari will freeze if I type too quick.
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
Here is a simple way. (with references to wikipedia and wikipedia).

First find a random number generator. Typically these are of the form R(n) = R(n-1)*A mod B, (see the first wikipedia reference) but you can do it much faster and almost as well with a combination of XORing a pair of bits at one end of a word and rolling the result into the other end (this is called a Linear feedback shift register, see the second wikipedia reference) .

In either case, you have something that generates a deterministic sequence of numbers that follows no easily discernible pattern.

Then, let's assume you have delay loops in your code. Maybe you loop while waiting for a button to be pressed, or loop waiting for a button to be released.

In each of those loops insert a call to generate the next random number (but don't do anything with it).

This will ensure that the sequence of number you do get (and use) are more random because the sequence is determined by the time the user takes to perform actions. This is essentially a random element.

edit: these random number generators (actually pseudo-random) return big numbers (say between 0 and 2^32). If you only want a number between 1 and 10, use the mod operator to restrict the range.
 
Last edited:
Top