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