Input output issues

Hi, I am trying to get a single led out of five, based on inputs from analog pins. The code that I have presently when one of the analog inputs triggers all of the leds come on instead one dealing with the analog pin. Any help would be helpful.

void loop() {
int sensorValue = analogRead(waterSensor1);
int sensorValue2 = analogRead(waterSensor2);
int sensorValue3 = analogRead(waterSensor3);
int sensorValue4 = analogRead(waterSensor4);
int sensorValue5 = analogRead(waterSensor5);

Serial.println("Sensor 1");
Serial.println(sensorValue);
if (sensorValue < waterTheshold1) {
Serial.println("Water found in basement! Station 1");
digitalWrite(waterAlert1, HIGH);
} else {
digitalWrite(waterAlert1, LOW);
}

Serial.println("Sensor 2");
Serial.println(sensorValue2);
if (sensorValue2 < waterTheshold1) {
Serial.println("Water found in basement! Station 2");
digitalWrite(waterAlert2, HIGH);
} else {
digitalWrite(waterAlert2, LOW);
}

Serial.println("Sensor 3");
Serial.println(sensorValue3);
if (sensorValue3 < waterTheshold1) {
Serial.println("Water found in basement! Station 3");
digitalWrite(waterAlert3, HIGH);
} else {
digitalWrite(waterAlert3, LOW);
}

Serial.println("Sensor 4");
Serial.println(sensorValue4);
if (sensorValue4 < waterTheshold1) {
Serial.println("Water found in basement! Station 4");
digitalWrite(waterAlert4, HIGH);
} else {
digitalWrite(waterAlert4, LOW);
}

Serial.println("Sensor 5");
Serial.println(sensorValue5);
if (sensorValue5 < waterTheshold1) {
Serial.println("Water found in basement! Station 5");
digitalWrite(waterAlert5, HIGH);
} else {
digitalWrite(waterAlert5, LOW);
}

Always show us your ‘current’ complete sketch.
Use CTRL T to format the sketch.
Please use code tags.
Use the </> icon in the posting menu.

[code] Paste sketch here. [/code]

A schematic, a parts list, and a photo of the wiring would also be helpful.

When you feel comfortable with it, using array(s) would shorten your program considerably.

This sort of thing may give anomalous results

int sensorValue = analogRead(waterSensor1);
  int sensorValue2 = analogRead(waterSensor2);
  int sensorValue3 = analogRead(waterSensor3);
  int sensorValue4 = analogRead(waterSensor4);
  int sensorValue5 = analogRead(waterSensor5);

because the Arduino has only one ADC and when it is switched to another pin there may be a residue of voltage from the previous pin.

If you experience a problem you should read each pin twice and discard the first value - like this

int sensorValue = analogRead(waterSensor1);
sensorValue = analogRead(waterSensor1);

...R

When you display the analog values you get, do they all go below the threshold at the same time?

That would tend to support Robin2's suggestion that the impedance of the analog inputs is not low enough to properly get the sample-hold capacitor to the desired level before the D/A does the conversion. What are you using for sensors? Do they have a pull-up or pull-down resistor on each analog pin?

Hello everyone so the analog values do change for each analog pull-up pin in the Serial Monitor, but what I have noticed is the print does not change like the code states. The full codes is below which it still does not make sense why that all of the leds kick on if one of the Analog pins gets below 500.

int waterTheshold1 = 500;
int waterSensor1 = A0;
int waterSensor2 = A1;
int waterSensor3 = A2;
int waterSensor4 = A3;
int waterSensor5 = A4;
const int waterAlert1 = 2;
const int waterAlert2 = 3;
const int waterAlert3 = 4;
const int waterAlert4 = 5;
const int waterAlert5 = 6;

void setup() {
// put your setup code here, to run once:
pinMode(waterSensor1, INPUT_PULLUP);
pinMode(waterSensor2, INPUT_PULLUP);
pinMode(waterSensor3, INPUT_PULLUP);
pinMode(waterSensor4, INPUT_PULLUP);
pinMode(waterSensor5, INPUT_PULLUP);
pinMode(waterAlert1, OUTPUT);
pinMode(waterAlert2, OUTPUT);
pinMode(waterAlert3, OUTPUT);
pinMode(waterAlert4, OUTPUT);
pinMode(waterAlert5, OUTPUT);
waterAlert1 == LOW;
waterAlert2 == LOW;
waterAlert3 == LOW;
waterAlert4 == LOW;
waterAlert5 == LOW;

Serial.begin(9600);
}

void loop() {
int sensorValue = analogRead(waterSensor1);
int sensorValue2 = analogRead(waterSensor2);
int sensorValue3 = analogRead(waterSensor3);
int sensorValue4 = analogRead(waterSensor4);
int sensorValue5 = analogRead(waterSensor5);

Serial.println("Sensor 1");
Serial.println(sensorValue);
if (sensorValue < waterTheshold1) {
Serial.println("Water found in basement! Station 1");
digitalWrite(waterAlert1, HIGH);
} else {
digitalWrite(waterAlert1, LOW);
}

Serial.println("Sensor 2");
Serial.println(sensorValue2);
if (sensorValue2 < waterTheshold1) {
Serial.println("Water found in basement! Station 2");
digitalWrite(waterAlert2, HIGH);
} else {
digitalWrite(waterAlert2, LOW);
}

Serial.println("Sensor 3");
Serial.println(sensorValue3);
if (sensorValue3 < waterTheshold1) {
Serial.println("Water found in basement! Station 3");
digitalWrite(waterAlert3, HIGH);
} else {
digitalWrite(waterAlert3, LOW);
}

Serial.println("Sensor 4");
Serial.println(sensorValue4);
if (sensorValue4 < waterTheshold1) {
Serial.println("Water found in basement! Station 4");
digitalWrite(waterAlert4, HIGH);
} else {
digitalWrite(waterAlert4, LOW);
}

Serial.println("Sensor 5");
Serial.println(sensorValue5);
if (sensorValue5 < waterTheshold1) {
Serial.println("Water found in basement! Station 5");
digitalWrite(waterAlert5, HIGH);
} else {
digitalWrite(waterAlert5, LOW);
}

delay(300); // delay in between reads for stability
}

These are not necessary as you are using A0-A5 and reading analog inputs not digital inputs:

pinMode(waterSensor1, INPUT_PULLUP);
pinMode(waterSensor2, INPUT_PULLUP);
pinMode(waterSensor3, INPUT_PULLUP);
pinMode(waterSensor4, INPUT_PULLUP);
pinMode(waterSensor5, INPUT_PULLUP);

Do you know the difference between = and ==

No, No, No!
waterAlert1 == LOW;
waterAlert2 == LOW;
waterAlert3 == LOW;
waterAlert4 == LOW;
waterAlert5 == LOW;

Show us a good schematic of your circuit.
Show us a good image of your wiring.
Give links to components. Posting images:
https://forum.arduino.cc/index.php?topic=519037.0

BTW
In the future, please use CTRL T to format the sketch.
Please use code tags.
Use the </> icon in the posting menu.

 Paste sketch here.

larryd, based on the documentation that is out there https://www.arduino.cc/en/Tutorial/AnalogInputPins in order to use the Analog pins as pullup inputs they have to declared. If they are not declared then the values go from 0 to 1043 which causes more issues.

Shadow29:
larryd, based on the documentation that is out there https://www.arduino.cc/en/Tutorial/AnalogInputPins in order to use the Analog pins as pullup inputs they have to declared. If they are not declared then the values go from 0 to 1043 which causes more issues.

At power up, the analog inputs can be read as analog inputs as is.
Turning pull-ups on will give unexpected readings.

You may want to take another look at that link.
“Be aware however that turning on a pull-up will affect the values reported by analogRead().”

We have asked you to show us your wiring and a schematic, I guess we are being ignored.
Not very nice.

larryd - if I had it available it would be uploaded. But I don't as for your comment

larryd:
At power up, the analog inputs can be read as analog inputs as is.
Turning pull-ups on will give unexpected readings.

You may want to take another look at that link.
“Be aware however that turning on a pull-up will affect the values reported by analogRead().”

We have asked you to show us your wiring and a schematic, I guess we are being ignored.
Not very nice.

Based on my tests the pullup resistor is more reliable than just doing the analog which is more variable and causes false results because it goes from 0 to 1043 (looking at the serial monitor). Also based on a question that I had in another forum using the pull up was suggested - Arduino Nano using Ground to Pins - General Electronics - Arduino Forum

Robin2 and johnwasser, I will see about the suggestions that you provided. I am also doing some side tests as well to see what I get. I sort of did not answer you fully but the serial monitor does show each of the analog pins dropping to below 500 as I touch ground to them. But I have noticed that the phase does not change it is always saying "Water found in basement! Station 1" but shows that Sensor 4 is below 500 even though Sensor 1 is above 501.

We assume you have a circuit like the one in image #2.

Highly suggest you use the image #1.

Shadow29:
larryd - if I had it available it would be uploaded.

If that refers to your wiring diagram or schematic then get out a pencil and paper and make one. An post a photo of it.

...R

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile: