how to make "wire loop game" with PIC18f4550 ?

Jan De

New Member
Hi everyone ..

I'm using micro-controller for the first time , so can you please help me in this circuit

I'm trying to do the wire loop game with using 'PIC18f4550'
the player will try to guide a metal loop to the end of the curved wire without touching it .

now I have this LCD circuit I will use it to show the welcome and the 'game over' message at the end.

how can I connect my wire and loop to this circuit ?
and what algorithm should I use to make 3 attemps ,, so that whenever the loop touch the wire the buzzer ON and the first LED will glow .. then the second .. and when the 3 LEDs glow the LCD will show the message ' game over'



 
Hi,

I suppose it'll look like this:

View attachment 73051

Do you know how the LCD, ADC, and the code workings? It is a very simple project, but I think it's fun.

Vizier87.


OK my friend the time I saw your reply I start working on my codes and I've completed the circuit
but I don't know if it's gonna work on the hardware

this is my code

Code:
#pragma config PLLDIV = 5
#pragma config CPUDIV = OSC1_PLL2
#pragma config USBDIV = 2
#pragma config FOSC = HSPLL_HS


#include<p18f4550.h>
#include<delays.h>

#define rs LATAbits.LATA0
#define rw LATAbits.LATA1
#define en LATAbits.LATA2

#define lcdport LATB
#define WIREport PORTAbits.RA3
#define ENDport PORTAbits.RA4
#define BUZZERport LATAbits.LATA5
#define LEDport LATD

void lcd_ini();
void lcdcmd(unsigned char);
void lcddata(unsigned char);
unsigned int i=0;


void lcd_ini()
{
lcdcmd(0x38);	 //16x2, 5x7matrix LCD.	
lcdcmd(0x01);	 //CLEAR DISPLAY AND DDRAM CONTENT	
lcdcmd(0x06);	 //ENTRY MODE	
lcdcmd(0x0C);	 //DISPLAY ON, CURSOR OFF	
}

void lcdcmd(unsigned char cmdout)
{
lcdport=cmdout;	 //Send command to lcdport=PORTB
rs=0;	
rw=0;
en=1;
Delay10KTCYx( 20 );
en=0;
}

void lcddata(unsigned char dataout)
{
lcdport=dataout;	//Send data to lcdport=PORTB
rs=1;
rw=0;
en=1;
Delay10KTCYx( 20 );
en=0;
}


// THE MAIN FUNCTION

