Question

In: Electrical Engineering

An example of a PIC C program code for line following and obstacle avoiding robot? For...

An example of a PIC C program code for line following and obstacle avoiding robot? For it move forward and avoid an obstacle? PIC18F86722

Solutions

Expert Solution


/* program for Line Follower using PIC16F877A */


#include <xc.h>

#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)

void main()
{
TRISD2 = 1; TRISD3 = 1; //Bath the IR sensor pins are declared as input
TRISC4 = 0; TRISC5 = 0; //Motor 1 pins declared as output
TRISC6 = 0; TRISC7 = 0; //Motor 2 pins declared as output


while(1)
{

if (RD2==1 && RD3==1) //Both sensor not over balck line
{
RC4=0; RC5=1; //Motor 1 forward
RC6=1; RC7=0; //Motor 2 forward
}

else if (RD2==0 && RD3==1) //Left sensor is over black line
{
RC4=1; RC5=1; //Motor 1 stop
RC6=1; RC7=0; //Motor 2 forward
}

else if (RD2==1 && RD3==0) //Right sensor is over black line
{
RC4=0; RC5=1; //Motor 1 forward
RC6=1; RC7=1; //Motor 2 stop
}

else //Both Sensor over black line
{
RC4=1; RC5=1; //Motor 1 stop
RC6=1; RC7=1; //Motor 2 stop
}

}
}

/*
Obstacle avoider using PIC16F877A
*/

#include <xc.h>

#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)

#define _XTAL_FREQ 20000000
#define Trigger RB1 //34 is Trigger
#define Echo RB2//35 is Echo

int time_taken;
int distance;


void back_off() //used to drive the robot backward
{
RC4=1; RC5=0; //Motor 1 reverse
RC6=0; RC7=1; //Motor 2 reverse
__delay_ms(1000);
}

void calculate_distance() //function to calculate distance of US
{
TMR1H =0; TMR1L =0; //clear the timer bits
  
Trigger = 1;
__delay_us(10);
Trigger = 0;
  
while (Echo==0);
TMR1ON = 1;
while (Echo==1);
TMR1ON = 0;
  
time_taken = (TMR1L | (TMR1H<<8));
distance= (0.0272*time_taken)/2;   
}

void main()
{
TRISD = 0x00; //PORTD declared as output for interfacing LCD
TRISB1 = 0; //Trigger pin of US sensor is sent as output pin
TRISB2 = 1; //Echo pin of US sensor is set as input pin
TRISB3 = 0; //RB3 is output pin for LED
  
TRISD2 = 1; TRISD3 = 1; //Both the IR sensor pins are declared as input
TRISC4 = 0; TRISC5 = 0; //Motor 1 pins declared as output
TRISC6 = 0; TRISC7 = 0; //Motor 2 pins declared as output

T1CON=0x20;


while(1)
{

calculate_distance();   
if (distance>5)
{
RC4=0; RC5=1; //Motor 1 forward
RC6=1; RC7=0; //Motor 2 forward
}

calculate_distance();
if (RD2==0 && RD3==1 && distance<=5) //Left sensor is blocked
{
back_off();
RC4=1; RC5=1; //Motor 1 stop
RC6=1; RC7=0; //Motor 2 forward
__delay_ms(500);
}
  
calculate_distance();
if (RD2==1 && RD3==0 && distance<=5) //Right sensor is blocked
{
back_off();
RC4=0; RC5=1; //Motor 1 forward
RC6=1; RC7=1; //Motor 2 stop
__delay_ms(500);
}
  
calculate_distance();
if (RD2==0 && RD3==0 && distance<=5)//Both sensor is open
{
back_off();
RC4=0; RC5=1; //Motor 1 forward
RC6=1; RC7=1; //Motor 2 stop
__delay_ms(500);
}
  
calculate_distance();
if (RD2==1 && RD3==1 && distance<=5)//Both sensor is blocked
{
back_off();
RC4=1; RC5=0; //Motor 1 reverse
RC6=1; RC7=1; //Motor 2 stop
__delay_ms(1000);
}

}
}


Related Solutions

An example of a PIC C program code for line following and obstacle avoiding robot? For...
An example of a PIC C program code for line following and obstacle avoiding robot? For it move forward and avoid an obstacle?
A PIC C code for Interfacing Ultrasonic sensor using PIC C 18F8722 microcontroller  
A PIC C code for Interfacing Ultrasonic sensor using PIC C 18F8722 microcontroller  
Create a schematic of a circuit using and write C program to run on a PIC...
Create a schematic of a circuit using and write C program to run on a PIC 16F88 that will flash between a red and green LED at 5Hz with the green on 75% of the time and the red on 25% of the time.
Need C++ code for following with a document describing the code as well: Write a program...
Need C++ code for following with a document describing the code as well: Write a program to implement the game of Tic Tac Toe Four. The game is played on a 4 × 4 chessboard, within 2 players. The player who is playing "X" always goes first. Players alternate placing Xs and Os on the board until either one player has four in a row, horizontally, vertically, or diagonally; or all 16 squares are filled(which is a draw). The program...
An example of an integrative technology is a/an: A) Transportation management system. B) Industrial robot. C)...
An example of an integrative technology is a/an: A) Transportation management system. B) Industrial robot. C) Radio frequency identification (RFID) system. D) Enterprise resource planning (ERP) system.
Write a C program (using Code::blocks) that outputs a formatted header line and creates 10 children...
Write a C program (using Code::blocks) that outputs a formatted header line and creates 10 children processes each of which displays a line of the output as shown below. Notice the values of Child no and x, they have to be the same as shown here while the processes PIDs values are based on the what your system assigns to these processes. Child    PID        PPID      X 0           13654    13653    5 1           13655    13653    10 2           13656    13653    15 3           13657   ...
write pseudocode for the following problems not c code Pseudocode only Write a C program to...
write pseudocode for the following problems not c code Pseudocode only Write a C program to print all natural numbers from 1 to n. - using while loop Write a C program to print all natural numbers in reverse (from n to 1). - using while loop Write a C program to print all alphabets from a to z. - using while loop Write a C program to print all even numbers between 1 to 100. - using while loop...
I need to write a program in C with the following specification * ​​​​​​​Line one of...
I need to write a program in C with the following specification * ​​​​​​​Line one of the standard input will have the total number of people which must not be greater than 10 * Each of the subsequent lines of input will be the first name of a person and their number (ex. "Adam 85") one space between name and number * Each name must be a maximum of 14 characters * Put the first names into an array called...
I need assistance translating a custom C++ program to MIPS. My C++ code is the following:...
I need assistance translating a custom C++ program to MIPS. My C++ code is the following: I have made numerous attempts on my own to no avail, any assistance is appreciated. Also, template code for this solution is provided below: #include int moveRobots(int *, int *, int, int ); int getNew(int, int); int main() { int x[4], y[4], i, j, myX = 25, myY = 25, move, status = 1; // initialize positions of four robots x[0] = 0; y[0]...
Design a line following robot by using AT89C51 microcontroller, DC motors and line sensors. Use Photo...
Design a line following robot by using AT89C51 microcontroller, DC motors and line sensors. Use Photo transistors and LEDS to design a line sensor. Use DC motos and attach DC motors to Microcontroller by using H-Bridge IC or circuit. Use assembly language for programming. Draw the model and simulate it. Show all the schematics and connections. After Designing Line Follower, convert it into maze solver by using your own algorithm. The robot should start and reach the end point in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT