Runtime define and ifdef

Hello smart people

I'm working on a small PC fan controller project on ESP32. Here's the setup. I have 4 fan channels with 3 pins each.

  1. PWR/PWM pin, for DC control and voltage regulation for 3 pin computer fan.
  2. PWM pin for actual PWM control for 4 pin fans. The PWR pin in this case is set to max always
  3. Read pin for reading of rising edge of fan hall sensor. Attached as an interrupt.

Here's what I want. When the PC powers on it also turns on the ESP32 controller that when it starts sets all the variables it needs and then starts the following function:

void fansetup(){

  //Set the pulses to 0
  FANBUS_1_NB_PULSE=0;
  FANBUS_2_NB_PULSE=0;
  FANBUS_3_NB_PULSE=0;
  FANBUS_4_NB_PULSE=0;

  //Write max speed to all the fan buses
  analogWrite(FANBUS_1_PWM,MAX_SPEED);
  analogWrite(FANBUS_2_PWM,MAX_SPEED);
  analogWrite(FANBUS_3_PWM,MAX_SPEED);
  analogWrite(FANBUS_4_PWM,MAX_SPEED);
  analogWrite(FANBUS_1_PWR,MAX_SPEED);
  analogWrite(FANBUS_2_PWR,MAX_SPEED);
  analogWrite(FANBUS_3_PWR,MAX_SPEED);
  analogWrite(FANBUS_4_PWR,MAX_SPEED);
  //Wait a bit for the fans to spin up
  delay(SPINUP_TIME);

  //Read the fan speeds
  sei();
  delay(DELAY_TIME);
  cli();

  //Check if NB of pulses is greater than 0 and calculate the rpm for each fan
  if(FANBUS_1_NB_PULSE =! 0){
    FAN_1=1;
    int FAN_1_SPEED_1=speedcalculation(FANBUS_1_NB_PULSE , FAN_1_HALL_NUMBER);
  }

  if(FANBUS_2_NB_PULSE =! 0){
    FAN_2=1;
    int FAN_2_SPEED_1=speedcalculation(FANBUS_2_NB_PULSE , FAN_2_HALL_NUMBER);
  }

  if(FANBUS_3_NB_PULSE =! 0){
    FAN_3=1;
    int FAN_3_SPEED_1=speedcalculation(FANBUS_3_NB_PULSE , FAN_3_HALL_NUMBER);
  }

  if(FANBUS_4_NB_PULSE =! 0){
    FAN_4=1;
    int FAN_4_SPEED_1=speedcalculation(FANBUS_4_NB_PULSE , FAN_4_HALL_NUMBER);
  }


  //Write to the PWM controler 50%
  analogWrite(FANBUS_1_PWM,HALF_SPEED);
  analogWrite(FANBUS_2_PWM,HALF_SPEED);
  analogWrite(FANBUS_3_PWM,HALF_SPEED);
  analogWrite(FANBUS_4_PWM,HALF_SPEED);
  //Wait for the RPM to stabilise
  delay(SPINUP_TIME);
 
  //Start listening to interrupts
  sei();
  delay(DELAY_TIME);
  cli();

  //Read the RPM from the second run
  if(FAN_1 =! 0){

    int FAN_1_SPEED_2=speedcalculation(FANBUS_1_NB_PULSE , FAN_1_HALL_NUMBER);
  }

  if(FAN_2 =! 0){

    int FAN_2_SPEED_2=speedcalculation(FANBUS_2_NB_PULSE , FAN_2_HALL_NUMBER);
  }

  if(FAN_3 =! 0){

    int FAN_3_SPEED_2=speedcalculation(FANBUS_3_NB_PULSE , FAN_3_HALL_NUMBER);
  }

  if(FAN_4 =! 0){

    int FAN_4_SPEED_2=speedcalculation(FANBUS_4_NB_PULSE , FAN_4_HALL_NUMBER);
  }

}

What I want to achieve is to be able to check if the fans are alive and if they are, are they PWM controllable and to make some kind of definition so I don't have to run whole blocks of code for controlling fans that aren't connected. This would be achieved by having the controller check the speed after reducing the PWM to half and checking if the fan speed now is below a certain reasonable RPM to make sure that they are PWM controllable.

Something like:

#DEFINE FAN1BUS 1

#ifdef FAN1BUS 1 
analogWrite(FANBUS_1_PWR,MAX_SPEED);
#endif


#DEFINE FAN1BUS 2

#ifdef FAN1BUS 2
analogWrite(FANBUS_1_PWM,MAX_SPEED);
#endif

But have this kind of thing during runtime. This reduces code size the controller has to go through considerably.

Any help would be great. :confused:

Where are you stuck ?

In setup()
Set the fan to half speed
Read the RPM for long enough to allow it to spin up
If the RPM is greater than zero after a period then set a boolean to true

Later in the code check the boolean and execute the speed control code if it is true

That was my ide however it still poses a problem down the line. I want to have multiple configs and fan bus biases and so forth. Checking a lot of booleans isn't something I want to waste a lot of clocks on.

Checking a lot of booleans isn't something I want to waste a lot of clocks on.

Then use the bits of an integer variable and test them using bit masking. That will be faster.

Even if you could do runtime #ifdefs you would need to check them wouldn't you ?

Another idea. In setup() check which fans are available and store the data for them in an array or an array of structs and store the number of active fans in a variable. In loop() read the data for the active fans from the array. You will only be reading and changing data for active fans and can do that in a for loop