Averaging multiple inputs only when connected using one standard sketch

Hi all, long time listener, first time caller. Love your show.

My question is this: Is there a feasible way of having one standard code that would average analog inputs only if they are physically connected to the microcontroller? For the project I am working on, I don't want to rewrite the sketch depending upon how many analog inputs are present. I want the end user to be able to plug in up to 3 inputs and have the sketch calculate the average accurately no matter if it's 1, 2 or 3 connected. There is an RC low pass filter for each one of the inputs, so individual input smoothing isn't necessary, just multiple input averaging. I'm thinking of something analogous to the Microsoft Excel function that only uses a cell's value if something is actually entered into that cell; if it's blank, it's not included in the average.

Gracias,

Joe

I'd set analog pin as digital first, set pull-up and read status. If not connected, most likely you get 1. To make it work reliably, analog voltage has to be lower 2V and less tha 10k impedance.

Similar to what @Magician says, you need a strategy to test for which analog pins are being fed with inputs. Whether you can tie the pins to ground or +5v with resistors (external or internal) will depend on the effect of those resistors on the signals.

Could you arrange to use a plug that connects to all 3 pins even for 1 input and include in the plug pull down resistors for the two unused pins?

Another thing to bear in mind is when are the connections changed? Does this only happen before the Arduino is switched on or could it happen while it is running. If the latter it will need to check continually for the changed connection.

…R

Magician: I'd set analog pin as digital first, set pull-up and read status. If not connected, most likely you get 1. To make it work reliably, analog voltage has to be lower 2V and less tha 10k impedance.

Unfortunately the end user won't be able to alter the sketch, so the pins have to remain analog from the start

Robin2: Similar to what @Magician says, you need a strategy to test for which analog pins are being fed with inputs. Whether you can tie the pins to ground or +5v with resistors (external or internal) will depend on the effect of those resistors on the signals.

Could you arrange to use a plug that connects to all 3 pins even for 1 input and include in the plug pull down resistors for the two unused pins?

Another thing to bear in mind is when are the connections changed? Does this only happen before the Arduino is switched on or could it happen while it is running. If the latter it will need to check continually for the changed connection.

...R

The controller could and probably would be disconnected from power before any changes were made to the circuitry. I'll have to give the plug idea some thought, there could be some potential (pun intended) there.

thx,

Joe

Unfortunately the end user won't be able to alter the sketch, so the pins have to remain analog from the start

He doesn't have to altered a sketch. You setting pinMode command inside setup(), than reading status and use it in "if" statement. Here is quasy code:

int an0 = 0;  // If "0" - disconnected, default state. 
int an1 = 0;
int an2 = 0
void setup()
{
 Serial.begin(115200) ;
  pinMode(14, OUTPUT); // Analog 0
 digitalWrite(14, HIGH); 
 delay(100);
   an0 =  digitalRead(14);
  pinMode(15, OUTPUT); // Analog 0
 digitalWrite(15, HIGH); 
 delay(100);
   an1 =  digitalRead(15);
  pinMode(16, OUTPUT); // Analog 0
 digitalWrite(16, HIGH); 
 delay(100);
   an2 =  digitalRead(16);
}

 loop()
{
if ( an0 ) --->> use data to average;
if ( an1 ) --->> use data to average;
if ( an2 ) --->> use data to average;

Ah I see what you mean, Magician. Another sticking point is that I need more than 1 bit resolution for the input state (i.e., I need to know if they're connected and also make a decision based upon the voltage level), that's why they are coming in over the analog pins. With the information being sent, I could probably get away with 2 or 3 bits instead of the 8 for PWM or 10 for AnalogRead, so maybe a Constrain function would help me filter out any anomalous responses if I were to use a pullup resistor on the analog pins.

Joe

Magician:

Unfortunately the end user won't be able to alter the sketch, so the pins have to remain analog from the start

He doesn't have to altered a sketch. You setting pinMode command inside setup(), than reading status and use it in "if" statement. Here is quasy code:

int an0 = 0;  // If "0" - disconnected, default state. 
int an1 = 0;
int an2 = 0
void setup()
{
 Serial.begin(115200) ;
  pinMode(14, OUTPUT); // Analog 0
 digitalWrite(14, HIGH); 
 delay(100);
   an0 =  digitalRead(14);
  pinMode(15, OUTPUT); // Analog 0
 digitalWrite(15, HIGH); 
 delay(100);
   an1 =  digitalRead(15);
  pinMode(16, OUTPUT); // Analog 0
 digitalWrite(16, HIGH); 
 delay(100);
   an2 =  digitalRead(16);
}

loop() { if ( an0 ) --->> use data to average; if ( an1 ) --->> use data to average; if ( an2 ) --->> use data to average;