Hi all,
Found a solution in the end, unfortunately @Kerbolosh , it requires to be totally hands free (automated) so using an encoder is not possible. It seems 'If else' control structures are not as good at solving my problem. Instead I used 'do while' loops until the correct measurement was reached and 'if else' statements to check the measurement is within the required range.
Another addition was a change to ReadSerial(); void loop, it now reads the serial much much faster and is responding the measurement very fast.
I have another topic to search into, but is a little off topic for the title so I will post in another forum regarding that and create a link here later if anyone wants to follow a similar project to mine.
Until next post, here is the working code;
int enA = 9;
int in1 = 8;
int in2 = 7;
String sylvacRad;
float FloatRad;
const float Hthreshold = 25.2245;
const float HthresholdOS = 25.2255;
const float Lthreshold = 24.8853;
const float LthresholdOS = 24.8845;
void setup() {
Serial.begin(9600);
Serial.setTimeout(100);
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
ReadSerial();
}
void loop() {
do {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(enA, 255);
ReadSerial();
} while (FloatRad < Hthreshold); //MoveUp
if (FloatRad > HthresholdOS) {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(enA, 100);
ReadSerial();
} else if (FloatRad < HthresholdOS) {
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
analogWrite(enA, 0);
}
delay(50000); //delay for autoIT
do {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(enA, 255);
ReadSerial();
} while (FloatRad > Lthreshold); //MoveDown
if (FloatRad < LthresholdOS) {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(enA, 100);
ReadSerial();
} else if (FloatRad > LthresholdOS) {
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
analogWrite(enA, 0);
}
delay(50000); //delay for autoIT
}
void ReadSerial() {
if (Serial.available() > 0) {
sylvacRad = Serial.readString();
}
delayMicroseconds(2);
FloatRad = sylvacRad.toFloat();
Serial.println(FloatRad, 4);
}
thanks everyone for your help!