PDA

View Full Version : need help with if, else if & else.



voodoo
06-23-2014, 09:31 PM
I have solved the problem myself but please read & you can see what worked incase anyone has the same sort of problem.

hi forum, i have a robot that i put on a showcase thread "Irowe 4550" i have been improving on the software it for the last few weeks. i have been trying to make him detect if he blocked in\cornered so i wrote this void routine. It works but steps through a mode one at a time. does whats it meant to do but i need it to go set the iMode_6 back to zero if its false, ie at the moment when it detects 260 from the ir sensors it steps a iMode_6 up so it drives abound then goes into the blocked switch i wrote. However the object of this void routine is to only step up if the sensors are 260 for the specified period of time with the the sw timer "CheckRoboTimer" i wrote.

all i am confused about is where to put else i mode_6 = 0; so if the Ir sensor drops below 260 in the specified time it resets iMode_6 back to 0. also can i have changed the "else if" to "if" & it works fine.

please any advice would help me!!!


int iMode|_6 = 0;
float iSpin_Timer;

void Cornered()
{
if(iMode_6 == 0)
{
if(iHighestSensorDistance >= 260)
{
iSpin_Timer = 0.5;
iMode_6 = 1;
}
}
//*******************************
else if(iMode_6 == 1)
{
if(iHighestSensorDistance >= 260)
{
if (CheckRoboTimer(&iSpin_Timer))

{
iSpin_Timer = 0.5;
iMode_6 = 2;
}
}
}
//*******************************
else if(iMode_6 == 2)
{
if(iHighestSensorDistance >= 260)
{
if (CheckRoboTimer(&iSpin_Timer))

{
iSpin_Timer = 0.5;
iMode_6 = 3;
}
}
}
//*******************************
else if(iMode_6 == 3)
{
if(iHighestSensorDistance >= 260) //280
{
if (CheckRoboTimer(&iSpin_Timer))
{
Mode = BLOCKED_IN;
iMode_6 = 0;
}
}
}
}

************************************************** *************************

what worked properly:

void Cornered() //try else to set to imode 6 = 0
{
if(iMode_6 == 0)
{
if(iHighestSensorDistance >= 260)
{
iSpin_Timer = 0.5;
iMode_6 = 1;
}
}
//*******************************
if(iMode_6 == 1)
{
if(iHighestSensorDistance >= 260)
{
if (CheckRoboTimer(&iSpin_Timer))

{
iSpin_Timer = 0.5;
iMode_6 = 2;
}
}
else
iMode = 0;
}
//*******************************
if(iMode_6 == 2)
{
if(iHighestSensorDistance >= 260)
{
if (CheckRoboTimer(&iSpin_Timer))

{
iSpin_Timer = 0.5;
iMode_6 = 3;
}
}
else iMode_6 = 0;
}
//*******************************
if(iMode_6 == 3)
{
if(iHighestSensorDistance >= 260) //280
{
if (CheckRoboTimer(&iSpin_Timer))
{
Mode = BLOCKED_IN;
iMode_6 = 0;
}
}
else
iMode_6 = 0;
}
}