Tuesday 12 August 2014

1d electronic ping pong game

I have had this idea to build a 3D ping-pong game for two players, consisting of many LEDs making a cube, and that is how I came up with this much easier and straight-forward 1D game. The project can be built and programmed in even one day if someone has all the required components. The outcome looks like that:



The aim of the game is to press each of the push-buttons once far left/right LED is on. Once a player presses it the 'ball' bounces off and LEDs are being illuminated sequentially in the opposite direction. One player wins when the other one presses the push-button if any other LED is on or does not press it when supposed to. If a player wins the yellow LED, above his push-button, lights up and the other one has to press the push-button in order to start the game over again. When one player wins 3 times the red LED, above the microcontroller, lights up indicating the end of the game. Each time the player successfully presses the button the 'ball' moves faster.

Here is the list of all required electronic components:
  • 1 x toogle switch(I did not have this one so I just used a 2-wire terminal block connector)
  • 2 x push-button
  • 4 x 10k resistor
  • 11 x 560R resistor
  • 11 x LED
  • microcontroller PIC16F628 or any similar(PIC16F628 datasheet)
  • power supply 3-5.5V
  • 1 x 100nF capacitor
  • 1 x 100pF capacitor
  • 6 pins to connect the microcontroller with PICkit 3 In-Circuit Debugger(PICkit 3)
You can also supply the board with 3-5.5V using the PICkit 3. In order to do that you have to use MPLAB X IDE and right click on a project you are working on. Then choose Properties->PICkit 3->Power in Option categories and check the 'Power target circuit from PICkit 3' box. After that you can adjust any supply power in range from 3 to 5.5V.

The following diagram shows the circuit and the wiring:


Above diagram along with the TinyCad design file can also be download from following link:

The only parts which are missing in the diagram are pins 5 and 14, which are to be connected to the ground and power supply, respectively. 

You can download the software from the following link:

The software consists of 3 files:
  • main.c - the main program
  • functions.h - description of all the functions
  • conf.h - configuration bits file
Keep in mind that I am using internal clock instead of the external RC circuit which you can see on the circuit diagram. Thus, the frequency, according to the PIC datasheet, is equal to 4Mhz. If you want to use a different clock input you must go to conf.h and change the following line of code:

#pragma config FOSC = INTOSCCLK 

As you change the frequency all the delays described in my code will be different due the change of clock cycle. The base delay is included in <delays.h>, library of xc8 compiler(you choose it when creating the project in MPLAB). In the functions.h file I have the following function:



void delay_10ms(void)


{
    int i,j;
    for(i=1;i<=10;i++)
    {
        for(j=1;j<=100;j++)
            _delay(10);
    }
}

It correspond to a delay of 10ms. It is based on _delay(10) corresponding to 10 instruction cycles, each of which takes 4 clock cycles. You can calculate the required delay using this formula:

DELAY=N*4/FREQUENCY


Have fun building your own ping-pong game and message me if you have any problems!









No comments:

Post a Comment