help me with code!

Altron

Well-Known Member
this code is in C2C++, which is, afaik, a mix of C and C++
i was wondering if the if/else statements were nested properly, and why there's an error in one of them.

#include<system.h>
#include<PICinit.h>

void motorsoff();
void forward();
void reverse();
void left();
void right();
void interrupt();

main()
{
configurePIC();
enable_interrupt(INTE);
enable_interrupt(GIE);
motorspeed(1,100);
motorspeed(2,75);
int home, floorsensor, nav, leftsensor, rightsensor;
floorsensor = read_adc(0);
nav = read_adc(1);
leftsensor = read_adc(2);
rightsensor = read_adc(3);
if(floorsensor<=27500) //if floor is white
{
home=1;
}
else //if floor is black
{
home=0;
}
while(1) //the main loop
{
floorsensor = read_adc(0);
if(floorsensor<=27500) //the robot is on white
{
if(home=1) //home white complier says there's an error in this line
{
output_low('E', 2);
output_high('A', 4);
if(read_adc(1)<= 27500) //if top sensor is bright, go forward
{
forward();
delay_s(1);
}
else //if top sensor is dark, spin
{
left();
delay_ms(250);
delay_ms(250);
forward();
delay_s(1);
}
}
else //home black
{
output_low('A', 4);
output_high('E', 2);
if(read_adc(2)<=27500) //if left is bright
{
if(read_adc(3)<=27500) //towards light
{
forward();
delay_s(1);
}
else //light to left
{
left();
delay_ms(250);
delay_ms(250);
forward();
delay_s(1);
}
}
else //if left is dark
{
if(read_adc(3)<=27500) //light to the right
{
right();
delay_ms(250);
delay_ms(250);
forward();
delay_s(1);
}
else //light behind
{
left();
delay_ms(250);
delay_ms(250);
forward();
delay_s(1);
}
}
}
}
else //robot on black
{
if(home=0; //home black
{
output_low('E', 2);
output_high('A', 4);
if(read_adc(1)<= 27500) //if top sensor is bright, go forward
{
forward();
delay_s(1);
}
else //if top sensor is dark, spin
{
left();
delay_ms(250);
delay_ms(250);
forward();
delay_s(1);
}
}
else //home white
{
output_low('A', 4);
output_high('E', 2);
if(read_adc(2)<=27500) //if left is bright
{
if(read_adc(3)<=27500) //towards light
{
forward();
delay_s(1);
}
else //light to left
{
left();
delay_ms(250);
delay_ms(250);
forward();
delay_s(1);
}
}
else() //if left is dark
{
if(read_adc(3)<=27500) //light to the right
{
right();
delay_ms(250);
delay_ms(250);
forward();
delay_s(1);
}
else //light behind
{
left();
delay_ms(250);
delay_ms(250);
forward();
delay_s(1);
}
}
}
}
}
}

void motorsoff()
{
output_low('C',0);
output_low('C',5);
output_low('C',3);
output_low('C',4);
}

void forward()
{
output_high('C',0);
output_low('C',5);
output_high('C',3);
output_low('C',4);
}


void reverse()
{
output_low('C',0);
output_high('C',5);
output_low('C',3);
output_high('C',4);
}


void left()
{
output_low('C',0);
output_high('C',5);
output_high('C',3);
output_low('C',4);
}


void right()
{
output_high('C',0);
output_low('C',5);
output_low('C',3);
output_high('C',4);
}

void interrupt()
{
save_data();

if(read_input ('E',0)==0) //left bumper
{
reverse();
delay_ms(250);
delay_ms(250);
right();
delay_ms(250);
delay_ms(250);
motorsoff();
}

if(read_input('E',1)==0) //right bumper
{
reverse();
delay_ms(250);
delay_ms(250);
left();
delay_ms(250);
delay_ms(250);
motorsoff();
}


if ((read_input ('E',0)==1) && (read_input ('E',1)==1))
{
clear_bit (INTCON, INTF);
}
restore_data();
return;
}
 
ok, my problem was that I can't use <= or >= with a read_adc, it has to be < or >
and in the highlighted line, i had to have (home==1) because it's comparing it, not setting it.
 
Comments starting with /********
are made by me.

Code:
#include<system.h>
#include<PICinit.h>

void motorsoff();
void forward();
void reverse();
void left();
void right();
void interrupt();

main()
{
	configurePIC();
	enable_interrupt(INTE);
	enable_interrupt(GIE);
	motorspeed(1,100);
	motorspeed(2,75);
	int home, floorsensor, nav, leftsensor, rightsensor; 
	floorsensor = read_adc(0);
	nav = read_adc(1);
	leftsensor = read_adc(2);
	rightsensor = read_adc(3);
	if(floorsensor<=27500) //if floor is white
	{
		home=1;
	}
	else //if floor is black
	{
		home=0;
	}
	while(1) //the main loop
	{
		floorsensor = read_adc(0);
		if(floorsensor<=27500) //the robot is on white
		{
			if(home=1) //home  /************* This shouldn't issue an error, just a warning, but you should change it to == */
			{
				output_low('E', 2);
				output_high('A', 4);
				if(read_adc(1)<= 27500) //if top sensor is bright, go forward 
				{
					forward();
					delay_s(1);
				}
				else //if top sensor is dark, spin
				{
					left();
					delay_ms(250);
					delay_ms(250);
					forward();
					delay_s(1);
				}
			}
			else //home black
			{
				output_low('A', 4);
				output_high('E', 2);
				if(read_adc(2)<=27500) //if left is bright
				{
					if(read_adc(3)<=27500) //towards light
					{
						forward();
						delay_s(1);
					}
					else //light to left
					{
						left();
						delay_ms(250);
						delay_ms(250);
						forward();
						delay_s(1);
					}
				}
				else //if left is dark
				{
					if(read_adc(3)<=27500) //light to the right
					{
						right();
						delay_ms(250);
						delay_ms(250);
						forward();
						delay_s(1);
					}
					else //light behind
					{
						left();
						delay_ms(250);
						delay_ms(250);
						forward();
						delay_s(1);
					}
				}
			}
		}
		else //robot on black
		{
			if(home=0; //home black   /******** THIS IS AN ERROR */
			{
				output_low('E', 2);
				output_high('A', 4);
				if(read_adc(1)<= 27500) //if top sensor is bright, go forward 
				{
					forward();
					delay_s(1);
				}
				else //if top sensor is dark, spin
				{
					left();
					delay_ms(250);
					delay_ms(250);
					forward();
					delay_s(1);
				}
			}
			else //home white
			{
				output_low('A', 4);
				output_high('E', 2);
				if(read_adc(2)<=27500) //if left is bright
				{
					if(read_adc(3)<=27500) //towards light
					{
						forward();
						delay_s(1);
					}
					else //light to left
					{
						left();
						delay_ms(250);
						delay_ms(250);
						forward();
						delay_s(1);
					}
				}
				else() //if left is dark  /******** else is not a function, should write it as else not else() */
				{
					if(read_adc(3)<=27500) //light to the right
					{
						right();
						delay_ms(250);
						delay_ms(250);
						forward();
						delay_s(1);
					}
					else //light behind
					{
						left();
						delay_ms(250);
						delay_ms(250);
						forward();
						delay_s(1);
					}
				}
			}
		}	
	}
}	

