Maze Solver code with Mickro C

Hello guys, i m here to ask for help with the development of a code for a maze solver robot.
I have about 50 hours of microcontrolers and was given this project by the teacher. He gave me part of the code and wants me to fill in the gaps. I have no clue where or how to start, and would appreciate guide lines pointers or even basic code examples for a functional 6 sensors maze solver that i can guide myself from, and present to the teacher something that woks, even if its really really basic code,.
As programmers i think you guys can all agree that 50 hours is in no way enough to do something like this so i ask for a litle of understanding.
The main focus of my course is automation and industrial control not programming, i m counting on persuing a course on C (hopefully with strong bases) but for now i need to get pass this problem.
Bellow is the code i was given i relly hope someone can help, thanks in advance. some works are in portuguese sorry for that.

//-----------LCD-----------------------
sbit LCD_RS at RC7_bit;
sbit LCD_EN at RC6_bit;
sbit LCD_D4 at RB7_bit;
sbit LCD_D5 at RD6_bit;
sbit LCD_D6 at RD4_bit;
sbit LCD_D7 at RD3_bit;
sbit LCD_RS_Direction at TRISC7_bit;
sbit LCD_EN_Direction at TRISC6_bit;
sbit LCD_D4_Direction at TRISB7_bit;
sbit LCD_D5_Direction at TRISD6_bit;
sbit LCD_D6_Direction at TRISD4_bit;
sbit LCD_D7_Direction at TRISD3_bit;
//-----------MOTORS--------------------
sbit MOTORL_B at RA6_bit;
sbit MOTORL_A at RA7_bit;
sbit MOTORR_A at RD0_bit;
sbit MOTORR_B at RD7_bit;
//-----------IR Receivers--------------
sbit RCL at RB0_bit;
sbit RCR at RD2_bit;
//-------------Debug Led---------------
sbit LED at RB6_bit;
sbit Button at RA1_bit;

int pow(int base, int expoente){
float resultado=1;
while(expoente!=0){
resultado=resultado*base;
–expoente;
}
return resultado;
}

void Init_Hardware(void){
TRISA=0x3F; //00111111
TRISB=0x1F; //00011111
TRISC=0x31; //00110001
TRISD=0x5C; //01011100
TRISE=0x03; //00000011
ANSELA=0x0F;
ANSELB=0x1E;
ANSELC=0x00;
ANSELD=0x00;
ANSELE=0x03;
//OSCCON=0x76;
//OSCTUNE=0xDF;
OSCCON=0b01110110;
OSCCON2.PLLRDY=0;
OSCTUNE.PLLEN=0;
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
PWM4_Init(980);
PWM5_Init(980);
PWM4_start();
PWM5_start();
}

void drive(int SpeedL, int SpeedR){
if(speedL>100) speedL=100;
if(speedR>100) speedR=100;
if(speedL>0){
MOTORL_B=1;
MOTORL_A=0;
}
else{
speedL=speedL*(-1);
MOTORL_B=0;
MOTORL_A=1;
}
if(speedR>0){
MOTORR_A=1;
MOTORR_B=0;
}
else{
speedR=speedR*(-1);
MOTORR_A=0;
MOTORR_B=1;
}
if(speedL==0){
MOTORL_B=0;
MOTORL_A=0;
PWM5_Set_Duty(0);
}
else PWM5_Set_Duty(speedL+155);
if(speedR==0){
MOTORR_A=1;
MOTORR_B=0;
PWM4_Set_Duty(0);
}
else PWM4_Set_Duty(speedR+155);
}

char keyboard(void){
int key_voltage;
key_voltage=ADC_read(1);
if(key_voltage>=900) return 0; // none
if(key_voltage>=600) return 3; // key3
if(key_voltage>=450) return 2; // key2
return 1; // key1
}

void automatic_start(void){
int contador=0;
do{
if((RCR&RCL)==1){
++contador;
Delay_ms(1);
}
else contador=0;
}while(contador<200);
}

void main(void) {
char ledStatus=0;
char flag=0;
Init_Hardware();
while(1){
MOTORL_B=0;
MOTORL_A=0;
MOTORR_B=0;
MOTORR_A=0;
PWM4_Set_Duty(190);
PWM5_Set_Duty(190);
}
}

Hello @Papatintos

I’m not sure if you’ve been given 50 hours to do the assignment or you’ve been learning to program for 50 hours, so I don’t know if this answer is on time haha. Anyways, I guess it’s better late than ever.

I haven’t worked with MikroC but I have programmed microcontrollers in C so I might be able to help you out. For what I can see the robot has two motors (for the wheels), two IR sensors (for measuring distance), and LCD (for displaying), and a LED (for debugging). I guess those are the 6 sensors you’re talking about.

The first part of the code is for the module connections, the rest of the code is function definitions you can use on the main function. You can notice they’re functions because they start with the type of variable they return (void, int, char, …) the next is the name of the function (pow, Init_Hardware, drive, …) and what is between parentheses are the input variables that the function uses.

Pow - calculates the power of a number, for example, if you need to compute 6^4 you would write int a = pow(6,4); in the main

Init_Hardware - initializes the hardware, it configures the pins as digital or analog, and the ports as inputs or outputs, initializes the LCD and the PWM modules that control the speed. This function does not return or need any variables so you use it as it is shown in the main.

Drive - allows you to control the motor and their speed using Pulse Width Modulation (PWM) you only need to provide the speed you want for the left and right wheels.

Keyboard - reads the voltage of the keyboard and return a value (0/1/2/3) according to it. I guess this is for controlling the direction of the movement (forward/backward/left/right). I’m not sure of this though.

Automatic_start - uses the information of the left and right IR receivers to check if there is a wall to either side of the robot, if there is a wall continues checking and if there isn’t a wall for more than 200ms it gets out of the function.

Finally, there is the main function which is where you execute the whole program, here you use all the other functions you defined before in order to perform the task you want, in this case reading the information provided by the IR sensors and according to this controlling the robot to solve the maze.

As your main problem with this assignment is the programming part you can start by thinking what actions are necessary for the robot to solve the maze, and check how the functions provided can help you. If you need help with programming them you can ask again, and I’ll do my best to assist you.

Also I think some examples of MrikroC programming could help you a lot. Check the ones provided by Mikroelektronika here https://www.mikroe.com/ebooks/pic-microcontrollers-programming-in-c. I suggest you reading examples 1, 6, 7 and 10.

As your course is about automation and control you will probably need to learn how to program or at least the basics, if you have no time to do a course right now it is ok. You can try to learn through a mobile app, I suggest you try SoloLearn where you can learn and practice on your phone when you have some time to spare.

I hope this info can help you and good luck with your assignment :smiley:

3 Likes