CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

PIC16F819 Memory game

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Bluefire



Joined: 20 Feb 2019
Posts: 1
Location: London

View user's profile Send private message

PIC16F819 Memory game
PostPosted: Tue Feb 26, 2019 7:47 am     Reply with quote

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: 9096
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Feb 26, 2019 8:23 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Tue Feb 26, 2019 1:36 pm     Reply with quote

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: 19215

View user's profile Send private message

PostPosted: Sat Mar 30, 2024 5:00 am     Reply with quote

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: 9096
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Mar 30, 2024 10:00 am     Reply with quote

hmm, feed AC power->diode-> MAX1489 into a counter pin, then read the 'count' register
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Sat Mar 30, 2024 10:58 am     Reply with quote

Problem with that is that if you are reading the same time after starting,
It'll always get the same count.... Sad
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: 9096
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Mar 30, 2024 2:24 pm     Reply with quote

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: 19215

View user's profile Send private message

PostPosted: Sun Mar 31, 2024 4:38 am     Reply with quote

I like the Geiger counter. Smile
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

View user's profile Send private message

PostPosted: Wed Apr 03, 2024 12:31 pm     Reply with quote

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: 9096
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Apr 03, 2024 12:59 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Apr 03, 2024 1:17 pm     Reply with quote

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: 9096
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Apr 03, 2024 4:31 pm     Reply with quote

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'.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group