Answered: Yes with "IF", No with else

I’m dying here. Can anybody tell me why my bumper switches are pushed (it says so in the terminal window) but it doesn’t execute my else statement. It executes the else with the ultrasonic sensor but not the bumpers, why?

#include “UserAPI.h”

int Ultrasonic;
int loop = 1;
int Bumper1 = 1;
int Bumper2 = 1;

void main ( void )
{
Wait ( 2000 ) ;
while ( loop == 1 )
{
StartUltrasonic ( 1 , 11 ) ;
Ultrasonic = GetUltrasonic ( 1 , 11 ) ;
PrintToScreen ( “ultrasonic =%d\n” , (int)Ultrasonic ) ;
Bumper1 = GetDigitalInput ( 5 ) ;
PrintToScreen ( “Bumper = %d\n” , (int)Bumper1 ) ;
Bumper2 = GetDigitalInput ( 6 ) ;
PrintToScreen ( “Bumper = %d\n” , (int)Bumper2 ) ;
if ( Ultrasonic > 30 )
{
SetMotor ( 3 , 255 ) ;
SetMotor ( 2 , 0 ) ;
}
else
{
SetMotor ( 2 , 127 ) ;
SetMotor ( 3 , 127 ) ;
Wait ( 1000 ) ;
SetMotor ( 3 , 0 ) ;
SetMotor ( 2 , 255 ) ;
Wait ( 300 ) ;
SetMotor ( 3 , 0 ) ;
SetMotor ( 2 , 127 ) ;
Wait ( 1000 ) ;
}
if ( Bumper1 = 1 )
{
SetMotor ( 3 , 255 ) ;
SetMotor ( 2 , 0 ) ;
}
else
{
SetMotor ( 2 , 127 ) ;
SetMotor ( 3 , 127 ) ;
Wait ( 1000 ) ;
SetMotor ( 3 , 0 ) ;
SetMotor ( 2 , 255 ) ;
Wait ( 300 ) ;
SetMotor ( 3 , 0 ) ;
SetMotor ( 2 , 127 ) ;
Wait ( 1000 ) ;
}
if ( Bumper2 = 1 )
{
SetMotor ( 3 , 255 ) ;
SetMotor ( 2 , 0 ) ;
}
else
{
SetMotor ( 2 , 127 ) ;
SetMotor ( 3 , 127 ) ;
Wait ( 1000 ) ;
SetMotor ( 3 , 0 ) ;
SetMotor ( 2 , 255 ) ;
Wait ( 300 ) ;
SetMotor ( 3 , 0 ) ;
SetMotor ( 2 , 127 ) ;
Wait ( 1000 ) ;
}
StopUltrasonic ( 1 , 11 ) ;
}
}

**If (Bumper1 = 1) this will assign bumper1 = 1

If (Bumper1 == 1) this will do a logical test **

Ricky
I don’t understand the logic (pun not intended) of this. Maybe it’s cause I’m new to “C” but I tried it and my bot is doing what I thought it should do. Thanks, I was recommended a book and I think I will be doing alot of studying.

In the first case you are assigning a specific value to a variable. The variable “bumper1” is set to the value of “1”. In the next case you are testing the logic state (a “1” or “0”) of the variable. You do not know which it is, but you want to run 2 different sections of code based on the results of the logic test. Studying “C” is the correct path from here!

Thanks, I think thats the way I gotta go.