void main(void)
{
unsigned char z,i,j;
unsigned char welcome[]= "Are You Ready !!";
unsigned char attemps[3]= {0x01,0x03,0x07};
ADCON1= 0x0f; // make all pins as digital I/O
CMCON= 0x07;  // make all comparators as digital I/O
 
TRISAbits.TRISA0=0; //config RA0 as output pin
TRISAbits.TRISA1=0; // config RA1 as output pin 
TRISAbits.TRISA2=0; // config RA2 as output pin
TRISAbits.TRISA3=1; // config RA3 as input pin
TRISAbits.TRISA4=1; // config RA4 as input pin
TRISAbits.TRISA5=0; // config RA5 as output pin 
LATA=0x00;
  
TRISB=0;	 // Configure Port B as output port
lcdport=0;  // intitialize it with zero
TRISD=0;     // Configure Port D as output port
LEDport=0;  // intialize it with zero

 

lcd_ini();	 // LCD initialization

//   *************


for(z=0; z<16; z++) // to display  "Are You Ready !!" message
  { 
     lcddata(welcome[z]);
  }

for(z=0; z<18; z++)  // some delay
   {
     Delay10KTCYx( 20 );
   }

   lcdcmd(0x01); // clear LCD
  
//   **************


// now display 3 .. 2.. 1 .. GO
 
lcdcmd(0x87); // move cursor to position 7
lcddata('3');
BUZZERport = 1; // buzzer ON
for(i=0; i<8; i++)  // some delay
     {
        Delay10KTCYx( 20 );
     } 
BUZZERport =0;  // buzzer OFF


lcdcmd(0x01); // clear LCD
lcdcmd(0x87);
lcddata('2');
BUZZERport = 1;  // buzzer ON
for(i=0; i<8; i++)  // some delay
     {
        Delay10KTCYx( 20 );
     } 
BUZZERport =0;  // buzzer OFF


lcdcmd(0x01); // clear LCD
lcdcmd(0x87);
lcddata('1');
BUZZERport = 1;   // buzzer ON
for(i=0; i<8; i++)  // some delay
     {
        Delay10KTCYx( 20 );
     } 
BUZZERport =0;   // buzzer OFF

lcdcmd(0x01); // clear LCD
lcdcmd(0x84); // position 4
lcddata('G'); 
lcddata('O');
lcddata('!');
lcddata('!');
for(i=0; i<8; i++)
     {
        Delay10KTCYx( 20 );
     } 
lcdcmd(0x01);

//     ************
// to drive the stepper motor ( the start ring ) 

for(i=0; i<2; i++)
 {
   LATD= 0XE0;  //0111
   Delay10KTCYx( 30 );
   LATD= 0XD0;  //1011
   Delay10KTCYx( 30 );
   LATD= 0XB0;  //1101
   Delay10KTCYx( 30 );
   LATD= 0X70;  //1110
   Delay10KTCYx( 30 );
}
   LATD= 0X70;  //0111
   Delay10KTCYx( 30 );
   LATD= 0XB0;  //1011
   Delay10KTCYx( 30 );
   LATD= 0XD0;  //1101
   Delay10KTCYx( 30 );
   LATD= 0XE0;  //1110
   Delay10KTCYx( 30 );

   LATD=0X00;
   




//   ************
// NOW start playing


while(1)   
{
    
     if(WIREport==1 && j!=3)   //  if the loop touch the wire and still the player's Attemps are not finished (not 3)
           {
             
            BUZZERport=1;        // the buzzzer ON
            LEDport=attemps[j];  // glow the the first attemp
            j++;             // then increase the number of attemps
             for(z=0; z<10; z++)  // some delay
              {
               Delay10KTCYx( 20 );
              }
          } 
      else
            BUZZERport=0;   // if the wire not touched then turn OFF the buzzer , but the attemp will stay ON

            for(z=0; z<3; z++)  // some delay
              {
               Delay10KTCYx( 20 );
              }

     if(j==3) // if the attemps reatched 3 ! that's mean the ' Game Over' message should appear
        {
            BUZZERport = 1;  // the buzzer ON
            for(z=0; z<10; z++)  // some delay
              {
               Delay10KTCYx( 20 );
              }
            BUZZERport =0; // the buzzer OFF

           while(1)  // this to show (Game Over) message forever
               {
                 lcdcmd(0x01); // clear LCD
                 lcdcmd(0x83);
                 lcddata('G');
                 lcddata('a');
                 lcddata('m');
                 lcddata('e');
                 lcdcmd(0x88);
                 lcddata('O');
                 lcddata('v');
                 lcddata('e');
                 lcddata('r');
                 for(i=0; i<20; i++)  // some delay
                       {
                       Delay10KTCYx( 20 );
                       }
               }
         }  


        if(ENDport==1) // here if the attemps are not 3, ant the player have reached the END, one of these message will appear
         {
             if(j==0)  // first: if no one of the attemps' LEDs is glowing :)
                {
                   while(1) // then show  "Awesome !" message forever  
                       {
                        lcdcmd(0x01); // clear LCD
                        lcdcmd(0x84); // first line ,4th position
                        lcddata('A');
                        lcddata('W');
                        lcddata('E');
                        lcddata('S');
                        lcddata('O');
                        lcddata('M');
                        lcddata('E');
                        lcddata('!');
                        for(i=0; i<20; i++)  // some delay
                          {
                            Delay10KTCYx( 20 );
                          } 
                       }
               }



       else if(j==1) // second: if one of the attmps' LEDs is glowing
          {
               while(1) // then show "GREATE :)" message forever
                  {
   
                     lcdcmd(0x01); // clear LCD
                     lcdcmd(0x84); // first line and 4th position
                     lcddata('G');
                     lcddata('R');
                     lcddata('E');
                     lcddata('A');
                     lcddata('T');
                     lcdcmd(0x8A);
                     lcddata(':');
                     lcddata(')');
                     for(i=0; i<20; i++)  // some delay
                         {
                         Delay10KTCYx( 20 );
                         }    

                  }
           }


         else if(j==2) // if two of the attemps' LEDs are glowing 
           {
               while(1) // then show "GOOD" message forever
                  {
   
                    lcdcmd(0x01); // clear LCD
                    lcdcmd(0x84); // firt line and 4th position
                    lcddata('G');
                    lcddata('o');
                    lcddata('o');
                    lcddata('d');
                    lcdcmd(0x89);
                    lcddata('^');
                    lcddata('^');
                    for(i=0; i<20; i++) // some delay
                        {
                         Delay10KTCYx( 20 );
                        }   

                 }
           }

         }

}
  
}



what's my circuit do is ..

* first it will show the welcome message
* then the motor will move << I use it here to drive the start ring
* after that the player will start .
* The time I bush RA3 button .. the buzzer in RC5 will turn ON and one of the attempts on portD will glow
* if all the 3 attempts glow .. the game is over
* if not .. and I could reach the end which is by pressing RC4 button , the LCD will display one of three messages depending on the number of attempts I got.

I know it's an easy circuit but I feel like I did great job to completed it and I'm praying to work fine on hardware

so what do you think about it ? do you think it's OK or still I have to change it
and also I have a question ..
is it important to give the PIC 5v exactly ? and if so , how can I drive it from 6v !

thank you for your reply
 
Hi Jan De,

A few tests are required anyway, so have you got the LCD displaying already? Sorry I was late in replying, I was quite busy.

The whole setup looks fine, but what's important is that you got each part working. Make your progresses step-by-step, so it'll be easier to troubleshoot if you have problems with your programming.

Cheers.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…