How do I add additional sensors?

hey I'm new here and have encountered a problem I can not solve. I want to add more sensors. how should I do?

const int VAL_PROBE = 0; // Analog pin 0
const int MOISTURE_LEVEL = 250; // the value after the LED goes ON
 
void setup() {
    Serial.begin(9600);
}
 
void LedState(int state) {
    digitalWrite(13, state);
}
 
void loop() {
    int moisture = analogRead(VAL_PROBE);
 
    Serial.println(moisture);
 
    if(moisture > MOISTURE_LEVEL) {
        LedState(HIGH);
    } else   {
        LedState(LOW);
    }
    delay(100);
}

I want to add more sensors. how should I do?

Add them to what? Deal with each sensor in a function. Move all the code in loop() to a function. Call the function from loop(). Add another function; add another call.

how do I do with the Serial print?

You print the value returned by the function PaulS told you to write.

my best friend is here too :smiley:

tobba__1990:
my best friend is here too :smiley:

That's great - maybe you can persuade her to work through some of the examples together.

I do not really understand? where can I read about how Serial println works how is it up to the computer how do I know which sensor it comes from?

how do I know which sensor it comes from?

How do you know which jar is which in the cupboard?
Simple, they have labels to tell you.

tobba__1990:
I do not really understand? where can I read about how Serial println works how is it up to the computer how do I know which sensor it comes from?

By reading a sensor then printing the value that it returns, preferably with a text label indicating what the value means.

Serial.print("Value from A0 : ";
Serial.println(analogRead(A0));

Thanks XD Now I'll try again and see if I succeed :slight_smile:

hey now I have a new problem, I want to pin 13 and 12 to activate my pump relay when the value is over 300 but it does not work?

int pumpPin1 = 12;
int pumpPin2 = 13;
int pumpLEDPin = 11;
int sensorPin1 = A1;
int sensorPin2 = A2;
int sensorPin3 = A3;
int sensorPin4 = A4;
int sensorPin5 = A5;
// variables
int sensorPinValue;
int sensorPin1Value;
int sensorPin2Value;
void setup(){
pinMode(pumpLEDPin, OUTPUT);
pinMode(pumpPin1,OUTPUT);
Serial.begin(9600);
}

void loop() 
{
sensorPinValue = analogRead(sensorPin1);
sensorPinValue = analogRead(sensorPin2);
Serial.print("Value from 1:");
Serial.println(analogRead(1));
Serial.print("Value from 2:");
Serial.println(analogRead(2));
sensorPinValue = analogRead(sensorPin3);
sensorPinValue = analogRead(sensorPin4);
sensorPinValue = analogRead(sensorPin5);
Serial.print("Value from 3:");
Serial.println(analogRead(3));
Serial.print("Value from 4:");
Serial.println(analogRead(4));
Serial.print("Value from 5:");
Serial.println(analogRead(5));
delay (5000);
{
if(sensorPin1Value >= 700)


digitalWrite(pumpPin1,HIGH);
delay(2000);
}

if ((sensorPin1Value < 700) && (sensorPin1Value >= 300))
{
delay(500);
digitalWrite(pumpPin1,LOW);
delay(600);

}
if(sensorPin2Value >= 700)
digitalWrite(pumpPin2,HIGH);
delay(2000);
if((sensorPin2Value < 700) && (sensorPin2Value >= 300))
{
delay(500);
digitalWrite(pumpPin2,LOW);
delay(600);
}

else digitalWrite(pumpPin2,HIGH);
{
delay(600);
}

}

You are testing sensorPin1Value but you never set it to anything. Copy and paste of sensorPinValue statements has bitten you badly.

I do not understand what you mean?

sensorPinValue = analogRead(sensorPin1);
sensorPinValue = analogRead(sensorPin2);
sensorPinValue = analogRead(sensorPin3);
sensorPinValue = analogRead(sensorPin4);
sensorPinValue = analogRead(sensorPin5);

The value of which pin is going to be in sensorPinValue?

int pumpPin1 = 12;
int pumpPin2 = 13;
int pumpLEDPin = 11;
int sensorPin1 = A1;
int sensorPin2 = A2;
int sensorPin3 = A3;
int sensorPin4 = A4;
int sensorPin5 = A5;
int sensorPinValue;
// variables
sensorPinValue = analogRead(sensorPin1);
sensorPinValue = analogRead(sensorPin2);
sensorPinValue = analogRead(sensorPin3);
sensorPinValue = analogRead(sensorPin4);
sensorPinValue = analogRead(sensorPin5);

I get the error message? where have I done wrong?

How many different sensorPins have you got?

And how many different sensorPinValues?

You get error messages because you're doing stuff that should be in a function outside of a function.
Or so it looks to me from the mere snippet you posted.

I have 5 sensors. I do not understand what you mean? this is the original code I wrote.

int pumpPin1 = 2;
int pumpPin2 = 3;
int pumpLEDPin = 11;
int sensorPin1 = A1;
int sensorPin2 = A2;
int sensorPin3 = A3;
int sensorPin4 = A4;
int sensorPin5 = A5;
// variables
int sensorPinValue;
int sensorPin1Value;
int sensorPin2Value;
void setup(){
pinMode(pumpLEDPin, OUTPUT);
pinMode(pumpPin1,OUTPUT);
Serial.begin(9600);
}

void loop() 
{
sensorPinValue = analogRead(sensorPin1);
sensorPinValue = analogRead(sensorPin2);
Serial.print("Value from 1:");
Serial.println(analogRead(1));
Serial.print("Value from 2:");
Serial.println(analogRead(2));
sensorPinValue = analogRead(sensorPin3);
sensorPinValue = analogRead(sensorPin4);
sensorPinValue = analogRead(sensorPin5);
Serial.print("Value from 3:");
Serial.println(analogRead(3));
Serial.print("Value from 4:");
Serial.println(analogRead(4));
Serial.print("Value from 5:");
Serial.println(analogRead(5));
delay (5000);
if(sensorPinValue >= 700)
{
delay(5000);
digitalWrite(pumpPin1,LOW);
}

else if((sensorPin1Value < 700) && (sensorPin1Value >= 300))
{
delay(500);
digitalWrite(pumpPin1,HIGH);
delay(600);
}

else digitalWrite(pumpPin1,LOW);
{
delay(600);
}

if((sensorPin2Value < 700) && (sensorPin2Value >= 300))
{
delay(500);
digitalWrite(pumpPin2,HIGH);
delay(600);
}

else digitalWrite(pumpPin2,LOW);
{
delay(600);
}

}

Look at these lines, which PaulS already suggested you examine...

sensorPinValue = analogRead(sensorPin1);
sensorPinValue = analogRead(sensorPin2);
Serial.print("Value from 1:");
Serial.println(analogRead(1));
Serial.print("Value from 2:");
Serial.println(analogRead(2));
sensorPinValue = analogRead(sensorPin3);
sensorPinValue = analogRead(sensorPin4);
sensorPinValue = analogRead(sensorPin5);

You have only one variable, sensorPinValue, but you do analogRead() from 5 different pins. So as soon as you store sensorPin1's reading in sensorPinValue, you immediately overwrite that with the reading from sensorPin2; and so on until the only value you have stored is the last one in the list, ie sensorPin5.

You need a few more variables, perhaps sensorPinValue1, sensorPinValue2 etc.

Or perhaps more sensibly, an array.