Let's Make Robots!

Line Follower Robot using a PIC18F252 microcontroller

I am using C Programming to program a PIC18F252 microcontroller chip that allows a robot to follow a line then change directions in different direction on each end and also senses blockage and goes back. This is my code wich at the moment is giving me problems allowing the robot to change directions in different directions and sense blockage...can anyone see where i am going wrong and help out?????????

 


#include <p18F252.h>

void move (void);
void turn (void);
int temp;
int flag = 0;
void delay (void)
{
 //set the timer 2 to produce certain delay
 // by loading T2CON and PR2 registers
    if(temp == 0x00)
    {
 T2CON = 0x49;
 PR2 = 0x7C;
    }
  
    if (temp == 0x01)
    {
 T2CON  =  0x21;
 PR2  =  0x7C;
    }
 

 T2CONbits.TMR2ON = 1;        // start the timer 2
 
while (!PIR1bits.TMR2IF);       // wait for timer flag
 
 T2CONbits.TMR2ON = 0;        // stop the Timer 2
 PIR1bits.TMR2IF = 0;        // clear timer flag
}
void main (void)
{
    //ADCON0bits.CHS2 = 0;
    //ADCON0bits.CHS1 = 0;
  //ADCON0bilts.CHS0 = 0;
  //ADCON0bits.ADON = 1;
    ADCON1  =  0x07;                      // PORTA is digital
    TRISB  =  0x00;                       // configure PORTB as output
    TRISA  =  0xFF;                       // cofigure PORTA as input
 PORTB  =  0xFF;
      while(1){            // run forever
         move  ();
   turn ();
  //ADCON0bits.GO_DONE = 1;       //start conversion
  //while(ADCON0bits.GO_DONE);     //wait for conversion end
  // PORTB = ADRESH;          //send to DAC
   //temp = PORTA;
    //temp = temp & 0x01;
            
  // PORTBbits.RB0 = 1;
 //  delay();
   //PORTBbits.RB0 = 0;
   //delay();
  }
 }
void turn (void){

if(flag == 1)
{
     PORTBbits.RB0 = 0;    // enable LHS
  PORTBbits.RB1 = 1;    // enable direction LHS
  PORTBbits.RB2 = 0;    // enable RHS
  PORTBbits.RB3 = 0;    // disable direction RHS
delay();
}
else if(flag = 0)
{
  PORTBbits.RB0 = 0;    // enable LHS
  PORTBbits.RB1 = 0;    // disable direction LHS
  PORTBbits.RB2 = 0;    // enable RHS
  PORTBbits.RB3 = 1;    // enable direction RHS
delay();
}
}

void move (void){

 if(PORTAbits.RA0 == 0 && PORTAbits.RA1 == 1 && PORTAbits.RA2 == 0)
  {
  PORTBbits.RB0 = 0;    // enable LHS
  PORTBbits.RB1 = 1;    // enable direction LHS
  PORTBbits.RB2 = 0;    // enable RHS
  PORTBbits.RB3 = 1;    // enable direction RHS
     delay();
  }
 else if(PORTAbits.RA0 == 0 && PORTAbits.RA1 == 0 && PORTAbits.RA2 == 1)
  {
  PORTBbits.RB0 = 0;    // enable LHS
  PORTBbits.RB1 = 1;    // enable direction LHS
  PORTBbits.RB2 = 1;    // disable RHS
  PORTBbits.RB3 = 0;    // disable direction RHS
   delay();
  }
 else if(PORTAbits.RA0 == 1 && PORTAbits.RA1 == 0 && PORTAbits.RA2 == 0)
  {
  PORTBbits.RB0 = 1;    // disable LHS
  PORTBbits.RB1 = 0;    // disable direction LHS
  PORTBbits.RB2 = 0;    // enable RHS
  PORTBbits.RB3 = 1;    // enable direction RHS
   delay();
  }
 else if(PORTAbits.RA0 == 0 && PORTAbits.RA1 == 1 && PORTAbits.RA2 == 1)
  {
  PORTBbits.RB0 = 0;    // enable LHS
  PORTBbits.RB1 = 1;    // enable direction LHS
  PORTBbits.RB2 = 1;    // enable RHS
  PORTBbits.RB3 = 1;    // enable direction RHS
   delay();
  }
 else if(PORTAbits.RA0 == 1 && PORTAbits.RA1 == 1 && PORTAbits.RA2 == 0)
  {
  PORTBbits.RB0 = 1;    // enable LHS
  PORTBbits.RB1 = 1;    // enable direction LHS
  PORTBbits.RB2 = 0;    // enable RHS
  PORTBbits.RB3 = 1;    // enable direction RHS
   delay();
  }
 else if(PORTAbits.RA0 == 0 && PORTAbits.RA1 == 0 && PORTAbits.RA2 == 0)
  {
  turn();
  delay();
  }

 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Welcome to LMR!
The first thing I noticed was that you've got "else if(flag = 0)" in your turn() function, which naturally won't work =)

Aside from that I don't see anything obviously wrong, could you give more detail on the specifics of the problem?

tried it with just else and it worked and also had to add a flag comand into my move() function to start on zero.

Am now using an Ultrasonic range finder SRF04Ultrasonic range finder SRF04, to turn the robot when obstructed. So if i place any object infront of it, it senses it and turns away.

any suggestions?

 cheers bud

Do you have the code needed to interface with the SRF04, or do you just need the 'AI' part? The SRF04 is pretty popular, it's worth looking around LMR and the rest of the web to see how other people have used them.

Not at the moment i have just started working on it, but i dont have a working C code for it, i am connecting it to my robot using two wires a Trigger input pulse of 10uS Min, and an Echo pulse output of 10uS to 8mS. This is my first time using the SRF04, any directions will be much appreciated.

 

cheers BUD