Min(x, y) function for 37 variables

The code I used is this:

    dir = min(min(min(min(min(min(min(min(min(min(min(var1, var2), min(var3, var4)), min(var5, var6), min(var7, var8)), min(var9, var10)), min(var11, var12), min(var13, var14)), min(var15, var16), min(var17, var18)), min(var19, var20), min(var21, var22)), min(var23, var24)), min(var25, var26), min(var27, var28)), min(var29, var30), min(var31, var32)), min(var33, var34), min(var35, var36))

I wanted to use 37 variables as well but the main problem I have is just finding the minimum of these variables. The error code message I got was this:

Arduino: 1.8.19 (Linux), Board: "Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"

/home/matthew/arduino-1.8.19/arduino-builder -dump-prefs -logger=machine -hardware /home/matthew/arduino-1.8.19/hardware -hardware /home/matthew/.arduino15/packages -tools /home/matthew/arduino-1.8.19/tools-builder -tools /home/matthew/arduino-1.8.19/hardware/tools/avr -tools /home/matthew/.arduino15/packages -built-in-libraries /home/matthew/arduino-1.8.19/libraries -libraries /home/matthew/Arduino/libraries -fqbn=arduino:avr:mega:cpu=atmega2560 -vid-pid=2341_0042 -ide-version=10819 -build-path /tmp/arduino_build_163955 -warnings=none -build-cache /tmp/arduino_cache_246478 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.arduinoOTA.path=/home/matthew/.arduino15/packages/arduino/tools/arduinoOTA/1.3.0 -prefs=runtime.tools.arduinoOTA-1.3.0.path=/home/matthew/.arduino15/packages/arduino/tools/arduinoOTA/1.3.0 -prefs=runtime.tools.avr-gcc.path=/home/matthew/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7 -prefs=runtime.tools.avr-gcc-7.3.0-atmel3.6.1-arduino7.path=/home/matthew/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7 -prefs=runtime.tools.avrdude.path=/home/matthew/.arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17 -prefs=runtime.tools.avrdude-6.3.0-arduino17.path=/home/matthew/.arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17 -verbose /home/matthew/Arduino/ELEGOO The Most Complete Starter Kit for MEGA V1.0.2023.05.05/ELEGOO The Most Complete Starter Kit for MEGA V1.0.2023.05.05/English/lessons/Lesson 30 - Servo/servo_big_project/servo_big_project.ino
/home/matthew/arduino-1.8.19/arduino-builder -compile -logger=machine -hardware /home/matthew/arduino-1.8.19/hardware -hardware /home/matthew/.arduino15/packages -tools /home/matthew/arduino-1.8.19/tools-builder -tools /home/matthew/arduino-1.8.19/hardware/tools/avr -tools /home/matthew/.arduino15/packages -built-in-libraries /home/matthew/arduino-1.8.19/libraries -libraries /home/matthew/Arduino/libraries -fqbn=arduino:avr:mega:cpu=atmega2560 -vid-pid=2341_0042 -ide-version=10819 -build-path /tmp/arduino_build_163955 -warnings=none -build-cache /tmp/arduino_cache_246478 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.arduinoOTA.path=/home/matthew/.arduino15/packages/arduino/tools/arduinoOTA/1.3.0 -prefs=runtime.tools.arduinoOTA-1.3.0.path=/home/matthew/.arduino15/packages/arduino/tools/arduinoOTA/1.3.0 -prefs=runtime.tools.avr-gcc.path=/home/matthew/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7 -prefs=runtime.tools.avr-gcc-7.3.0-atmel3.6.1-arduino7.path=/home/matthew/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7 -prefs=runtime.tools.avrdude.path=/home/matthew/.arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17 -prefs=runtime.tools.avrdude-6.3.0-arduino17.path=/home/matthew/.arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17 -verbose /home/matthew/Arduino/ELEGOO The Most Complete Starter Kit for MEGA V1.0.2023.05.05/ELEGOO The Most Complete Starter Kit for MEGA V1.0.2023.05.05/English/lessons/Lesson 30 - Servo/servo_big_project/servo_big_project.ino
Using board 'mega' from platform in folder: /home/matthew/.arduino15/packages/arduino/hardware/avr/1.8.5
Using core 'arduino' from platform in folder: /home/matthew/.arduino15/packages/arduino/hardware/avr/1.8.5
Detecting libraries used...
/home/matthew/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/bin/avr-g++ -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -I/home/matthew/.arduino15/packages/arduino/hardware/avr/1.8.5/cores/arduino -I/home/matthew/.arduino15/packages/arduino/hardware/avr/1.8.5/variants/mega /tmp/arduino_build_163955/sketch/servo_big_project.ino.cpp -o /dev/null
Alternatives for Servo.h: [Servo@1.1.8]
ResolveLibrary(Servo.h)
  -> candidates: [Servo@1.1.8]
/home/matthew/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/bin/avr-g++ -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -I/home/matthew/.arduino15/packages/arduino/hardware/avr/1.8.5/cores/arduino -I/home/matthew/.arduino15/packages/arduino/hardware/avr/1.8.5/variants/mega -I/home/matthew/arduino-1.8.19/libraries/Servo/src /tmp/arduino_build_163955/sketch/servo_big_project.ino.cpp -o /dev/null
Alternatives for Stepper.h: [Stepper@1.1.3]
ResolveLibrary(Stepper.h)
  -> candidates: [Stepper@1.1.3]
