#ifdef doesn't seem to be working

I've seen people use this in code, and thought I'd try it. I have different PCB version, which use different pins from the Arduino. So I can uncomment the version I need when uploading, but I get the redeclaration error. Is there something wrong with the way I'm using #ifdef?

//#define Version_3.0
//#define Version_4.0
//#define Version_4.1
#define Version_4.2

int CenterDebounce = 15;  //+- this much is still considered center on the joystick

struct config_mySettings { //these are stored in EEPROM
  unsigned long center;    //center reading of axis on joystick
  unsigned long up;        //upper limit of axis on joystick
  unsigned long down;        //lower limit of axis on joystick
}
joystick;


void setup(){
#ifdef Version_3.0
  int motorUpPin = 6;     //pin 6
  int motorDownPin = 11;  //pin 11
  int LEDpin = A5;        //Power indicator
  int analogPin = A2;     //joystick connection pin
  int buttonPin = 2;     //program button
#endif

#ifdef Version_4.1
  int motorUpPin = 6;     //pin 6
  int motorDownPin = 11;  //pin 11
  int LEDpin = A5;        //Power indicator
  int analogPin = A2;     //joystick connection pin
  int buttonPin = 2;     //program button
#endif

#ifdef Version_4.2
  int motorUpPin = 6;     //pin 6
  int motorDownPin = 11;  //pin 11
  int LEDpin = A5;        //Power indicator
  int analogPin = A2;     //joystick connection pin
  int buttonPin = 2;     //program button
#endif

You can't use a dot in a C/C++ variable name. Change them all to the form:

#define Version_4_2

Pete

Also probably not very useful to put them as local declarations inside setup - you probably want these to be global consts. In that case you're about to be tripped up by another Arduino pre-processor bug, but you can avoid it by putting a variable definition in front of your conditional sections.

You probably also want to use #if/#elif with a default case that raises an error or provides default definitions when the version is not defined.

//#define Version_3_0
//#define Version_4_0
//#define Version_4_1
#define Version_4_2

byte me;
#if defined Version_3_0
const int motorUpPin = 6;     //pin 6
const int motorDownPin = 11;  //pin 11
const int LEDpin = A5;        //Power indicator
const int analogPin = A2;     //joystick connection pin
const int buttonPin = 2;     //program button
#elif defined Version_4_1
const int motorUpPin = 6;     //pin 6
const int motorDownPin = 11;  //pin 11
const int LEDpin = A5;        //Power indicator
const int analogPin = A2;     //joystick connection pin
const int buttonPin = 2;     //program button
#elif defined Version_4_2
const int motorUpPin = 6;     //pin 6
const int motorDownPin = 11;  //pin 11
const int LEDpin = A5;        //Power indicator
const int analogPin = A2;     //joystick connection pin
const int buttonPin = 2;     //program button
#else
#error version undefined
#endif

Ok, thanks for solving that mystery!
cheers.

I have finished a project and have a few motor controllers ready to ship out to friends. But I see now that my sketch does what you said not to do. But it works, so I don't understand why. I declared my variables in setup(), but use them in loop(). But my controllers work perfect, so will they ever stop working because of this? The variables in question don't actually change, they are just defining pin numbers. see my setup and loop below:

void setup(){
    Serial.begin(9600);
#ifdef PCB_Version_2.0
  LEDpin = A1;        //power indicator
  analogPin = A2;     //joystick connected to A2
  buttonPin = A5;     //program button on digital pin 2
#endif

#ifdef PCB_Version_3.0
  LEDpin = A1;        //Power indicator
  analogPin = A3;     //joystick connection pin
  buttonPin = A5;     //program button
#endif

#ifdef PCB_Version_4_0
  LEDpin = A5;        //Power indicator
  analogPin = A3;     //joystick connection pin
  buttonPin = 2;      //program button
#endif

#ifdef PCB_Version_4.2
  LEDpin = A5;        //Power indicator
  analogPin = A2;     //joystick connection pin
  buttonPin = 2;      //program button
#endif
  
  pinMode(buttonPin, INPUT);  //set pin as input
  pinMode(motorUpPin, OUTPUT);
  pinMode(motorDownPin, OUTPUT);
  pinMode(LEDpin, OUTPUT);
  digitalWrite(LEDpin, HIGH); //power indicator
  digitalWrite(buttonPin, HIGH);  //turn on pullup resistor
  digitalWrite(motorUpPin, LOW);  //be sure motor is off
  digitalWrite(motorDownPin, LOW);  //be sure motor is off
  analogWriteSAH_Init();  //this initiates the PWM timer to run on 20Khz
  EEPROM_readAnything(50, joystick);     //load settings from EEPROM
}

void loop(){
  if (buttonPressed()) {  //has the program button been pushed?
    Serial.println("The button has been pressed.");
    Serial.println("Entering program mode.");
    programMode();  //enter program mode if so
  }
  int reading = analogRead(analogPin);  //get a reading from the analogPin
  if (reading < 100 || reading > 900) error(); //the reading is way out of range (connector failure??)

  int offset = reading - joystick.center; //how far the reading is from the center value
  offset = abs(offset);
  //Serial.print("THE READING:  ");
  //Serial.println(reading);
  if (offset < CenterDebounce) { //we'll allow for the center reading to float a little
    motorIdle();  //turn off motor if it's this close to center
  }
  else if (reading > joystick.center) motorUp(reading);   //motor up in this range  ( center + 35 < reading < 900 )
  else if (reading < joystick.center) motorDown(reading); //motor down in this range ( center - 35 > reading > 100 )
  delay(10);
}

I can change them all if need be, but it will envolve a great deal of unpackaging. They work now. Will they ever NOT work because of this problem? What I've read online says a variable declared in setup is NOT available to use in the loop.

Ah, but you haven't declared those variables in setup(), merely initialized them. Take LEDpin, for example. Somewhere you must have an actual declaration such as:

int LEDpin ;

I don't see it in the code you posted, so I suspect that it is somewhere outside of setup() and loop(), presumably at file scope, which will make them "global" variables -- available to any function that does not re-declare them. If that is the case, then you needn't worry.

SouthernAtHeart:
I declared my variables in setup(), but use them in loop().

No, you've only assigned values to them in setup. Since they are constant values, it would have been better to declare them as const and assign their values when they are defined, as shown in the example I posted previously. This will protect you against bugs which tried to change these values, and also enable some compiler optimisations.

WHEW! Thanks again. I thought I was in big trouble, but you are right:

#include <EEPROM.h>
#include "writeAnything.h"
#include <math.h>
//#define PCB_Version_2.0
//#define PCB_Version_4.0
#define PCB_Version_4.2

int CenterDebounce = 15;  //+- this much is still considered center on the joystick
int motorUpPin = 6;    
int motorDownPin =11;
int LEDpin;
int analogPin;
int buttonPin;
char splashScreen[50];
struct config_mySettings { //these are stored in EEPROM
  unsigned long center;    //center reading of axis on joystick
  unsigned long up;        //upper limit of axis on joystick
  unsigned long down;        //lower limit of axis on joystick
}
joystick;

I will touch up my code, putting them back the way they should be. I had moved them to setup(), trying to solve that initial problem.
thanks again.