Problems with is statement

HI there,

I know that this may be really basic problem but I am out of options.
I have a if else statement for three buttons which control some relays the thing is that button and button2 are working fine all the time, but button three is working only one time. So if I press one time this button will work fine if I press it second time it dose not work. I have tried everything. Here is the code

  #include <Encoder.h>
#include <SoftwareSerial.h>

#define NOFIELD 508L    // Analog output with no applied field, calibrate this
#define TOMILLIGAUSS 3756L  // For A1302: 1.3mV = 1Gauss, and 1024 analog steps = 5V, so 1 step = 3756mG

Encoder knob(2, 3);
long positionShaft  = -999;
long newVal;
int relayPin = 5;
int relayPin2 = 4;
int buttonPin = 11;
int buttonPin2 = 9;
int buttonPin3 = 12;
int incomingByte = 0;
long gauss;

long DoMeasurement()
{
  int raw = analogRead(0); 
  long compensated = raw - NOFIELD;                 // adjust relative to no applied field 
  gauss = compensated * TOMILLIGAUSS / 1000;   // adjust scale to Gauss
  //if(gauss>=1800){
  //Serial.println(gauss);
  //Serial.println(raw);
  //}
}

void setup()
{
  Serial.begin(9600);
  pinMode(relayPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  pinMode(relayPin2, OUTPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
}

void loop()
{
  newVal = knob.read()/11.3777778/12;
  
  if (newVal != positionShaft ) {
      if(newVal>=360 || newVal<=-360){
      knob.write(0);
    }
    
    Serial.print(1,DEC);
    Serial.println(newVal,DEC);
    Serial.println();
    positionShaft = newVal;
  }
  if (Serial.available()) {
    Serial.read();
    Serial.println("Reset both knobs to zero");
    knob.write(0);
  }
  
  int button = digitalRead(buttonPin);
  int button2 = digitalRead(buttonPin2);
  int button3 = digitalRead(buttonPin3);

  if (button==HIGH && button2==LOW)
  {
    digitalWrite(relayPin,HIGH);
    digitalWrite(relayPin2,LOW);
  } 
  else if(button==LOW && button2 == HIGH)
  {
    digitalWrite(relayPin, HIGH);
    digitalWrite(relayPin2, HIGH);
  }
  else if (button3 == HIGH && gauss<1800)
  {
    do
    {
    DoMeasurement();
    Serial.println(button3);
    digitalWrite(relayPin, HIGH);
    digitalWrite(relayPin2, HIGH);
    }
    while(gauss<1800);
  }
  else if(button==LOW && button2==LOW && button3 == LOW)
  {
    digitalWrite(relayPin,LOW);
    digitalWrite(relayPin2,LOW);
  }
  delay(100);
}

Any ideas ?
And thank you in advance

The code is updated now

Post the full code, or at least a small subset that still compiles and demonstrates the problem.

Here is the code

Wrong. That is only some of the code.
Please post it all.

gauss isn't unless it's under 1800. Once it equals or is greater than 1800, the variable is never updated, therefore the condition will fail. The only reason it works now is because it's initialized to 0. If you were to initialize it to 2000, it wouldn't work the first time.

Don't be afraid to litter your code with Serial.print statements to debug it. Printing the value of guass would have given you a clue as to the problem.

Thank you very much,
The thing is that one day ago it was working but I changed somethings and than I forgot that I have delete the line to update th gauss.
Thank you again

Please post the working code for future reference - others might find your thread helpful

See also - Arduino Playground - Hall Effect -

While you are at it, fix the thread title. You are not having trouble with an is statement.