/home/matthew/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/bin/avr-g++ -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -I/home/matthew/.arduino15/packages/arduino/hardware/avr/1.8.5/cores/arduino -I/home/matthew/.arduino15/packages/arduino/hardware/avr/1.8.5/variants/mega -I/home/matthew/arduino-1.8.19/libraries/Servo/src -I/home/matthew/arduino-1.8.19/libraries/Stepper/src /tmp/arduino_build_163955/sketch/servo_big_project.ino.cpp -o /dev/null
Error while detecting libraries included by /tmp/arduino_build_163955/sketch/servo_big_project.ino.cpp
Using cached library dependencies for file: /home/matthew/arduino-1.8.19/libraries/Servo/src/avr/Servo.cpp
Using cached library dependencies for file: /home/matthew/arduino-1.8.19/libraries/Servo/src/mbed/Servo.cpp
Using cached library dependencies for file: /home/matthew/arduino-1.8.19/libraries/Servo/src/megaavr/Servo.cpp
Using cached library dependencies for file: /home/matthew/arduino-1.8.19/libraries/Servo/src/nrf52/Servo.cpp
Using cached library dependencies for file: /home/matthew/arduino-1.8.19/libraries/Servo/src/sam/Servo.cpp
Using cached library dependencies for file: /home/matthew/arduino-1.8.19/libraries/Servo/src/samd/Servo.cpp
Using cached library dependencies for file: /home/matthew/arduino-1.8.19/libraries/Servo/src/stm32f4/Servo.cpp
Using cached library dependencies for file: /home/matthew/arduino-1.8.19/libraries/Stepper/src/Stepper.cpp
Generating function prototypes...
/home/matthew/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/bin/avr-g++ -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -I/home/matthew/.arduino15/packages/arduino/hardware/avr/1.8.5/cores/arduino -I/home/matthew/.arduino15/packages/arduino/hardware/avr/1.8.5/variants/mega -I/home/matthew/arduino-1.8.19/libraries/Servo/src -I/home/matthew/arduino-1.8.19/libraries/Stepper/src /tmp/arduino_build_163955/sketch/servo_big_project.ino.cpp -o /tmp/arduino_build_163955/preproc/ctags_target_for_gcc_minus_e.cpp
servo_big_project:221:391: error: macro "min" passed 3 arguments, but takes just 2
     dir = min(min(min(min(min(min(min(min(min(min(min(var1, var2), min(var3, var4)), min(var5, var6), min(var7, var8)), min(var9, var10)), min(var11, var12), min(var13, var14)), min(var15, var16), min(var17, var18)), min(var19, var20), min(var21, var22)), min(var23, var24)), min(var25, var26), min(var27, var28)), min(var29, var30), min(var31, var32)), min(var33, var34), min(var35, var36))
                                                                                                                                                                                                                                                                                                                                                                                                       ^
Using library Servo at version 1.1.8 in folder: /home/matthew/arduino-1.8.19/libraries/Servo 
Using library Stepper at version 1.1.3 in folder: /home/matthew/arduino-1.8.19/libraries/Stepper 
exit status 1
macro "min" passed 3 arguments, but takes just 2

Put the values in a 37-element array rather than 37 different variables. Loop through the array and find the minimum.

Or use the min function with an initializer list:
e.g., to find the minimum of 4 values:

int min_value = std::min({10, 5, 8, 2});

You got min( x, y, z) somewhere in your Code.

int varArray[] = { var1, var2, ... };
int dir = 32767;
for(int i = 0; i<sizeof(varArray)/sizeof(varArray[0]);i++)
  if(varArray[i]<dir)
    dir = varArray[i];

std::min - cppreference.com looks cool, but I got this error:

error: macro "min" passed 4 arguments, but takes just 2
 int min_value = std::min({10, 5, 8, 2});

I'd blame:

Works on an ESP32:

#include <algorithm>

void setup() {
  Serial.begin(115200);
  delay(2000);

  Serial.println(std::min({10, 5, 8, 2}));
}

void loop() {
}

Yeah, I was wondering about that. A lot of std stuff can't be implemented on a base arduino.
I should probably have given a warning.

ask yourself: when you just need the lowest value of 37 ... why do you store the other 36 anyway?

Can you say how to do that please?

I didn't know how to do it at the time

If I were to use that, do you know how I could find the lower number because it only shows how to sort it from lowest to highest and reverse

int varArray[] = { var1, var2, ... };
int dir = INT_MAX;
for(int i = 0; i<sizeof(varArray)/sizeof(varArray[0]);i++)
  if(varArray[i]<dir)
    dir = varArray[i];

Use of INT_MAX is more portable in an environment with multiple sizes for int type.

Instead of INT_MAX you could use the First element of the Array.

Optimizes to the same when compiled, either approach is better than using a magic number which assumes a specific architecture.

#include <ArduinoSort.h>

void setup() {
  Serial.begin(9600);
  delay(3000);
  // Set up array of 5 numbers
  int myArray[5];
  myArray[0] = 3;
  myArray[1] = 1;
  myArray[2] = 4711;
  myArray[3] = 2;
  myArray[4] = 0;

  // Sort normally
  sortArray(myArray, 5);
  int smallest = myArray[0];
  Serial.print("Smallest: ");
  Serial.println(smallest);
}

void loop() {
}

I got that to work, thank you! Do you by chance know how I can make this command work?

for (y = 1; y  <=37; y++) {
      if (var y == smallest) {
        myServo.write(y);
      }
    }

I want to make it so it has var and beside it is a number from 1-37. Like this: var2 and it has to repeat going upwards 37 times.

I'll look, but surely someone must at some point said this is what array variables are for.

C/C++ has no direct mechanism like you imagine. There are languages that do, the creators of C didn't mess with that.

Array of integers, you can refer to one in an array by its index, just a number.

Arrays go good with for loops.

a7

It's already determined, this is the command after