The objective is to have the Robot “SEARCH” for a black line. When a black line
ID: 3825768 • Letter: T
Question
The objective is to have the Robot “SEARCH” for a black line. When a black line is detected the robot will then “FOLLOW” the black line. If the line ends the robot will turn around and go back following the black line again. This will go on forever.
I have the code completed up to the point where I have no red errors
I NEAD YOU TO FIX THE COMPILING ERRORS POSTED IN THE IMAGE
The CODE IS IN C
I am USING THE chipKIT Pro MX4 : Embedded Systems Trainer Board. With The Microchip® PIC32MX460F512L.
I am Programing in MPLAB X IDE V3.50
I am using 4 IR Proximity Sensors. When the Line is black, the reading is 0 and when it is white the sensor reads 1.
The motors are controlled using a PWM ISR operation and are controlled using the OC2 and OC3 registers.
Please comment all the code so I can follow along.
Please post the entire Code when finished.
Bellow is a picture of the course the robot will be running and the COMPILING ERROR
make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf
make[1]: Entering directory 'C:/Users/andri/MPLABXProjects/jazz.X'
make -f nbproject/Makefile-default.mk dist/default/production/jazz.X.production.hex
make[2]: Entering directory 'C:/Users/andri/MPLABXProjects/jazz.X'
"C:Program Files (x86)Microchip21.42in2-gcc.exe" -g -x c -c -mprocessor=32MX460F512L -MMD -MF build/default/production/jazz.o.d -o build/default/production/jazz.o jazz.c -DXPRJ_default=default -legacy-libc
nbproject/Makefile-default.mk:111: recipe for target 'build/default/production/jazz.o' failed
make[2]: Leaving directory 'C:/Users/andri/MPLABXProjects/jazz.X'
nbproject/Makefile-default.mk:84: recipe for target '.build-conf' failed
make[1]: Leaving directory 'C:/Users/andri/MPLABXProjects/jazz.X'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
jazz.c:54:6: error: 'TurnLeft' redeclared as different kind of symbol
void TurnLeft();
^
jazz.c:34:43: note: previous definition of 'TurnLeft' was here
typedef enum{Forward, Backward, TurnRight,TurnLeft} Movement_Mode;
^
jazz.c:55:6: error: 'Stop' redeclared as different kind of symbol
void Stop();
^
jazz.c:32:39: note: previous definition of 'Stop' was here
typedef enum{Line_Search, Line_Track, Stop} Operation_Mode;
^
jazz.c: In function 'T1Interrupt':
jazz.c:293:18: warning: comparison between pointer and integer [enabled by default]
if (mode == Stop)
^
jazz.c:390:34: error: incompatible types when assigning to type 'Operation_Mode' from type 'void (*)()'
mode = Stop;
^
jazz.c:413:34: error: incompatible types when assigning to type 'Operation_Mode' from type 'void (*)()'
mode = Stop;
^
jazz.c:437:34: error: incompatible types when assigning to type 'Operation_Mode' from type 'void (*)()'
mode = Stop;
^
jazz.c:441:31: error: incompatible types when assigning to type 'Movement_Mode' from type 'void (*)()'
mode2 = TurnLeft;
^
jazz.c:459:14: error: incompatible types when assigning to type 'Operation_Mode' from type 'void (*)()'
mode = Stop;
^
make[2]: *** [build/default/production/jazz.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 743ms)
****************************************************************
My Code:
***************************************
//main.c
#include <xc.h> /* contains Vector Name/Number Macros */
#include <sys/attribs.h> /* contains __ISR() Macros */
#include <stdio.h>
/************************Oscillator Settings****************************/
#pragma config FNOSC = PRIPLL // Oscillator selection
#pragma config POSCMOD = EC // Primary oscillator mode
#pragma config FPLLIDIV = DIV_2 // PLL input divider
#pragma config FPLLMUL = MUL_20 // PLL multiplier
#pragma config FPLLODIV = DIV_1 // PLL output divider
#pragma config FPBDIV = DIV_8 // Peripheral bus clock divider
#pragma config FSOSCEN = OFF // Secondary oscillator enable
#pragma config IESO = OFF // Internal/external clock switchover
#pragma config FCKSM = CSDCMD // Clock switching (CSx)/Clock monitor(CMx)
#pragma config OSCIOFNC = OFF // Clock output on OSCO pin enable
#pragma config UPLLEN = ON // USB PLL enable
#pragma config UPLLIDIV = DIV_2 // USB PLL input divider
/******************Other Peripheral Device settings*********************/
#pragma config FWDTEN = OFF // Watchdog timer enable
#pragma config WDTPS = PS1024 // Watchdog timer post-scaler
/*******************Code Protection settings******************************/
#pragma config CP = OFF // Code protection
#pragma config BWP = OFF // Boot flash write protect
#pragma config PWP = OFF // Program flash write protect
typedef enum{Line_Search, Line_Track, Stop} Operation_Mode;
typedef enum{On, Off} Power_State_Mode;
typedef enum{Forward, Backward, TurnRight,TurnLeft} Movement_Mode;
Power_State_Mode Power = Off; // on or off state
Operation_Mode mode = Stop; //operation state
Movement_Mode mode2 = Backward; //movement state
/*********************LOCAL PROTOTYPES*******************************/
void InitializeSystem();
void Initialize_Timer1();
void Initialize_Timer2();
void Initialize_Timer3();
void Initialize_Timer4();
void Initialize_OC();// Initialize hardware and global variables
void Initialize_IO();
void Initialize_IC();
void Interrupts_enable();
void Motor_Forward();
void Motor_Backward();
void centerRight();
void turnRight();
void centerLeft();
void TurnLeft();
void Stop();
void Power_On();
void Find_Line();
/** main() ********************************************************************/
unsigned int Timer3OV2 = 0;
unsigned int Timer3OV3 = 0;
int main(void)
{
InitializeSystem();
Interrupts_enable();
while(1)
{
if (PORTAbits.RA6 == 1)
{
Power = On;
}
else if (PORTAbits.RA7 == 1)
{
Power = Off;
}
}
}
void InitializeSystem()
{
Initialize_IO();
Initialize_OC();
Initialize_Timer1();
Initialize_Timer2();
Initialize_Timer3();
Initialize_Timer4();
Initialize_IC();
}
void Initialize_IC()
{
// Initialize IC2
TRISDbits.TRISD9 = 1;
IC2CON= 0x0000;
IC2CONbits.SIDL = 1; // halt in CPU Idle Mode
IC2CONbits.C32 = 0; // 16bit timer;
IC2CONbits.ICTMR = 0; // select Timer 3;
IC2CONbits.ICI = 3; // interrupt every 4 events
IC2CONbits.ICM = 5; // increment every 16 rising edge
// Initialize IC3
TRISDbits.TRISD10 = 1;
IC3CON= 0x0000;
IC3CONbits.SIDL = 1; // halt in CPU Idle Mode
IC3CONbits.C32 = 0; // 16bit timer;
IC3CONbits.ICTMR = 0; // select Timer 3;
IC3CONbits.ICI = 3; // interrupt every fourth event
IC3CONbits.ICM = 5; // increment every 16 rising edge
}
void Initialize_IO()
{
//buttons 1 & 2 = inputs
TRISAbits.TRISA6 = 1;
TRISAbits.TRISA7 = 1;
//LEDs
TRISBbits.TRISB10 = 0;
TRISBbits.TRISB11 = 0;
TRISBbits.TRISB12 = 0;
TRISBbits.TRISB13 = 0;
//LF Sensor ports
TRISGbits.TRISG12 = 1;
TRISGbits.TRISG13 = 1;
TRISGbits.TRISG14 = 1;
TRISGbits.TRISG15 = 1;
//Motor Direction bits
TRISDbits.TRISD6 =0;
TRISDbits.TRISD7 =0;
TRISDbits.TRISD1 =0;
LATDbits.LATD1 = 0;
TRISDbits.TRISD2 =0;
LATDbits.LATD2 =0;
}
void Initialize_OC()
{
// output compare
OC2CONbits.OCM =6;
OC2R = 0;
OC2RS = 0;
OC2CONbits.ON=1;
OC3CONbits.OCM =6;
OC3R = 0;
OC3RS = 0;
OC3CONbits.ON = 1;
/* Set Interrupt Controller for multi-vector mode */
}
void Initialize_Timer1()
{
T1CONbits.TON = 0;
T1CONbits.TCKPS = 3;
PR1 = 250;
TMR1 = 0;
T1CONbits.ON =1;
}
void Initialize_Timer2()
{
T2CONbits.TON = 0;
T2CONbits.TCKPS = 7;
PR2 = 2000;
TMR2 = 0;
T2CONbits.ON =1;
}
void Initialize_Timer3()
{
T3CONbits.TON = 0;
T3CONbits.TCKPS = 7;
PR3 = 0xFFFF;
TMR3 = 0;
T3CONbits.ON =1;
}
void Initialize_Timer4()
{
T4CON = 0x0;
T4CONSET = 0x0070;
PR4 = 0xFFFF;
TMR4 = 0;
T4CONSET = 0x8000;
}
void Interrupts_enable()
{
// Timer one interrupt is enabled when it enters the Path search mode
IPC1bits.T1IP = 7;
IFS0bits.T1IF = 0;
IEC0bits.T1IE = 0;
// Interrupt for IC2
IFS0bits.IC2IF =0;
IPC2bits.IC2IP = 7;
IEC0bits.IC2IE = 1;
//// Interrupt for IC3
IFS0bits.IC3IF =0;
IPC3bits.IC3IP = 7;
IEC0bits.IC3IE = 1;
//turn ON IC2 & IC3
IC2CONbits.ON = 1;
IC3CONbits.ON= 1;
//Interrupt for Timer1
IFS0bits.T1IF =0;
IPC1bits.T1IP = 7;
IEC0bits.T1IE = 1;
INTCONbits.MVEC = 1;
__builtin_enable_interrupts();
}
void Motor_Forward()
{
LATDbits.LATD6 = 1;
LATDbits.LATD7 = 0;
OC2RS = 800;
OC3RS = 800;
}
void Stop()
{
OC2RS = 0;
OC3RS = 0;
}
void centerRight()
{
LATDbits.LATD6 = 1;
LATDbits.LATD7 = 0;
OC2RS = 700; //OC for right motor
OC3RS = 400; //OC for left motor
}
void turnRight()
{
LATDbits.LATD6 = 0;
LATDbits.LATD7 = 0;
OC2RS = 800; //Right motor speed
OC3RS = 800; //Left motor speed
}
void centerLeft()
{
LATDbits.LATD6 = 1;
LATDbits.LATD7 = 0;
OC2RS = 400;
OC3RS = 700;
}
void TurnLeft()
{
LATDbits.LATD6 = 1;
LATDbits.LATD7 = 1;
OC2RS = 800;
OC3RS = 800;
}
void Motor_Backward()
{
LATDbits.LATD6 = 0;
LATDbits.LATD7 = 1;
OC2RS = 800;
OC3RS = 800;
}
void __ISR (_TIMER_1_VECTOR, IPL7SOFT) T1Interrupt(void)
{
//Indexing Variable Declarations
int index = 0;
int rotate = 0;
if (Power == On)
{
if (mode == Stop)
{
if (PORTGbits.RG15 == 0 || PORTGbits.RG14 == 0 || PORTGbits.RG13 == 0 || PORTGbits.RG12 == 0) // Picks up a line
{
mode = Line_Track;
}
else if(PORTGbits.RG15 == 1 && PORTGbits.RG14 == 1 && PORTGbits.RG13 == 1 && PORTGbits.RG12 == 1) // No line found
{
mode = Line_Search;
}
}
else if (mode == Line_Track) //Found line
{
Motor_Forward(); //By default move forward
if (PORTGbits.RG12 == 0 && PORTGbits.RG14 == 1 && PORTGbits.RG15 == 1) //
{
TurnLeft();
}
else if (PORTGbits.RG15 == 0 && PORTGbits.RG12 == 1 && PORTGbits.RG13 == 1)
{
turnRight();
}
else if (PORTGbits.RG13 == 0 && PORTGbits.RG12 == 1 && PORTGbits.RG14 == 1 && PORTGbits.RG15 == 1)
{
centerLeft();
}
else if (PORTGbits.RG14 == 0 && PORTGbits.RG12 == 1 && PORTGbits.RG13 == 1 && PORTGbits.RG15 == 1)
{
centerRight();
}
else if (PORTGbits.RG15 == 0 && PORTGbits.RG14 == 0 && PORTGbits.RG13 == 0 && PORTGbits.RG12 == 1)
{
turnRight();
}
else if (PORTGbits.RG12 == 0 && PORTGbits.RG13 == 0 && PORTGbits.RG14 == 0 && PORTGbits.RG15 == 1)
{
TurnLeft();
}
else if (PORTGbits.RG12 == 0 && PORTGbits.RG13 == 0 && PORTGbits.RG14 == 0 && PORTGbits.RG15 == 0)
{
TMR4 = 0;
index = 0;
while (index < 2)
{
while (TMR4 < 39063) //Second Calculation used from Project 1
{
TurnLeft();
}
index++;
}
TMR4 = 0;
index = 0;
while (index < 2)
{
while (TMR4 < 39063)
{
centerLeft();
}
index++;
}
}
//Goes off line
else if (PORTCbits.RC12 == 1 && PORTCbits.RC13 == 1 && PORTCbits.RC14 == 1 && PORTCbits.RC15 == 1)
{
mode = Line_Search;
}
}
else if (mode == Line_Search)
{
index = 0;
rotate = 0;
while (rotate < 4)
{
//Moves forward for 3 seconds to abide by project requirements
index = 0;
while (index < 10)
{
TMR4 = 0;
while (TMR4 < 39063)
{
if (PORTGbits.RG12 == 0 || PORTGbits.RG13 == 0 || PORTGbits.RG14 == 0 || PORTGbits.RG15 == 0)
{
mode = Line_Track;
goto end;
}
if (PORTAbits.RA7 == 1)
{
mode = Stop;
Power = Off;
goto end;
}
mode2 = Forward;
Motor_Forward();
}
index++;
}
//Backwards Distance = Forwards Difference
index = 0;
while (index < 10)
{
TMR4 = 0;
while (TMR4 < 39063)
{
if (PORTGbits.RG12 == 0 || PORTGbits.RG13 == 0 || PORTGbits.RG14 == 0 || PORTGbits.RG15 == 0)
{
mode = Line_Track;
goto end;
}
if (PORTAbits.RA7 == 1)
{
mode = Stop;
Power = Off;
goto end;
}
mode2 = Backward;
Motor_Backward();
}
index++;
}
//Rotates 90 Degrees
index = 0;
while (index < 5)
{
TMR4 = 0;
while (TMR4 < 9766)
{
if (PORTGbits.RG12 == 0 || PORTGbits.RG13 == 0 || PORTGbits.RG14 == 0 || PORTGbits.RG15 == 0)
{
mode = Line_Track;
goto end;
}
if (PORTAbits.RA7 == 1)
{
mode = Stop;
Power = Off;
goto end;
}
mode2 = TurnLeft;
TurnLeft();
}
index++;
}
rotate++;
}
if (rotate == 4)
{
Power = Off;
}
end:;
}
}
else if (Power == Off)
{
mode = Stop;
Stop();
}
IFS0bits.T1IF = 0;
}
Explanation / Answer
This is the problem of name conflicts.
You have used 2 names two times for different things.
The first error is TurnLeft() redeclared ...
You have first use the word TurnLeft in the enum Movement_Mode and then also you are declaring the function with same name, which is not possible as compiler can't identify 2 different things with same name.
So, wherever you have written TurnLeft(), remember the brackets, instead of TurnLeft(), rename the function as Turn_Left() and this error is no more there.
Don't change anywhere where it is written something like mode2 = TurnLeft. This TurnLeft is the enum Movement_Mode value and it is correct.
The same goes with Stop(). You have written 'Stop' word in Operation_Mode first and you are again declaring the function with the same name.
So you need to rename this function in every place in the file. I would replace it with Stop_() so that the meaning is not changed.
So, once you do this, the next error is also gone.
The last 4 errors displaying : Incompatible types will also be gone now.
The error was coming because if you write mode_1 = Stop, at last compiler found that Stop is a function with return type void, so it assumes that void * is the type of Stop and Operation_Mode is the type of 'mode_1' but you know this Stop belongs to the enum Operation_Mode.
So, once you rename the 2 functions, all the errors will be gone.
The modified code where I replaced the names of these 2 functions is as follows...
Source Code:
//main.c
#include <xc.h> /* contains Vector Name/Number Macros */
#include <sys/attribs.h> /* contains __ISR() Macros */
#include <stdio.h>
/************************Oscillator Settings****************************/
#pragma config FNOSC = PRIPLL // Oscillator selection
#pragma config POSCMOD = EC // Primary oscillator mode
#pragma config FPLLIDIV = DIV_2 // PLL input divider
#pragma config FPLLMUL = MUL_20 // PLL multiplier
#pragma config FPLLODIV = DIV_1 // PLL output divider
#pragma config FPBDIV = DIV_8 // Peripheral bus clock divider
#pragma config FSOSCEN = OFF // Secondary oscillator enable
#pragma config IESO = OFF // Internal/external clock switchover
#pragma config FCKSM = CSDCMD // Clock switching (CSx)/Clock monitor(CMx)
#pragma config OSCIOFNC = OFF // Clock output on OSCO pin enable
#pragma config UPLLEN = ON // USB PLL enable
#pragma config UPLLIDIV = DIV_2 // USB PLL input divider
/******************Other Peripheral Device settings*********************/
#pragma config FWDTEN = OFF // Watchdog timer enable
#pragma config WDTPS = PS1024 // Watchdog timer post-scaler
/*******************Code Protection settings******************************/
#pragma config CP = OFF // Code protection
#pragma config BWP = OFF // Boot flash write protect
#pragma config PWP = OFF // Program flash write protect
typedef enum{Line_Search, Line_Track, Stop} Operation_Mode;
typedef enum{On, Off} Power_State_Mode;
typedef enum{Forward, Backward, TurnRight,TurnLeft} Movement_Mode;
Power_State_Mode Power = Off; // on or off state
Operation_Mode mode = Stop; //operation state
Movement_Mode mode2 = Backward; //movement state
/*********************LOCAL PROTOTYPES*******************************/
void InitializeSystem();
void Initialize_Timer1();
void Initialize_Timer2();
void Initialize_Timer3();
void Initialize_Timer4();
void Initialize_OC();// Initialize hardware and global variables
void Initialize_IO();
void Initialize_IC();
void Interrupts_enable();
void Motor_Forward();
void Motor_Backward();
void centerRight();
void turnRight();
void centerLeft();
void Turn_Left();
void Stop_();
void Power_On();
void Find_Line();
/** main() ********************************************************************/
unsigned int Timer3OV2 = 0;
unsigned int Timer3OV3 = 0;
int main(void)
{
InitializeSystem();
Interrupts_enable();
while(1)
{
if (PORTAbits.RA6 == 1)
{
Power = On;
}
else if (PORTAbits.RA7 == 1)
{
Power = Off;
}
}
}
void InitializeSystem()
{
Initialize_IO();
Initialize_OC();
Initialize_Timer1();
Initialize_Timer2();
Initialize_Timer3();
Initialize_Timer4();
Initialize_IC();
}
void Initialize_IC()
{
// Initialize IC2
TRISDbits.TRISD9 = 1;
IC2CON= 0x0000;
IC2CONbits.SIDL = 1; // halt in CPU Idle Mode
IC2CONbits.C32 = 0; // 16bit timer;
IC2CONbits.ICTMR = 0; // select Timer 3;
IC2CONbits.ICI = 3; // interrupt every 4 events
IC2CONbits.ICM = 5; // increment every 16 rising edge
// Initialize IC3
TRISDbits.TRISD10 = 1;
IC3CON= 0x0000;
IC3CONbits.SIDL = 1; // halt in CPU Idle Mode
IC3CONbits.C32 = 0; // 16bit timer;
IC3CONbits.ICTMR = 0; // select Timer 3;
IC3CONbits.ICI = 3; // interrupt every fourth event
IC3CONbits.ICM = 5; // increment every 16 rising edge
}
void Initialize_IO()
{
//buttons 1 & 2 = inputs
TRISAbits.TRISA6 = 1;
TRISAbits.TRISA7 = 1;
//LEDs
TRISBbits.TRISB10 = 0;
TRISBbits.TRISB11 = 0;
TRISBbits.TRISB12 = 0;
TRISBbits.TRISB13 = 0;
//LF Sensor ports
TRISGbits.TRISG12 = 1;
TRISGbits.TRISG13 = 1;
TRISGbits.TRISG14 = 1;
TRISGbits.TRISG15 = 1;
//Motor Direction bits
TRISDbits.TRISD6 =0;
TRISDbits.TRISD7 =0;
TRISDbits.TRISD1 =0;
LATDbits.LATD1 = 0;
TRISDbits.TRISD2 =0;
LATDbits.LATD2 =0;
}
void Initialize_OC()
{
// output compare
OC2CONbits.OCM =6;
OC2R = 0;
OC2RS = 0;
OC2CONbits.ON=1;
OC3CONbits.OCM =6;
OC3R = 0;
OC3RS = 0;
OC3CONbits.ON = 1;
/* Set Interrupt Controller for multi-vector mode */
}
void Initialize_Timer1()
{
T1CONbits.TON = 0;
T1CONbits.TCKPS = 3;
PR1 = 250;
TMR1 = 0;
T1CONbits.ON =1;
}
void Initialize_Timer2()
{
T2CONbits.TON = 0;
T2CONbits.TCKPS = 7;
PR2 = 2000;
TMR2 = 0;
T2CONbits.ON =1;
}
void Initialize_Timer3()
{
T3CONbits.TON = 0;
T3CONbits.TCKPS = 7;
PR3 = 0xFFFF;
TMR3 = 0;
T3CONbits.ON =1;
}
void Initialize_Timer4()
{
T4CON = 0x0;
T4CONSET = 0x0070;
PR4 = 0xFFFF;
TMR4 = 0;
T4CONSET = 0x8000;
}
void Interrupts_enable()
{
// Timer one interrupt is enabled when it enters the Path search mode
IPC1bits.T1IP = 7;
IFS0bits.T1IF = 0;
IEC0bits.T1IE = 0;
// Interrupt for IC2
IFS0bits.IC2IF =0;
IPC2bits.IC2IP = 7;
IEC0bits.IC2IE = 1;
//// Interrupt for IC3
IFS0bits.IC3IF =0;
IPC3bits.IC3IP = 7;
IEC0bits.IC3IE = 1;
//turn ON IC2 & IC3
IC2CONbits.ON = 1;
IC3CONbits.ON= 1;
//Interrupt for Timer1
IFS0bits.T1IF =0;
IPC1bits.T1IP = 7;
IEC0bits.T1IE = 1;
INTCONbits.MVEC = 1;
__builtin_enable_interrupts();
}
void Motor_Forward()
{
LATDbits.LATD6 = 1;
LATDbits.LATD7 = 0;
OC2RS = 800;
OC3RS = 800;
}
void Stop_()
{
OC2RS = 0;
OC3RS = 0;
}
void centerRight()
{
LATDbits.LATD6 = 1;
LATDbits.LATD7 = 0;
OC2RS = 700; //OC for right motor
OC3RS = 400; //OC for left motor
}
void turnRight()
{
LATDbits.LATD6 = 0;
LATDbits.LATD7 = 0;
OC2RS = 800; //Right motor speed
OC3RS = 800; //Left motor speed
}
void centerLeft()
{
LATDbits.LATD6 = 1;
LATDbits.LATD7 = 0;
OC2RS = 400;
OC3RS = 700;
}
void Turn_Left()
{
LATDbits.LATD6 = 1;
LATDbits.LATD7 = 1;
OC2RS = 800;
OC3RS = 800;
}
void Motor_Backward()
{
LATDbits.LATD6 = 0;
LATDbits.LATD7 = 1;
OC2RS = 800;
OC3RS = 800;
}
void __ISR (_TIMER_1_VECTOR, IPL7SOFT) T1Interrupt(void)
{
//Indexing Variable Declarations
int index = 0;
int rotate = 0;
if (Power == On)
{
if (mode == Stop)
{
if (PORTGbits.RG15 == 0 || PORTGbits.RG14 == 0 || PORTGbits.RG13 == 0 || PORTGbits.RG12 == 0) // Picks up a line
{
mode = Line_Track;
}
else if(PORTGbits.RG15 == 1 && PORTGbits.RG14 == 1 && PORTGbits.RG13 == 1 && PORTGbits.RG12 == 1) // No line found
{
mode = Line_Search;
}
}
else if (mode == Line_Track) //Found line
{
Motor_Forward(); //By default move forward
if (PORTGbits.RG12 == 0 && PORTGbits.RG14 == 1 && PORTGbits.RG15 == 1) //
{
Turn_Left();
}
else if (PORTGbits.RG15 == 0 && PORTGbits.RG12 == 1 && PORTGbits.RG13 == 1)
{
turnRight();
}
else if (PORTGbits.RG13 == 0 && PORTGbits.RG12 == 1 && PORTGbits.RG14 == 1 && PORTGbits.RG15 == 1)
{
centerLeft();
}
else if (PORTGbits.RG14 == 0 && PORTGbits.RG12 == 1 && PORTGbits.RG13 == 1 && PORTGbits.RG15 == 1)
{
centerRight();
}
else if (PORTGbits.RG15 == 0 && PORTGbits.RG14 == 0 && PORTGbits.RG13 == 0 && PORTGbits.RG12 == 1)
{
turnRight();
}
else if (PORTGbits.RG12 == 0 && PORTGbits.RG13 == 0 && PORTGbits.RG14 == 0 && PORTGbits.RG15 == 1)
{
Turn_Left();
}
else if (PORTGbits.RG12 == 0 && PORTGbits.RG13 == 0 && PORTGbits.RG14 == 0 && PORTGbits.RG15 == 0)
{
TMR4 = 0;
index = 0;
while (index < 2)
{
while (TMR4 < 39063) //Second Calculation used from Project 1
{
Turn_Left();
}
index++;
}
TMR4 = 0;
index = 0;
while (index < 2)
{
while (TMR4 < 39063)
{
centerLeft();
}
index++;
}
}
//Goes off line
else if (PORTCbits.RC12 == 1 && PORTCbits.RC13 == 1 && PORTCbits.RC14 == 1 && PORTCbits.RC15 == 1)
{
mode = Line_Search;
}
}
else if (mode == Line_Search)
{
index = 0;
rotate = 0;
while (rotate < 4)
{
//Moves forward for 3 seconds to abide by project requirements
index = 0;
while (index < 10)
{
TMR4 = 0;
while (TMR4 < 39063)
{
if (PORTGbits.RG12 == 0 || PORTGbits.RG13 == 0 || PORTGbits.RG14 == 0 || PORTGbits.RG15 == 0)
{
mode = Line_Track;
goto end;
}
if (PORTAbits.RA7 == 1)
{
mode = Stop;
Power = Off;
goto end;
}
mode2 = Forward;
Motor_Forward();
}
index++;
}
//Backwards Distance = Forwards Difference
index = 0;
while (index < 10)
{
TMR4 = 0;
while (TMR4 < 39063)
{
if (PORTGbits.RG12 == 0 || PORTGbits.RG13 == 0 || PORTGbits.RG14 == 0 || PORTGbits.RG15 == 0)
{
mode = Line_Track;
goto end;
}
if (PORTAbits.RA7 == 1)
{
mode = Stop;
Power = Off;
goto end;
}
mode2 = Backward;
Motor_Backward();
}
index++;
}
//Rotates 90 Degrees
index = 0;
while (index < 5)
{
TMR4 = 0;
while (TMR4 < 9766)
{
if (PORTGbits.RG12 == 0 || PORTGbits.RG13 == 0 || PORTGbits.RG14 == 0 || PORTGbits.RG15 == 0)
{
mode = Line_Track;
goto end;
}
if (PORTAbits.RA7 == 1)
{
mode = Stop;
Power = Off;
goto end;
}
mode2 = TurnLeft;
Turn_Left();
}
index++;
}
rotate++;
}
if (rotate == 4)
{
Power = Off;
}
end:;
}
}
else if (Power == Off)
{
mode = Stop;
Stop_();
}
IFS0bits.T1IF = 0;
}
This will solve the issue for sure.
Do comment if there is any query. Thank you. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.