I've already posted a few threads in this forum for help with some basics, but this is the project that I am working towards. The code below works but I think it'll make most people here cringe! 
I have 4 input switches (RA0 to RA3) and 4 output LEDs (RB0 to RB3). The game starts with an LED countdown. Once the countdown is over, the first player to hit the switch lights their corresponding LED. I added a basic check to prevent cheating (by holding the switch during the countdown).
I would really appreciate any feedback as to how I can improve this program. At the moment, the winning LED remains lit as it is stuck in a never-ending loop. I tried to come up with a better solution but I'm not sure how. I'd also like to add a buzzer and perhaps some chasing LED effects.
Thanks in advance.
I have 4 input switches (RA0 to RA3) and 4 output LEDs (RB0 to RB3). The game starts with an LED countdown. Once the countdown is over, the first player to hit the switch lights their corresponding LED. I added a basic check to prevent cheating (by holding the switch during the countdown).
I would really appreciate any feedback as to how I can improve this program. At the moment, the winning LED remains lit as it is stuck in a never-ending loop. I tried to come up with a better solution but I'm not sure how. I'd also like to add a buzzer and perhaps some chasing LED effects.
Thanks in advance.
Rich (BB code):
#include "htc.h"
#define _XTAL_FREQ 4000000
#define delay_s(T) {unsigned char i; for (i=0; i<T*10; i++) __delay_ms(100);}
__CONFIG(FOSC_INTRC_NOCLKOUT & WDTE_OFF & PWRTE_ON & MCLRE_ON & CP_OFF & CPD_OFF & BOREN_OFF & IESO_OFF & FCMEN_OFF & LVP_OFF & DEBUG_OFF & BOR4V_BOR40V & WRT_OFF);
char team0_ok, team1_ok, team2_ok, team3_ok;
int main(void) {
TRISA = 0b00001111; // RA0 to RA3 are inputs, RA4 to RA7 are outputs
TRISB = 0b11110000; // RB0 to RB3 are outputs, RB4 to RB7 are inputs
ANSEL = 0; // configure PORTA analog channels as a digital inputs
// initial LED "countdown"
PORTB = 0b00000001;
delay_s(1);
PORTB = 0b00000011;
delay_s(1);
PORTB = 0b00000111;
delay_s(1);
PORTB = 0b00001111;
delay_s(3);
PORTB = 0; // no output LEDs
// reset team_ok values
team0_ok = 1;
team1_ok = 1;
team2_ok = 1;
team3_ok = 1;
// check whether an input is already high (cheating!)
if(RA0 == 1){
team0_ok = 0;
}
if(RA1 == 1){
team1_ok = 0;
}
if(RA2 == 1){
team2_ok = 0;
}
if(RA3 == 1){
team3_ok = 0;
}
// loop to detect an input and light the corresponding LED
for(;;) {
if((RA0 == 1)&&(team0_ok == 1)){
for(;;){RB0 = 1;}
}
else if((RA1 == 1)&&(team1_ok == 1)){
for(;;){RB1 = 1;}
}
else if((RA2 == 1)&&(team2_ok == 1)){
for(;;){RB2 = 1;}
}
else if((RA3 == 1)&&(team3_ok == 1)){
for(;;){RB3 = 1;}
}
else {
// nothing -- is this ok?
}
}
}
Last edited: