# Help with making if statement routine more efficient

**URL:** <https://forum.arduino.cc/t/help-with-making-if-statement-routine-more-efficient/1363808>\
**Category:** General Guidance\
**Created:** [March 15, 2025, 12:10am UTC](https://forum.arduino.cc/t/help-with-making-if-statement-routine-more-efficient/1363808 "2025-03-15T00:10:50Z")\
**Posts on this page:** 2\
**Page:** 2

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [March 16, 2025, 9:49am UTC](https://forum.arduino.cc/t/help-with-making-if-statement-routine-more-efficient/1363808/21 "2025-03-16T09:49:54Z")

</div>

> [@mbiasotti](#):
>
> I've tried out VarSpeedServo and also slowmotionservo libraries. Both are excellent but don't give me the ablity to interact with my Led fading function

I don't know why you say so, you probably did not try very hard 😉

Here is an example for you to play with.

The code uses two servos.  
In the setup() function I set them to their initial angle and then give an order to move at a slow speed to a desired position (`targetX` and `targetY`). On purpose I set a target and speed that are different. The slow speed is to give us time to see what's going on.

The loop tracks the max distance to go thanks to a function that reads the current servos' position compared to their respective target and returns the max absolute distance left to go ➜ this is what you would use to drive your led brightness. In my code I just print the distance to go to the Serial monitor every time it changes.

You can test that here.

> **[VarSpeedServo Demo - Wokwi ESP32, STM32, Arduino Simulator](https://wokwi.com/projects/425574342778534913)**
>
> Run IoT and embedded projects in your browser: ESP32, STM32, Arduino, Pi Pico, and more. No installation required!

The Serial monitor will show

```plaintext
HOMING
setting new destination
Max Distance to go : 180
Max Distance to go : 179
Max Distance to go : 178
Max Distance to go : 177
...
Max Distance to go : 3
Max Distance to go : 2
Max Distance to go : 1
Max Distance to go : 0
Both Servo reached their respective target position.

```

If you were to add as parameters the starting positions, given you have the current positions and the targets, it would be pretty simple to normalise the output of the function so that you get a value say between 0 and 255 where you get 0 when you are at the max distance and 255 when the move is completed or your led brightness curve directly.

* * *

> **click to see the code**
>
> ```arduino
> /* ============================================
> code is placed under the MIT license
> Copyright (c) 2025 J-M-L
> For the Arduino Forum : https://forum.arduino.cc/u/j-m-l
> Discussion : https://forum.arduino.cc/t/help-with-making-if-statement-routine-more-efficient/1363808$0
> 
> Permission is hereby granted, free of charge, to any person obtaining a copy
> of this software and associated documentation files (the "Software"), to deal
> in the Software without restriction, including without limitation the rights
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
> copies of the Software, and to permit persons to whom the Software is
> furnished to do so, subject to the following conditions:
> 
> The above copyright notice and this permission notice shall be included in
> all copies or substantial portions of the Software.
> 
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
> THE SOFTWARE.
> ===============================================
> */
> 
> #include "VarSpeedServo.h" // https://github.com/netlabtoolkit/VarSpeedServo$0
> 
> const byte servoXPin = 9;
> const byte servoYPin = 10;
> VarSpeedServo servoX;
> VarSpeedServo servoY;
> 
> int targetX, targetY;
> 
> int maxDistanceToDestination(int targetX, int targetY) {
> int dX = servoX.read() - targetX; dX = abs(dX);
> int dY = servoY.read() - targetY; dY = abs(dY);
> return max(dX, dY);
> }
> 
> void trackMovement(int targetX, int targetY) {
> static int previousDistance = -1;
> int currentDistance = maxDistanceToDestination(targetX, targetY);
> if (previousDistance != currentDistance) {
> previousDistance = currentDistance;
> Serial.print(F("Max Distance to go : ")); Serial.println(currentDistance);
> if (currentDistance == 0) {
> Serial.println(F("Both Servo reached their respective target position."));
> }
> }
> }
> 
> void setup() {
> servoX.attach(servoXPin);
> servoY.attach(servoYPin);
> 
> Serial.begin(115200);
> Serial.println(F("\nHOMING"));
> 
> // set the intial position of the servo, as fast as possible, wait until done
> servoX.write( 0, 255, true); // write(value, speed, wait)
> servoY.write(20, 255, true); // write(value, speed, wait)
> 
> delay(2000); // small pause to see them at their position
> 
> Serial.println(F("setting new destination"));
> // move the servo to a target position, slow speed so that we can see what's happening, run in background
> targetX = 180;
> targetY = 150;
> servoX.write(targetX, 20, false);
> servoY.write(targetY, 40, false);
> }
> 
> void loop() {
> trackMovement(targetX, targetY);
> }
> 
> /* some of the VarSpeedServo API
> 
> attach(pin ) - Attaches a servo motor to an i/o pin.
> attach(pin, min, max ) - Attaches to a pin setting min and max values in microseconds, default min is 544, max is 2400
> attached() - Returns true if there is a servo attached.
> detach() - Stops an attached servos from pulsing its i/o pin.
> 
> write(value) - Sets the servo angle in degrees. (invalid angle that is valid as pulse in microseconds is treated as microseconds)
> write(value, speed) - speed varies the speed of the move to new position 0=full speed, 1-255 slower to faster
> write(value, speed, wait) - wait is a boolean that, if true, causes the function call to block until move is complete
> 
> writeMicroseconds() - Sets the servo pulse width in microseconds
> read() - Gets the last written servo pulse width as an angle between 0 and 180.
> readMicroseconds() - Gets the last written servo pulse width in microseconds. (was read_us() in first release)
> 
> stop() - stops the servo at the current position
> 
> wait(); - wait for movement to finish
> isMoving() - return true if servo is still moving
> 
> */
> 
> ```
> 
> ![image](https://europe1.discourse-cdn.com/arduino/original/4X/d/8/3/d83946d9066ee25f9925a75b6465f606ed5d35a0.png)

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 12, 2025, 9:49am UTC](https://forum.arduino.cc/t/help-with-making-if-statement-routine-more-efficient/1363808/22 "2025-09-12T09:49:55Z")

</div>

Closed on September 12, 2025, 9:49am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.

[Previous page](https://forum.arduino.cc/t/help-with-making-if-statement-routine-more-efficient/1363808.md?page=1)
