# multiple if statements to drive motors based on readings from a magnetomer

Ive managed to get the serial data from my magnetometer saved into a variable, now im trying to use those values in if statements to have my motors do different things based on the readings, However the code seems to be getting stuck in a loop. Heres my code

``````void loop()
{

//int  Lidar = myLidarLite.distance();

pulseWidth = pulseIn(9, HIGH); // Count how long the pulse is high in microseconds
// Serial.println();

int Lidar = pulseWidth/10;
Serial.println(Lidar);

while(Serial.available() )
{

//  driveArdumoto(MOTOR_A, REVERSE, 255);
//      driveArdumoto(MOTOR_B, FORWARD, 255);

{
if ( (Lidar > 50 ))
{

driveArdumoto(MOTOR_A, REVERSE, 255);
driveArdumoto(MOTOR_B, FORWARD, 255);
}
if (Lidar <= 50)                     //Object detected on left side of patricia
{

driveArdumoto(MOTOR_A, FORWARD, 255);
driveArdumoto(MOTOR_B, REVERSE, 255);
}

}

{
Serial.print("shit we off course, baby") ;
stopArdumoto(MOTOR_A);  // STOP motor B
stopArdumoto(MOTOR_B);
}

}

}
``````

any ideas where im going wrong?

``````    if (getHeading() >= 340 && getHeading() <=  10)
``````

Can getHeading() be greater than 340 and less than 10 ?

getheading() can only be values from 0 to 360

helm2762:
getheading() can only be values from 0 to 360

``````   if (getHeading() >= 340 && getHeading() <=  10)
``````

“if getHeading is greater than or equal to 340 and getHeading() is smaller than or equal to 10”

360 ?
No

180 ?
No

0 ?
No

Im so glad you took the time to offer 0 solutions even when you couldve just said " You should be using in or statement". you truly are the hero this forum needs

helm2762:
Im so glad you took the time to offer 0 solutions even when you couldve just said " You should be using in or statement". you truly are the hero this forum needs

But UKHeliBob gave you the solution. You just failed to see it.

&& means AND.
You're asking if both values are true at the same time.

You probably mean OR.
https://www.arduino.cc/reference/en/language/structure/boolean-operators/logicalor/
Leo..

im trying to get motors to do various things given different values coming in off the serial monitor, however i can only seem to get one else if statement to run. the second one is completely ignored, This is what it looks like so far

``````int heading = getHeading();
{

//Serial.print("we gucci mang");
// Serial.println(Lidar);0
Forward() ;

}

{
Serial.println("stuff") ;
stopArdumoto(MOTOR_A);  // STOP motor B
stopArdumoto(MOTOR_B);
delay(2000);
}

{
Serial.println("Other stuff") ;
stopArdumoto(MOTOR_A);
driveArdumoto(MOTOR_B, REVERSE, 255);

}

}
``````

any ideas where i may be going wrong with my logic?

print out the value of heading and see if it satisfies the other two else if statements.

I am not understanding what is your problem.

There is a problem with your code snippet.
You have an extraneous }

A quick glance at your code tells me there is NO reason for you to haven ANY "else" in there. Just make your tests stand by themselves.

Paul

After reading the heading, print it out to see what you really get:

``````int heading = getHeading();
``````

The snippet does look sound to me other than that stray } all the way in the end; but do fix your indentation (CTRL-T in the IDE) to make it a lot more readable, and help you find out what belongs to what much easier.

@helm2762, do not cross-post. Threads merged.

helm2762:
Im so glad you took the time to offer 0 solutions even when you couldve just said " You should be using in or statement". you truly are the hero this forum needs

I note with interest that you have deleted your condescending post where you told me to "read reply #3 very carefully" and told me that "maths was fun".

Hi,