void motorsoff()
{
output_low('C',0);
output_low('C',5);
output_low('C',3);
output_low('C',4);
}

void forward()
{
output_high('C',0);
output_low('C',5);
output_high('C',3);
output_low('C',4);
}


void reverse()
{
output_low('C',0);
output_high('C',5);
output_low('C',3);
output_high('C',4);
}


void left()
{
output_low('C',0);
output_high('C',5);
output_high('C',3);	
output_low('C',4);
}


void right()
{
output_high('C',0);
output_low('C',5);
output_low('C',3);
output_high('C',4);
}

void interrupt()
{
            save_data();
            
			if(read_input ('E',0)==0) //left bumper
			{	
				reverse();
				delay_ms(250);
				delay_ms(250);
				right();
				delay_ms(250);
				delay_ms(250);
				motorsoff();
			}	

			if(read_input('E',1)==0) //right bumper
			{
				reverse();
				delay_ms(250);
				delay_ms(250);
				left();
				delay_ms(250);
				delay_ms(250);
				motorsoff();
			}


            if ((read_input ('E',0)==1) && (read_input ('E',1)==1))
            {
                        clear_bit (INTCON, INTF);
            }
            restore_data();
            return;   /******** Why use a return statement at the end of a void function ? */
}

Next time use the code tags ;)

I'm glad that you indent, except on the direction functons.
 
I'm not sure what you mean. :confuse3:

The only point of using return statements inside a void function is to get out of the function on certain conditions. There's no point in putting the call at the end of the function since: it isn't returning a value (it is void) and the function will end anyway.
 
yeah, after going through it a few times, I found the errors and removed them and it ran. thanks luis!

only one more assignment, and then I'm done with C++ forever!
 
yeah, after going through it a few times, I found the errors and removed them and it ran. thanks luis!

only one more assignment, and then I'm done with C++ forever!

Now there is a little lesson you should learn, a correct program is not always the correct program.
 
Back
Top