Define Config via External Input

It feels like I am trying to put the the cart before the horse with this one, but....

I want to have an optional configuration.
The option is to be set by an external switch.
If the switch is "off" the Arduino will boot with configuration 1.
If the switch if "on" the Arduino will boot with configuration 2.
Let's say the switch is connected VCC and D5.
At startup only, a digitalRead is done on pin 5 and the configuration determined from the status of pin 5.

I have started writing some code, but the problem is of course I cannot do a digitalRead before "setup", so I did not get very far.
I was wondering f I could get around this by using .c or .cpp tabs.
I am already using .h tabs, so know how they work.
I only know .c and .cpp files are compiled separately, but I cannot find a good explanation of how to use them.
Any help/info would be appreciated.

Let's say the switch is connected VCC and D5.

Lets be smart and say the switch is connect to D5 and ground, and that we'll use the internal pullup resistor.

I have started writing some code, but the problem is of course I cannot do a digitalRead before "setup", so I did not get very far.

It isn't clear why you would want to. You can change how the program behaves after setup() starts, based on the state of a pin. But, you can't change which sketch runs.

So, a little less handwaving and a lot more detail would be good.

I was wondering f I could get around this by using .c or .cpp tabs.

No.

I would think something like this would work.

#include <SPI.h> // etc.

void setup(){
pinMode (2, INPUT_PULLUP);
byte flag = digitalRead(2);
  if (flag == HIGH){
  // high switch setup code
  }
  else {
  // low switch setup code
  }
}
void loop(){
  if (flag == HIGH){
  // high flag code
  }
  else {
  // low flag code
  }
}

PaulS:
Lets be smart and say the switch is connect to D5 and ground, and that we'll use the internal pullup resistor.

That is already what I am basing my final design on.
However, in developing the code, I found that when using the internal pull-up, the status of the pin cannot be manipulated with digitalWrite. So I am starting off without internal pull-up so I can do a soft simulation.

CrossRoads:
I would think something like this would work.

CrossRoads, I have a version of test code that is very similar to yours viz.

#define conf_Optional  //  conf_1 / conf_2 / conf_Optional
#define pinConf 5 // Ext switch input
char conf[7];
int pinConf_state = LOW;

void setup() {
  Serial.begin(9600);
  Serial.println("Serial Started!");
  
#ifdef conf_Optional
  // pinMode(pinConf, INPUT_PULLUP);
  pinMode(pinConf, INPUT); // for soft simulation
  
  pinConf_state = (digitalRead(pinConf));
  Serial.print("1 : ");
  Serial.println(pinConf_state);
  
  //digitalWrite(pinConf, HIGH); // uncomment to simulate soft switch
  //delay(25); 

  pinConf_state = (digitalRead(pinConf));
  Serial.print("2 : ");
  Serial.println(pinConf_state);
  
  if (pinConf_state == 0) strcpy(conf, "conf_1");
  else strcpy(conf, "conf_2");
  
  Serial.print("Conf : ");
  Serial.println(conf);
#endif
/*
#ifdef conf_Optional
  if (conf == "conf_2") #include <Ethernet.h>;
#endif
*/

}

void loop() {

}

The caveat, is that for config 2, I want to include the Ethernet Library and this code won't do it since it is being done in setup.

In theory I need something the equivalent of the below in the header section...

#ifdef lan_Optional
  int pinLan = 5; // Ext switch input
  pinMode(pinLan, INPUT);
//  digitalWrite(pinLan, HIGH); // uncomment for soft switch test only 
  if (digitalRead(pinLan) == HIGH) define lan_Ethernet;
#endif

The above of course won't work, hence my question about .c and .cpp tabs since they are compiled separately.

The only other idea I can think of is to write a pin 5 state change to EEPROM then use that during the next boot-up to determine the config.

aisc:
The caveat, is that for config 2, I want to include the Ethernet Library and this code won't do it since it is being done in setup.

Just include it for both versions and only use it in one.

...R

Robin2:
Just include it for both versions and only use it in one.

...R

My post was to get simplified concept working code.
For the actual sketch a categoric include is not ideal as I am switching between Ethernet and WiFi.

Edit : I need to retract the above. Paul__B's post #6 got me thinking, my concept of the implementation was flawed, and he is right - if I want to switch options while running, both options need to be compiled.
That makes your point valid and the code as suggested by CrossRoads, which is similar to my own test, would work.
A case of overthinking and not being able to see the forest for the trees :frowning:

aisc:
However, in developing the code, I found that when using the internal pull-up, the status of the pin cannot be manipulated with digitalWrite. So I am starting off without internal pull-up so I can do a soft simulation.

That statement was thoroughly confused rubbish.

You can play around with #ifdefs and #include, but they are compile time options. Either code is included, or it is not. If your operating system requires to be dynamically (by a link) configured as to alternate interfaces, it clearly must have all options compiled, but choose - in setup() - which to initialise.

Paul__B:
That statement was thoroughly confused rubbish.

Have you seen the movie "How to lose a guy in 10 days"? - Then I call Bullshit!!!

That statement is accurate and the underlying concept has been tested. Why don't you do yourself a favour and test it yourself before throwing around "thoroughly confused rubbish" opinions.

I'll still be here when you are ready to apologise.
(I hope I don't have to eat my hat on this one...)

You can play around with #ifdefs and #include, but they are compile time options. Either code is included, or it is not. If your operating system requires to be dynamically (by a link) configured as to alternate interfaces, it clearly must have all options compiled, but choose - in setup() - which to initialise.

I don't disagree. I am just trying to implement a simple user interface to change which option to initialize in setup.

Edit : The penny has finally dropped. Your post did make me realize where my approach was flawed. My test code will in fact work.

I'll still be here when you are ready to apologise.
(I hope I don't have to eat my hat on this one...)

I hope that you like hat. Want some ketchup with your hat?

You can NOT, at tun time, influence what the compiler and linker did, on another computer, some time in the past.

You can not, at compile time, read the state of a switch connected to another computer, to decide what to compile.

Enjoy your hat.

Hold the ketchup - this is the statement in question:

aisc:
However, in developing the code, I found that when using the internal pull-up, the status of the pin cannot be manipulated with digitalWrite. So I am starting off without internal pull-up so I can do a soft simulation.

Hold the ketchup - this is the statement in question:

from: aisc on Today at 06:58:32

However, in developing the code, I found that when using the internal pull-up, the status of the pin cannot be manipulated with digitalWrite. So I am starting off without internal pull-up so I can do a soft simulation.

When I read this in its original post I FELT it was correct.

...R

Robin2:
When I read this in its original post I FELT it was correct.

...R

Well in that case I FEEL I can take this statement as your sincere and heartfelt apology :slight_smile:

So I'll don my hat and PaulS can stow his ketchup.

aisc:
Well in that case I FEEL I can take this statement as your sincere and heartfelt apology :slight_smile:

I think you have people mixed up.

I was just trying to make poor joke about a FELT HAT.

I don't think I did anything that required an apology (not in this Thread, anyway)

...R

Robin2:
I think you have people mixed up.

I was just trying to make poor joke about a FELT HAT.

I don't think I did anything that required an apology (not in this Thread, anyway)

...R

Oops - sorry about that - serves me right for not checking poster's names.
Oh the irony of it all.. here I am waiting for an apology and find myself having to issue one.

Lol - good joke though, very subtle - I obviously didn't get it first time round.

So I guess I will take off my hat and have PaulS stand by with the ketchup while I wait for Paul__B's response.