multiple analog inputs

Hi Guys,

We are working on a project where we are using an arduino uno .
we use 2 analog inuput and get 2 digital output but this program was run in 1 analog input to get 1digital that was working perfectly. but the same time using 2 analog input but that time sensing not perfect please tell the rectified to this program .this is code.

int analoginpin = A1;  // Analog input pin that the potentiometer is attached to 
int analoginpin1 = A2;  // Analog input pin that the potentiometer is attached to

int out =12;
int outt =13;
int sensorValue = 0;    // value read from the pot
int sensorValue1 = 0;   // value read from the pot

void setup () { 
  // intialize serial communications at 9600 bps:
  Serial.begin(9600);
  pinMode(out, OUTPUT);
  pinMode(outt, OUTPUT);
  
}
void loop (){
  // read the analog in value :
  sensorValue = analogRead(analoginpin);
  sensorValue = analogRead(analoginpin1);

  Serial.print("sensor = ");
  Serial.println(sensorValue);
  Serial.println(sensorValue1);

  delay(500);
  if (sensorValue>700)
  {
    digitalWrite(out,1);
  }
    else
    {
      digitalWrite (out,0);
    }
     if (sensorValue1>300)
  {
    digitalWrite(outt,1);
  }
    else
    {
      digitalWrite (outt,0);
    }
}
  sensorValue = analogRead(analoginpin);
  sensorValue = analogRead(analoginpin1);

Both analog reads change the same variable. Is that what you want.

For noisy inputs you can try putting a 0.1uf ceramic capacitor between the analog input and ground, that may help. And/or read each analog input twice and discard the first result.

i try this but inputs are twisted output was not on correct time

groundFungus told you TWO things and the response was "i try this". Tried WHAT? The first thing? The second thing? Both? Neither?

The names of the variables are bound to cause trouble. For example, out does not really explain anything and is not very different from outt.

Good Luck!

If you make changes to your code you should post the new code so that we don't have to guess at what you have changed.

i try this but inputs are twisted output was not on correct time

I don't know what that means.

Besides the thing that groundFungus pointed out, I have also come to understand that when you analogRead from multiple different analog pins, you should discard the first value you read from a pin thats not the same pin you read last time.

And if you want more robust readings, you should take multiple readings and calculate an average:

#define ANALOG_READINGS 5
#define ANALOG_READ_DELAY 10

int analogAverage(byte pin)
{
  unsigned int r = 0;
  for (byte i = 0; i < ANALOG_READINGS; i++)
  {
    r += analogRead(pin);
    delay(ANALOG_READ_DELAY);
  }
  return r / ANALOG_READINGS;
}

You could also do this with a cyclic buffer which would only require one new reading (combined with 4 previous readings) to get an average :slight_smile: