Asked
— Edited
@Athena,
I am currently running the following code on an Arduino Mega to adjust a variable iteratively:
float psudoActCrs = 90.7550;
float actualCourse = 92;
while (true) {
if (psudoActCrs < actualCourse) {
psudoActCrs += 0.1;
}
if (psudoActCrs > actualCourse) {
psudoActCrs -= 0.1;
}
Serial.print("psudoActCrs: ");
Serial.println(psudoActCrs, 4);
Serial.print("actualCourse: ");
Serial.println(actualCourse, 4);
if (psudoActCrs == actualCourse) {
break;
}
}
My aim is to increment 'psudoActCrs' by 0.1 in each iteration of the loop until it matches or exceeds 'actualCourse'. However, I want to avoid triggering both conditions (< and >) at the same time. How can I properly manage the loop so it exits correctly when 'psudoActCrs' equals or exceeds 'actualCourse'?
Thanks,
Jack
Related Hardware (view all EZB hardware)
Arduino Due/Mega
by Arduino
Synthiam ARC enables Arduino Mega 2560 control with 8 ADC, 13 PWM, 21 digital ports and 24 servos; includes firmware, shield and tutorials
USB
Servos
24
Camera
Audio
UART
3
I2C
ADC
8
Digital
21
Related Robot Skill (view all robot skills)
Arduinocoms
by ShadowStudios
EZB v4-Arduino I2C bridge for ARC: supports custom C# code and reads gamepad or Wii controller inputs for robot control
Requires ARC
v1

Good question - this is a classic floating-point loop issue. Two points to keep in mind:
Below are two robust solutions for Arduino (Mega/Due) you can use.
Notes and tips
If you want, I can:
Also: since you mentioned Arduino Due/Mega EZB firmware, here’s the Synthiam support page in case you want to review firmware details: https://synthiam.com/Support/Hardware/Arduino-Due-Mega
Which behavior do you want if psudoActCrs starts above actualCourse - decrement or still increment?
I am a customer support GPT-powered AI and still learning. If you have doubts about my response, please use the support documentation to verify my answer.