/* This program is the "brains" of our self-scoring bean bag toss game. This program incorporates the sensor system with the team scores display as well as the games won display. It has wireless code that communicates with the Zigbee part. It also has code that accomodates the reset button. One goal of this program is to have the boards keep track of the score and games won without having to reset the game until two new teams want to start a game. EESDlib contains all of the functions and EESD.h is the header file. */ #include #include "EESD.h" #pragma DATA _CONFIG1H, _OSC_INTIO67_1H #pragma DATA _CONFIG2H, _WDT_OFF_2H #pragma DATA _CONFIG4L, _LVP_OFF_4L #pragma DATA _CONFIG3H, _MCLRE_ON_3H #pragma CLOCK_FREQ 8000000 // Declaring global variables bool gameisnotover = 1; // Game over flag bool sflag1 = 1; // Sensor 1 flag bool sflag2 = 1; // Sensor 2 flag bool sflag3 = 1; // Sensor 3 flag bool sflag4 = 1; // Sensor 4 flag bool sflag5 = 1; // Sensor 5 flag bool sflag6 = 1; // Sensor 6 flag bool reset = 1; // Reset flag void main(void) { //trisa.2 = 0; //trisa.3 = 1; //delay_s(2); osccon = 0x72; // Clock Initialization //LCD_init(); //LCD_printf("Start"); delay_s(1); //LCD_clear(); // SPI Initialization trisc.5 = 0; //Make it an output (Enables SDO) trisc.3 = 0; //Make it an output (Enables SCK) trisd.3 = 0; //Make it an output (load CS for LED maxim) trisd.4 = 0; //Make it an output (load CS for SEVEN maxim) sspcon1 = 0x22; //Control Register (SPI MODE) //Master Synchronous Seial Port Enable, enables serial port and configures SCK, SDO, SDI, and invSS as serial port pins. //SPI Master Mode clock = Fosc/64 sspstat = 0x40; //Status Register (SPI MODE) CKE Trasmit occurs on transition from active to idle clock state. // Zigbee Initialization Serial_init(); // Initialize UART for Zigbee transmission interrupt_init(); // Initialize interrupt function // Phototransistor Initialization trisb |= 00000001b; // Photo1 input B1 trisb |= 00000010b; // Photo2 input B2 trisb |= 00000011b; // Photo3 input B3 trisb |= 00000100b; // Photo4 input B4 trisb |= 00000101b; // Photo5 input B5 trisa |= 00000001b; // Photo6 input A1 volatile bit data1; // Assigns variables to Photo inputs volatile bit data2; volatile bit data3; volatile bit data4; volatile bit data5; volatile bit data6; data1 = portb.1; data2 = portb.2; data3 = portb.3; data4 = portb.4; data5 = portb.5; data6 = porta.1; // Set Games Won flags for each team int team1_gw = 0; int team2_gw = 0; reset = 1; LED_maxim_init(); delay_ms(50); SEVEN_maxim_init(); while(reset) // While game has not been reset { // LED Initialization //Initialize maxim with set registers. //bounce_blink(); // Blink LEDs on all holes at same time delay_ms(100); blink1(); all_on1(); delay_ms(100); blink2(); all_on2(); delay_ms(100); blink3(); all_on3(); delay_ms(100); blink4(); all_on4(); delay_ms(100); blink5(); all_on5(); delay_ms(100); blink6(); all_on6(); // Seven Segment Display Initialization int score_val1 = 0; int score_val2 = 0; // Display Team Scores and Games Won score1(score_val1); LCD_init(); LCD_printf("Score 1"); delay_s(1); LCD_clear(); score2(score_val2); LCD_init(); LCD_printf("Score 2"); delay_s(1); LCD_clear(); gameswon1(team1_gw); gameswon2(team2_gw); // Flag Initialization gameisnotover = 1; // Flag that game is not over sflag1 = 1; // Sensor 1 flag sflag2 = 1; // Sensor 2 flag sflag3 = 1; // Sensor 3 flag sflag4 = 1; // Sensor 4 flag sflag5 = 1; // Sensor 5 flag sflag6 = 1; // Sensor 6 flag while(gameisnotover) { //LCD_init(); //LCD_printf("Game is starting"); delay_s(1); //LCD_clear(); data1 = portb.1; data2 = portb.2; data3 = portb.3; data4 = portb.4; data5 = portb.5; data6 = porta.1; if(sflag1==0 && sflag2==0 && sflag3==0 && sflag4==0 && sflag5 ==0 && sflag6==0) // If all holes have been hit... { Send_Zigbee(7); //Send Zigbee that game is over so other board knows //LCD_init(); //LCD_printf("Game is over"); delay_s(1); //LCD_clear(); team1_gw = team1_gw + 1; // Increase Team 1 Games Won value by 1 if(team1_gw == 10) // If Team 1 has won 10 games, display a 0 on Games Won (because the highest number that can be shown is 9) { team1_gw = 0; } gameswon1(team1_gw); // Display Team 1 Games Won Value Send_Zigbee(team1_gw+10); // Send Team 1 Games Won Value to other board gameisnotover = 0; // Game is over sflag1 = 0; sflag2 = 0; sflag3 = 0; sflag4 = 0; sflag5 = 0; sflag6 = 0; delay_ms(100); bounce_blink(); // Blink lights } if(sflag1 == 1) // If the hole 1 has not been hit.. { if(data1 == 0) // If hole 1 is not covered { //LCD_init(); //LCD_printf("Check"); delay_ms(250); //LCD_clear(); data1 = portb.1; } else // Hole is covered { //lata.2 = 1; //LCD_printf("Pin 1 Covered"); delay_ms(250); //LCD_clear(); delay_ms(100); blink1(); // Blink lights delay_ms(100); //all_off1(); // Turn off lights score_val1 = score_val1 + 1; // Increase Team 1 score by 1 score1(score_val1); // Output new Team 1 score on 7 seg display //LCD_init(); //LCD_printf("Score 1"); delay_s(1); //LCD_clear(); Send_Zigbee(score_val1); // Send score on Zigbee to other board delay_s(1); LCD_clear(); data1 = portb.1; sflag1 = 0; // Turn sensor 1 flag off //delay_s(4); //lata.2 = 0 } } if(sflag2 == 1) // If hole 2 has not been hit { if(data2 == 0) // If hole 2 is not covered { //LCD_printf("Pin 2 Not Covered"); delay_ms(250); //LCD_clear(); data2 = portb.2; } else // Hole 2 is covered { //lata.2 = 1; //LCD_printf("Pin 2 Covered"); delay_ms(250); //LCD_clear(); delay_ms(100); blink2(); // Blink LEDs delay_ms(100); //all_off2(); // Turn LEDs off score_val1 = score_val1 + 1; // Increase score value by 1 for Team 1 score1(score_val1); // Output new Team 1 score on 7 seg display //LCD_init(); //LCD_printf("Score 1"); delay_s(1); //LCD_clear(); Send_Zigbee(score_val1); // Send score on Zigbee to other board delay_s(1); //LCD_clear(); data2 = portb.2; sflag2 = 0; // Turn sensor flag off //delay_s(4); //lata.2 = 0; } } if(sflag3 == 1) // If hole 3 has not been hit { if(data3 == 0) // If hole 3 is not covered { LCD_printf("Pin 3 Not Covered"); delay_ms(250); LCD_clear(); data3 = portb.3; } else // Hole 3 is covered { //lata.2 = 1; //LCD_printf("Pin 3 Covered"); delay_ms(250); //LCD_clear(); delay_ms(100); blink3(); // Blink LEDs delay_ms(100); //all_off3(); // Turn LEDs off score_val1 = score_val1 + 1; // Increase score value by 1 for Team 1 score1(score_val1); // Output new Team 1 score on 7 seg display //LCD_init(); //LCD_printf("Score 1"); delay_s(1); //LCD_clear(); Send_Zigbee(score_val1); // Send score on Zigbee to other board delay_s(1); //LCD_clear(); data3 = portb.3; sflag3 = 0; // Turn sensor flag off //delay_s(4); //lata.2 = 0; } } if(sflag4 == 1) // If hole 4 has not been hit { if(data4 == 0) // If hole 4 is not covered { //LCD_printf("Pin 4 Not Covered"); delay_ms(250); //LCD_clear(); data4 = portb.4; } else // Hole 4 is covered { //lata.2 = 1; //LCD_printf("Pin 4 Covered"); delay_ms(250); //LCD_clear(); delay_ms(100); blink4(); // Blink LEDs delay_ms(100); //all_off4(); // Turn LEDs off score_val1 = score_val1 + 1; // Increase score value by 1 for Team 1 score1(score_val1); // Output new Team 1 score on 7 seg display //LCD_init(); //LCD_printf("Score 1"); delay_s(1); //LCD_clear(); Send_Zigbee(score_val1); // Send score on Zigbee to other board delay_s(1); //LCD_clear(); data4 = portb.4; sflag4 = 0;// Turn sensor flag off //delay_s(4); //lata.2 = 0; } } if(sflag5 == 1) // If hole 5 has not been hit { if(data5 == 0) // If hole 5 is not covered { //LCD_printf("Pin 5 Not Covered"); delay_ms(250); //LCD_clear(); data5 = portb.5; } else // Hole 5 is covered { //lata.2 = 1; //LCD_printf("Pin 5 Covered"); delay_ms(250); //LCD_clear(); delay_ms(100); blink5(); // Blink LEDs delay_ms(100); //all_off5(); // Turn LEDs off score_val1 = score_val1 + 1; // Increase score value by 1 for Team 1 score1(score_val1); // Output new Team 1 score on 7 seg display //LCD_init(); //LCD_printf("Score 1"); delay_s(1); //LCD_clear(); Send_Zigbee(score_val1); // Send score on Zigbee to other board delay_s(1); //LCD_clear(); data5 = portb.5; sflag5 = 0; // Turn sensor flag off //delay_s(4); //lata.2 = 0; } } if(sflag6 == 1) // If hole 6 has not been hit { if(data6 == 0) // If hole 6 is not covered { //LCD_printf("Pin 5 Not Covered"); delay_ms(250); //LCD_clear(); data6 = porta.1; } else // Hole 6 is covered { //lata.2 = 1; //LCD_printf("Pin 6 Covered"); delay_ms(250); //LCD_clear(); delay_ms(100); blink6(); // Blink LEDs delay_ms(100); //all_off6(); // Turn LEDs off score_val1 = score_val1 + 1; // Increase score value by 1 for Team 1 score1(score_val1); // Output new Team 1 score on 7 seg display //LCD_init(); //LCD_printf("Score 1"); delay_s(1); //LCD_clear(); Send_Zigbee(score_val1); // Send score on Zigbee to other board delay_s(1); //LCD_clear(); data6 = porta.1; sflag6 = 0; // Turn sensor flag off //delay_s(4); //lata.2 = 0; } } /* if(*/ } } //delay_s(2); }