Using #if - #endif

#if defined PCB_Version_2_0
const int LEDpin = A1;        //power indicator
const int analogPin = A2;     //joystick connected to A2
const int buttonPin = A5;     //program button on digital pin 2
const String splash = "Microphone Controller Version 2.0";
#elif defined PCB_Version_3_0
const int LEDpin = A1;        //Power indicator
const int analogPin = A3;     //joystick connection pin
const int buttonPin = A5;     //program button
const int potPin = A6;        //potentiometer
const String splash = "Microphone Controller Version 3.0";
#endif

I have this code before my Setup() block. Is it okay to use this again later within my loop, something like this:

#if defined PCB_Version_3_0
   ...check on the potentiometer
#endif

because I need to do this, only if I'm using the Version 3 (Ver 2 doesn't have a POT). I've tried adding this into my code and it compiles, but I just want to make sure I'm not doing something wrong. I'm not real familiar with #if.
Thanks.

AFAIK you can use that as many times as you like.


Rob

While it is perfectly acceptable to do it that way, a more common approach is to set the state of a variable and then use that. For example:

#define VERSION2 2
#define VERSION3 3

int whichPCB;

#if defined PCB_Version_2_0
whichPCB = VERSION2 ;
const int LEDpin = A1;        //power indicator
const int analogPin = A2;     //joystick connected to A2
const int buttonPin = A5;     //program button on digital pin 2
const String splash = "Microphone Controller Version 2.0";
#elif defined PCB_Version_3_0
whichPCB = VERSION3 ;
const int LEDpin = A1;        //Power indicator
const int analogPin = A3;     //joystick connection pin
const int buttonPin = A5;     //program button
const int potPin = A6;        //potentiometer
const String splash = "Microphone Controller Version 3.0";
#endif

// ...a bunch of code...
if (whichPCB == VERSION3 ) {
   // do whatever...
}

While it is perfectly acceptable to do it that way, a more common approach is to set the state of a variable and then use that. For example:

The problem with that approach is that:

if (whichPCB == VERSION3 ) {
   // do whatever...
}

will be compiled and included in the hex file regardless of which version of the board is being used. Using #if/#endif, the code in between will only be compiled and included in the hex file if the relevant board is being used.

Yep…you’re right.

This code is all located before my setup(). Is it OK to put a second #define inside the #ifdef section. See the second line of this code:

...
#elif defined PCB_Version_3_0
#define DownPotentiometer A6  //POT to regulate the max down speed (Ver 3 only)
const int LEDpin = A1;        //Power indicator
const int analogPin = A3;     //joystick connection pin
const int buttonPin = A5;     //program button
const int potPin = A6;        //potentiometer
const String splash = "Microphone Controller Version 3.0";
#elif defined PCB_Version_4_0
const int LEDpin = A5;        //Power indicator
const int analogPin = A3;     //joystick connection pin
const int buttonPin = 2;      //program button
const String splash = "Microphone Controller Version 4.0";
#endif

Is it OK to put a second #define inside the #ifdef section.

Yes. Be careful how you try to use that name later. It needs to be in an #if/#endif block using the same name as the block it is defined in.

Here's what I had in mind:

This part before my setup(), defines the PCB, and also some versions will have a POT on an analogPin, so you see DownPotentiometer is defined in version 3.

#if defined PCB_Version_2_0
const int LEDpin = A1;        //power indicator
const int analogPin = A2;     //joystick connected to A2
const int buttonPin = A5;     //program button on digital pin 2
const String splash = "Microphone Controller Version 2.0";
#elif defined PCB_Version_3_0
#define DownPotentiometer A6  //POT to regulate the max down speed (Ver 3 only)
const int LEDpin = A1;        //Power indicator
const int analogPin = A3;     //joystick connection pin
const int buttonPin = A5;     //program button
const int potPin = A6;        //potentiometer
const String splash = "Microphone Controller Version 3.0";
#elif defined PCB_Version_4_0
const int LEDpin = A5;        //Power indicator
const int analogPin = A3;     //joystick connection pin
const int buttonPin = 2;      //program button
const String splash = "Microphone Controller Version 4.0";
#elif defined  PCB_Version_4_2
const int LEDpin = A5;        //Power indicator
const int analogPin = A2;     //joystick connection pin
const int buttonPin = 2;      //program button
const String splash = "Microphone Controller Version 4.2";
#endif

And then in my motor function if DownPotentiometer is defined, if reads the value from that POT, otherwise, it assumes there is no POT.

void motorDown(unsigned long reading) {  //run the motor down (1 to 799 is 0% to 100%)
int maxSpeed = 799; //set max down speed to 100%
#ifdef DownPotentiometer //is there a POT for adjusting the max down speed?
  int analogVal = analogRead(DownPotentiometer); //read the val, will be  0 to 1023
  //map function:  map(value, fromLow, fromHigh, toLow, toHigh)
  maxSpeed = map (analogVal, 0,1023, 400, 799); //remap the val; 799 is 100% motor speed 
#endif

  //int mappedVal = fscale( joystick.down, joystick.center, 799, 1, reading, 4.5); //the curve is pos, not neg, b/c the map is inverted
  int mappedVal = fscale( joystick.down, joystick.center - CenterDebounce, maxSpeed, 50, reading, 4.5); //the curve is pos, not neg, b/c the map is inverted
  digitalWrite(motorUpPin, LOW);  //going up
  digitalWrite(motorDownPin, HIGH);  //pulled low for motor ground
  analogWriteSAH(mappedVal);  //Set the PWM for the motor speed
  Serial.print("Motor Down Speed: ");
  Serial.println(mappedVal);
}