PDA

View Full Version : A little Quiz game (Two players + audience) with PIC12F675



ivanrosales
- 31st August 2011, 15:43
This is a little Quiz game requested by my little son to use in his classroom, there are 4 pushbuttons, one for each player ("Red Player" and "Blue Player") and the other two ("Correct" and "Incorrect" answer) are for the teacher. You can use a RGB LED (Red player, Blue player an Green audience) or three separate LEDs.

The interesting part of this little and silly project is the use of an analog input to check the status of two pushbuttons.

The schematic is included in the source code you can add a 9v bat. with a 7805 regulator and an on-off switch.

Enjoy.


'************************************************* ***************
'* Name : MaraTron+12F675 *
'* Author : Ivan Rosales, Guadalajara México *
'* Notice : Copyright (c) 2011 Ivan Rosales *
'* : All Rights Reserved *
'* Date : 27/08/2011 *
'* Version : 1.0 *
'* Notes : Quiz Game (Two Players + Audience) *
'* : *
'*
'*
'* Vdd
'* +
'* |
'* |
'* .-.
'* | | PIC12F675
'* | |1K ___ Red
'* '-' -o|° |o- LED Green
'* | .--------o| |o---->|-. LED Blue
'* .----o----o-------)--------o| |o-------)--->|-. LED
'* | | | .-o|___|o-------)------)--->|-.
'* | o | o | | | | |
'* |=|> |=|> | | | | |
'* | o | o Red | Blue| | | |
'* Correct |Incorrect| | | | | |
'* | | | o | o | | |
'* | | |=|> |=|> | | |
'* | | | o | o | | |
'* | .-. | | | | |
'* | | | | | | | |
'* | | |1K | | | | |
'* | '-' | | | | |
'* | | | | | | |
'* | | | | | | |
'* '----o----o-------o------o---------------o------o------'
'* |
'* ===
'* GND
'************************************************* ***************
@ Device PIC12F675,WDT_ON,PWRT_ON,PROTECT_OFF,MCLR_OFF,BOD_ OFF
ansel=%00111000 'Use GP4/AN3 (Pin 3) as analog input
adcon0 = %00001101
cmcon=7 'Don't use analog comparator
trisio=%111000 '
option_reg=0 'Enable Pull-Ups
wpu=$FF 'Config Pull-Ups (GP3 DOESN'T have Pull-Up)

DEFINE ADC_BITS 10
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 50

'Definitions
Rojo VAR GPIO.0
Verde var GPIO.1
Azul var GPIO.2
Bot1 var GPIO.3
Bot2 var GPIO.5
Todos var bit
Cont var byte
Tiempo var byte
SiNo var word

gpio=0 'Turn off LEDs
todos=0

Pral:
gosub do_adc 'Get ADC reading

if !bot1 and !rojo and !Azul then 'If "Red" player is the fastest
high Rojo
gosub do_shuffle
endif

if !bot2 and !Rojo and !azul then 'If "Blue" player is the fastest
high Azul
gosub do_shuffle
endif

if sino=0 then 'If the "Answer" is correct
gpio=0
todos=0
endif

if sino=1 and !todos and (rojo or azul)then 'If the "Answer" is incorrect (First player fails)
toggle azul
toggle rojo
repeat
gosub do_adc
until sino=3
todos=1
endif

if sino=1 and todos and (rojo or azul)then 'If the "Answer" is incorrect (Both players fails)
low azul
low rojo
high verde 'Turn On Green (It's the audience turn's)
repeat
gosub do_adc
until sino=3
todos=0
endif

if sino=1 and verde then 'If the "Answer" is incorrect (Both players and audience fails)
gpio=0
repeat
gosub do_adc
until sino=3
todos=0
endif

goto pral

Do_ADC:
PAUSEUS 50 ' Wait for A/D channel acquisition time
ADCON0.1 = 1 ' Start conversion
WHILE ADCON0.1 ' Wait for it to complete
WEND
sino = ADRESH>>6
return

Do_Shuffle: 'Just a little dramatic effect ;)
tiempo=50
for cont = 1 to 32
toggle rojo
toggle azul
pause tiempo
tiempo=tiempo+5
next cont
return

end

ivanrosales
- 31st August 2011, 16:21
Ops!, there is a 1k pullup resistor missing in the schematic, so please add this to the pin 4...

Errare humanum est ;)

lester
- 1st September 2011, 06:38
Great, your code exaple is in the User Projects section, here (http://www.picbasic.co.uk/forum/content.php?r=433-A-little-Quiz-game-%28Two-players-audience%29-with-PIC12F675). Thank you for taking time to post this project. We appreciate you taking time to post this.

oldisgold
- 9th September 2011, 08:25
i wud like to extend it to more players [say, 4 players]. can u help me by showing how to extend ?

mister_e
- 9th September 2011, 09:58
Good stuff, like Lester said good code EXAPLE ;)

lester
- 9th September 2011, 10:53
olisgold..

Please, at least try. Ivan has made a great example, try to read it understand it, ask questions about the code flow and try modification yourself.

Its really unreasonable to ask Ivan to do the work for you....and you learn nothing by that process.

ivanrosales
- 10th September 2011, 05:37
Dear olisgold, you can count on me if you get stuck, but I would like to see at least one try, because as lester said, it's the best way to learn.

As a little tip, you can expand the game with diferent approaches, one could be with charlieplexed LEDs (controling them with 3 pins) and then use the 3 remaining pins for the buttons (as in the example, using them as analog inputs to read 2 buttons per pin)

Regards.

ivanrosales
- 10th September 2011, 05:39
mister_e, I think lester is referring to Steve Jobs (Ex-Apple) :)

mister_e
- 10th September 2011, 10:55
Rofl! Win! :D