And Statments Am I missing somthing

Can some one see what is wrong with this code I am not sure I am using the if and the and statements correct. The code uploads but does not do what I think it should be doing. The locks do not seem to be working and I was wondering if it was because I stacked to may && statements. Or because I left out the {}

if (angle == direct) {left = 0;
right = 0;}
if (left == 0 && right == 0 && angle < direct ) right = 1 ; //Turn right lock

if (right == 0 && left == 0 && angle > direct) left = 1 ; // Turn left lock

if (right == 1 && angle < direct && angle > 0 && angle < direct - 5 ) digitalWrite(11, HIGH);

if (right == 1 && angle > direct + 180) digitalWrite(11, HIGH) ;

if (left == 1 && angle > direct && angle < 359 && angle > direct + 5) digitalWrite(12, HIGH);

if (left == 1 && angle < direct - 180) digitalWrite(12, HIGH) ;

if (angle==direct) { digitalWrite(11, LOW);
digitalWrite(12, LOW);}

Thank Ron


if (right == 1 && angle > (direct + 180)) digitalWrite(11, HIGH) ;


if (right  == 1 && angle < direct && angle > 0  && angle < direct - 5 )  digitalWrite(11, HIGH);

There seems to be a redundant clause in this if statement, and another similar one further down, apart from that it looks valid to me.

The extra parentheses suggested by LarryD should not actually be required, because the conditional expression will be evaluated that way anyhow, with the extra parentheses.

Is this supposed to be steering some kind of robot ?

It seems to me, you are going to run into difficulties if the target direction is close to 0

I find it easier to understand that sort of logic if I break the IFs into a cascade - something like this

if (right == 0) {
  if (left == 0) {
     if (angle > direct) {
     else {

I find it very easy to miss a relevant condition when I have a compound IF statement.


Thanks for the input . I do like the cascading if's better.

There is a problem when steering near 0 . Any thoughts on a better way to steer using a compass locked to a heading. I have been trying to com up with some sort of auto pilot using the compass to control direction.


Piolt34: There is a problem when steering near 0 .

You don't say what the problem is.

It would be normal to have a small dead-band in which no action is intended to take place to prevent hunting.


If you are dealing with angles and want to avoid turning the long way round then:

1) calculate the relative angle (target - actual). 2) normalise to the range -180 .. +180 ( or -PI .. +PI if in radians). 3) the sign of the result tells you which way to turn, the magnitude how far.

In the code you post the variables left and right are outputs of the early tests, then inputs to the later ones. I think that's confusing, it worries me.

Comment each variable with its actual meaning and then compare to each use - is each used consistently?

There are many problems I am having and just needed to verify that the code I am using was formated correctly . The overall problem I am trying to over come is locking the direction (direct) and having the out put go high when the angle is greater then the dead band. The biggest issue is the transition from 0-359 0r 359 -0 deg. The greater than less than option works fair but it is the only way I can think of doing this. I am sure there is a better way to steer to a direction and correct if it deviates from the direction . Of coarse it need to correct in the right direction and not make 360 deg turns.

Below is the code I have been working on to turn on a relay to correct the direction of flight.
The problem that seems to keep happening is the right and left variable goes to zero every time the
angle goes past 0 even if the direct variable is 180. Seems to me the if stament is not working
correctly. I have tried it with {} and with out. I have added 1 to the angle so that it would never see
0 degrees none of these things work.

#include <Wire.h>
#include <HMC5883L.h>
HMC5883L compass;

float declinationAngle = 0.212; // Radians
int direct ; // direction you lock on too Rn
int error ;
int off ;
int off2 ;
int angleneg;
int start;

void setup() {
start = 1;
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
compass = HMC5883L();
// Set scale to +/- 1.3 Ga
int error = compass.SetScale(1.3);
if (error != 0)
// Set measurement mode to continous
error = compass.SetMeasurementMode(Measurement_Continuous);
if (error != 0)

void loop() {
int angle = getDegrees()+1;
int center;
int angle_adj ;
int correction;
const int buttonPin = 2; // the number of the pushbutton pin
int left ;
int right ;

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {direct = angle ; //get angle and holds on too it
start = 0;}

if (angle == direct){ left = 0; // Lock the direction of the output
right = 0;}

if (left == 0 && right == 0 && angle < direct && start == 0 ) right = 1 ; //Turn right lock

if (right == 0 && left == 0 && angle > direct && start == 0 ) left = 1 ; // Turn left lock

if (left == 1){ digitalWrite(12, HIGH);
digitalWrite(11, LOW);}
if (right == 1 ) { digitalWrite(11, HIGH);
digitalWrite(12, LOW);}

That code doesn't look like it will compile. It also hasn't been posted properly. Read this and then fix up your post: How to post code properly


Code does compile. I left some of it out to prevent confusion.

Code does compile. I left some of it out to prevent create confusion.

Fixed that for you.

I count 8 if statements. Which one do you think isn't working?

At the very least you have some stuff in loop() that should be in setup(). Generally things like pinMode() are used in setup().

You may want to put some Serial prints in there to see what is happening to your variables. If the button is pressed you set direct = angle and right after that you check if they are equal, resulting in left and right becoming 0.

You asked this stupid question, last week.

Did you understand the answers you got then, or are you just trolling ?

NO I did not ask this Stupid question last week ! The question is on the the same project but this question is more specific in nature . IF you do not have a answer for me than just move on .Or do you just need to belittle people to feel good?

I did get the my previous questioned answered ....I do not see how this applies to the problem I am having now. Why would the If statement allow for the angle==direct when the compass passes 0 ( int angle = getDegrees()+1;) . Direct is not 0 so the if statement is not true there for the left right should not be set to 0. But it does every time I rotate the compass past 0 Degrees.

You asked this stupid question, last week.

What question you talking about. Probably best to add links to vague references.