View previous topic :: View next topic |
Author |
Message |
Bluefire
Joined: 20 Feb 2019 Posts: 1 Location: London
|
PIC16F819 Memory game |
Posted: Tue Feb 26, 2019 7:47 am |
|
|
Hi, I'm new to this forum and have recently started using MPLAB (ccs complier) to program a PIC16F819. I'm trying to create a memory game that would consist of 4 LEDs lighting up in random sequences. The user would then need to press 4 switches that correspond to the LEDs in the same sequence that the LED light up in.
Can someone please help me with starting the code?
Thank you |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Tue Feb 26, 2019 8:23 am |
|
|
1st program....
code for a 1Hz LED program.
Simply have the PIC turn on/off an LED at a 1Hz rate.
This will confirm you can code/compile/burn PIC and it works.
2nd program..
add 3 more LEDs (and resistors) and have flash as well.
3rd program..
I'd grab the 'button.c' code from the library and use for the 'push button' functions. Just use the 'search' option top, center of this webpage to find it.
4th program
code for ONE LED and ONE PB. test....
5th program
code for 4 LEDs, 4 buttons, test
6th program
add 'random' using the CCS supplied RND() function
... |
|
|
dluu13
Joined: 28 Sep 2018 Posts: 395 Location: Toronto, ON
|
|
Posted: Tue Feb 26, 2019 1:36 pm |
|
|
I think that you should try and look up state machines and their implementation. That might help you out in the structure of your program. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Sat Mar 30, 2024 5:00 am |
|
|
One thing that has not been mentioned, is the problem of making
this 'random'.
Unfortunately, making something that does not wake up repeating the
same sequence is very hard. The 'rnd' function returns a 'psuedo
random' number sequence that will repeat every time it is used. To
make it start with a 'random' value, you need something to seed this
that is unpredicatable. I'd suggest you have the user push a button
and release it to start the program. Have this timed by the code, and
use the number from this set the initial seed to the rnd function.
Result something that is impossible to predict. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Sat Mar 30, 2024 10:00 am |
|
|
hmm, feed AC power->diode-> MAX1489 into a counter pin, then read the 'count' register |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Sat Mar 30, 2024 10:58 am |
|
|
Problem with that is that if you are reading the same time after starting,
It'll always get the same count....
You need something to make the seed 'unpredictable'. Since he will have
buttons for a game, if you time a press to millionths of a second, you will
get something very unpredictable to seed the random number generator.
Something like counting hum off a input pin, can also work, if this comes
as a result of just how people are moving round the unit. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Sat Mar 30, 2024 2:24 pm |
|
|
Use a small PIC as a white noise generator ?
Geiger counter as the pulse source ?
Count when I have a coffee ? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Sun Mar 31, 2024 4:38 am |
|
|
I like the Geiger counter.
Yes, a basic radio receiver, or noise off a diode are ideal ways to seed
the generator.
The downside to all of these is they need extra hardware. The point about
the key approach, was assuming the system does have some keys, it can
be very easily done with no extra hardware. If you time a key press to uSec
resolution, you will get an unpredictable value to seed the rnd function.
So you just have it display something like 'press key to start game', and
record the time from this and use this to seed the generator to make the
game a good approximation to random. |
|
|
pmuldoon
Joined: 26 Sep 2003 Posts: 218 Location: Northern Indiana
|
|
Posted: Wed Apr 03, 2024 12:31 pm |
|
|
It's funny that it seems so hard to generate a random number, yet a large amount of posts on this site are about random events occurring in a seemingly deterministic design.
Kinda reminds me of the question, How do you make an oscillator? By designing an amplifier, of course!
Just thought I'd throw that in. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Wed Apr 03, 2024 12:59 pm |
|
|
BTDT, many, many times......
hmm, OP has 4 switches, so SIMPLE to create a random number. In a 'create random number function', get the times it takes to press all 4 buttons. Now do some 'math' on those 4 numbers to create the 'seed' for the random function. |
|
|
pmuldoon
Joined: 26 Sep 2003 Posts: 218 Location: Northern Indiana
|
|
Posted: Wed Apr 03, 2024 1:17 pm |
|
|
Well, in the spirit of overthinking this, what about using one of the adc inputs to drive one of the leds? won't an led create a voltage when it's exposed to light? This could be read at startup, maybe multiple times before using the LED as an output. Too bad this adc doesn't have a temperature diode option. That could be another variable. How about the raw supply voltage? Does it run on batteries or line xformer? Either will vary a bit. (no pun intended).
Using the buttons is just to easy. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Wed Apr 03, 2024 4:31 pm |
|
|
yes, LEDS can work 'in reverse' ! Used 3(R,G,B ) to make a 'color detector' years ago( think 16C84 era )....
One of those lunchtime napkin ideas,to relieve the boredom of wirewrapping a 4by6 perfboard filled with 28 and 40 pin DIPs....
The challenge was in calibration but once done, it was really good for being so 'simple'. |
|
|
stollingsp
Joined: 01 Jul 2024 Posts: 1
|
|
Posted: Mon Jul 01, 2024 4:55 am |
|
|
temtronic wrote: | yes, LEDS can work 'in reverse' ! Used 3(R,G,B ) to make a 'color detector' years ago( think 16C84 era )....
One of those lunchtime napkin ideas,to relieve the boredom of wirewrapping a 4by6 perfboard filled with 28 and 40 pin DIPs....
The challenge was in calibration but once done, it was really good for being so 'simple'.
Jackpot skripti harika bir konu! Bu skripti kullanarak oyun deneyimini çok daha heyecanlı hale getirebilirsiniz. Plinko-X'de bulunan bu harika kaynağı keşfetmek için https://plinko-x.com/ adresini ziyaret etmenizi öneririm. Jackpot skripti sayesinde büyük ödüller kazanma şansınız artacak ve oyununuzu daha da zevkli hale getireceksiniz. Oyun tutkunları için harika bir fırsat!
| Thanks for the information! |
|
|
sagyujg
Joined: 17 Sep 2024 Posts: 1
|
|
Posted: Thu Sep 26, 2024 3:40 am |
|
|
temtronic wrote: |
PIC16F819 ile ilgili hafıza oyunu hakkında güzel bir tartışma dönüyor! Bu tarz projeler her zaman ilgimi çekmiştir, çünkü elektronik ve programlama bilgimizi geliştirmek için harika fırsatlar sunuyor. Özellikle yeni başlayanlar için PIC mikrodenetleyici projeleri çok eğitici olabilir. Bu arada, serbest zamanlarımda bir şeyler denemek istiyorsanız, bu https://mb-tr.com/tr/line/football tavsiye ederim. Strateji ve şans gerektiren oyunları seviyorsanız, keyif alabilirsiniz. Tabii ki, her şeyde olduğu gibi ölçülü olmak önemli. Bu forumdaki tartışmaların devamını sabırsızlıkla bekliyorum! İyi çalışmalar ve iyi oyunlar dilerim!
. | Bilgi için teşekkürler! |
|
|
|