Seperating analog inputs from eachother.

Hello.

I'm trying something really basic, and I am quite new to Arduino.

I have 4 wires connected to 4 different conductive surfaces, where I am trying to monitor whether or not there's human contact with the surface.

The sensor values work fine on one analogPin at a time. Problem is, if I try to run 4 at the same time they read eachothers values.

I have created if statements, that activate if the sensor value is above 1020, but it again activates them all.

Is there any way to get around this, or create a for loop that reads the 4 pins individually, and turns them off in the loop when not used?

Thanks in advance.

Analog inputs are supposed to be driven by voltage sources of lowish impedance (10k or lower for
full accuracy on Uno and other ATmega based Arduinos).

If you have high impedance (basically floating) analog inputs, they will pick up interference
readily.

This is intrinsic to the thing you are measuring. I think you need some way to ground the inputs
you are not measuring, perhaps by setting all the pins but the one you are currently measuring to

pinMode (A0, OUTPUT)

To measure one of the pins, set its pinMode to INPUT, then do the analogRead(), then set it to OUTPUT
again.

MarkT:
Analog inputs are supposed to be driven by voltage sources of lowish impedance (10k or lower for
full accuracy on Uno and other ATmega based Arduinos).

If you have high impedance (basically floating) analog inputs, they will pick up interference
readily.

This is intrinsic to the thing you are measuring. I think you need some way to ground the inputs
you are not measuring, perhaps by setting all the pins but the one you are currently measuring to

pinMode (A0, OUTPUT)

To measure one of the pins, set its pinMode to INPUT, then do the analogRead(), then set it to OUTPUT
again.

So to understand you correctly, I could do something like this:

val1 = analogRead(analogPin);
val2 = analogRead(analogPin);


// Check val1
pinMode(pinVal1, INPUT);
pinMode(pinVal2, OUTPUT);
if(val1 > 1024){
// Something to complete
}

And then something similiar for val2 and so on. And that would let me read them independently?

No. You want to try

pinMode(A0, INPUT);
val0 = analogRead(A0);
pinMode(A0, OUTPUT);
//
pinMode(A1, INPUT);
val1 = analogRead(A1);
pinMode(A1, OUTPUT);
etc

So that at any time only the pin you are about to read is set to INPUT and the others are all OUTPUT. Once you've read them all you can do all your tests as normal.

Steve

Revenger96:
So to understand you correctly, I could do something like this:

val1 = analogRead(analogPin);

val2 = analogRead(analogPin);

// Check val1
pinMode(pinVal1, INPUT);
pinMode(pinVal2, OUTPUT);
if(val1 > 1024){
// Something to complete
}




And then something similiar for val2 and so on. And that would let me read them independently?

No, not at all. What is pinVal1, pinVal2 ? The pin you are reading is analogPin.
Make sure to use the generic names for analog pins, ie A0, A1, not 0, 1 (pinMode requires this).

Can you confirm which Arduino board you are using?

MarkT:
No, not at all. What is pinVal1, pinVal2 ? The pin you are reading is analogPin.
Make sure to use the generic names for analog pins, ie A0, A1, not 0, 1 (pinMode requires this).

Can you confirm which Arduino board you are using?

I think I managed to write that in a very confusing manner, sorry about that. This is what I came up with:

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

  // Sets the analogTouch inputs to "off".
  pinMode(analogTouch1, OUTPUT);
  pinMode(analogTouch2, OUTPUT);
  pinMode(analogTouch3, OUTPUT);
  pinMode(analogTouch4, OUTPUT);
}

  // Checks the current state of the analogInputs.
  //    AnalogPin1:
  pinMode(analogTouch1, INPUT);
  analogLamp1 = analogRead(analogTouch1);
  if(analogTouch1 > 1020){
      setState(1);
  }
  pinMode(analogTouch1, OUTPUT);
  
  //    AnalogPin2:
  pinMode(analogTouch2, INPUT);
  analogLamp2 = analogRead(analogTouch2);
  if(analogTouch2 > 1020){
      setState(2);
  }
  pinMode(analogTouch2, OUTPUT);

  //    AnalogPin3:
  pinMode(analogTouch3, INPUT);
  analogLamp3 = analogRead(analogTouch3);
  if(analogTouch3 > 1020){
      setState(3);
  }
  pinMode(analogTouch3, OUTPUT);

  //    AnalogPin4:
  pinMode(analogTouch4, INPUT);
  analogLamp4 = analogRead(analogTouch4);
  if(analogTouch4 > 1020){
      setState(4);
  }
  pinMode(analogTouch4, OUTPUT);

setState is a method I have created to set some specifics for some lights.

I'm using a Uno board.

Wait I think I understand what you mean, I am supposed to use A1 and so on instead of the int values i created. I assume my variables as such wont work?

int analogTouch1 = A1;
int analogTouch2 = A2;
int analogTouch3 = A3;
int analogTouch4 = A4;

should have written
pinMode(A1, OUTPUT);

Sorry about that.

You need to learn about arrays - when you have a bunch of identical things, use an array of them.

const byte analogTouch[4] = { A1, A2, A3, A4 };

void setup()
{
  for (byte i = 0 ; i < 4 ; i++)
    pinMode (analogTouch[i], OUTPUT) ;
  ...
}

A byte is plenty to represent a pin number.

MarkT:
You need to learn about arrays - when you have a bunch of identical things, use an array of them.

const byte analogTouch[4] = { A1, A2, A3, A4 };

void setup()
{
  for (byte i = 0 ; i < 4 ; i++)
    pinMode (analogTouch[i], OUTPUT) ;
  …
}




A byte is plenty to represent a pin number.

I wrote it up in here, and not in the arduino project. But you’re correct an arraylist would be a better choice. I am really only somewhat confident in Java, and trying by learning in different languages.

However, thanks for the help.

arraylist? There is no arraylist in C, its a very dumb array only.

Fair enough, didn’t know that for example. Thanks :slight_smile: