Need to be able to pass variable in analogRead

I have 16 Channel relay system with feedback voltage (Analog). So when I send command to operate the relay, I should be able to read the Analog voltage on that channel without having to hard cade. For example analogRead(A0), analogRead(A1)

I am looking at passing analogRead (Ai) where i will have values from 0 to 15.

Is there any way out

You almost answered your own question there with the idea of "Ai" :wink: ... use an array of pin numbers.

(What does it mean for a relay to have analog feedback though- just curious.)

Hello milind23
Post your current sketch, well formated, with comments and in so called code tags "</>" to see how we can help.

Have a nice day and enjoy coding in C++.

Yes for testing purpose I tried the following
int i=0;
int sensorvoltage = analogRead(Ai);

It is giving me an error

Relay are connected to different sensors, so when I operate the relay I have to read the analog voltage on each channel. Hope I have addressed your curiosity
:grinning:

if (relay_number == 0)
{
int i=0;
int sensorValue = analogRead(Ai);
if (sensorValue > 100)
{
Serial.println("Sensor1 Activated");
}
}

This is not working

You can't just make stuff up. Your attempt is very creative, you do not know enough to be creative using C++!

@marble_hazelnut gave you a google word "array". google

arrays in C

add Arduino to focus and find some learning materials you like. In the meantime, take a look at these few lines of code:

// define some pin numbers where you want to analogRead:

const byte inputPins[ ] = {A0, A1, A2};

// later... to read the first pin

    int reading = analogRead(inputPins[0]);

// and to read the second pin

    int reading = analogRead(inputPins[1]);

You use the array name and an index N in brackets [ N ] to refer to one element on the array.

Indexes start at 0 and go to N - 1 where N is the number of elements in the array.

a7

consider

const byte AnlgPin [] = { A0, A1, A2 };
const byte RelayPin [] = { 11, 12, 13 };
#define N_Devices    sizeof(RelayPin)

byte stateLst [N_Devices];

enum { Off = HIGH, On = LOW };

// -----------------------------------------------------------------------------
void
loop (void)
{
    for (unsigned i = 0; i < N_Devices; i++)  {
        byte state = (100 < analogRead (AnlgPin [i]));

        if (stateLst [i] != state)  {
            stateLst [i] = state;
            digitalWrite (RelayPin [i], ! state);
        }
    }
}

// -----------------------------------------------------------------------------
void
setup (void)
{
    Serial.begin (9600);

    for (unsigned i = 0; i < N_Devices; i++)  {
        stateLst [i] = Off;
        digitalWrite (RelayPin [i], ! stateLst [i]);
        pinMode      (RelayPin [i], OUTPUT);
    }
}

A1, A2….etc are just defines that lead to a number. you can either pass pin directly, for example

int i = 7;
analogRead(i); // read pin 7

or put all your pins in an array and then you can access by orderly index

int pins[15] = {A1, A2……A15};
int i = 0;
analogRead(pins[i]); // read pin A1

Seems to me you're describing a 16:1 analog multiplexer
Any more details on your system?
Perhaps a circuit diagram?

Thank you got it

Got it

Thank you all for prompt replies

Hi,
Can you please post a schematic of your project?
Please do not use Fritzy, a hand drawn image will be fine.
Include ALL power supplies, component names and pin labels.

We need to see how you have your hardware connected.

Thanks.. Tom.... :smiley: :+1: :coffee: :australia:

What you are saying is ambiguous.

If you are saying your 16 channels of relays feed back to 16 analogue inputs then all you have to do is to use the same number in the analogue read as you did to set which relay to activate. Although you don't have 16 analogue inputs on many Arduinos.

If you are saying you have 16 channels and the relays funnel one of 16 inputs to the same analogue pin then you have no need to change the analogue pin you read.

As others have said we need to see a schematic of what you have, or the code that you would consider to be hard coded, for at least three channels.

You are advised to post complete code, not snippets. Please read How to get the best out of this forum how to post code using code tags.

Post the full error that you get; when using IDE 1.8.x, there will be a button at the right hand side in the orange bar to "coppy error messages"; click it and next paste it here, again using code tags (the </> button in a reply windows on the forum).

A most useless description of the problem. Describe what you expect your code to do and describe what it actually does.

Hello Tom,

Unfortunately, the schematic is at hand drawn. I have purchased 16 Channel Relay board from the net and will experiment with that. Once when it is all working will have the drawing ready.

Sorry about that.

I got the solution from Alto777 and gcjr. So thank you all.

Great answer, thank you so much. I may need your help in the future as well. I am a mechanical engineer and not from software background so please bare with me.

Regards,
Milind

+918010949597

People don't have problems with that. Take a photo and drop it here in a reply box.

So if you are going to make a mechanical something, would you make it and then do the drawing? I think not. A mechanical engineer would draw up what is needed to be made and then make it. As my school metal work teacher and my technical drawing teacher told me back in the 60s, "if you can't draw it then you can't make it".

It is exactly the same with an electronic engineer, draw it then make it. If you don't the you don't know what to make and don't know if you have made it correctly or if the design is just wrong.