Go Down

Topic: Trying to code a pump using an input from a moisture sensor  (Read 119 times) previous topic - next topic

CrazyPharaoh

Hi there,
I'm in the middle of doing a project which is an automatic watering system for a plant pot. I've got all of the circuits sorted but now that I'm onto the coding, im sort of confused...

So with the moisture sensor I've calibrated it so I know at what value the plant needs watering, at what value it is at maximum dryness and maximum wetness and at what value it should stop watering again (so that the soil isn't waterlogged).

The max dryness value is around 750 and wetness at 150.

The way I want it to work is for the code to keep checking wether the dryness gets to a value bigger than 400. If it is then I want it to start watering until it's reading a value of 200 then to stop watering and repeat the cycle.

I'm not sure how to code this but I'm sure there's a simple way of doing it.

Thanks in advance for your help!
Ali

Perehama

Code: [Select]
void setup() {
// here's where you setup the pin modes for your sensor, other inputs and outputs etc.
}

void loop() {
if (sensorValue() >= 400) wateringOff();
if (sensorValue() <= 200) wateringOn();
}

int sensorValue() {
int x = 0;
// where you read the pin value into x
return x;
}

void wateringOff() {
// where you do what you need to turn off the water
}

void wateringOn() {
// where you do what you need to turn on the water
)
The slender snake hissed as she slithered slowly over the rocks, grass and leaves.

CrazyPharaoh

Code: [Select]
void setup() {
// here's where you setup the pin modes for your sensor, other inputs and outputs etc.
}

void loop() {
if (sensorValue() >= 400) wateringOff();
if (sensorValue() <= 200) wateringOn();
}

int sensorValue() {
int x = 0;
// where you read the pin value into x
return x;
}

void wateringOff() {
// where you do what you need to turn off the water
}

void wateringOn() {
// where you do what you need to turn on the water
)



Code: [Select]
int pump = 8;
int LED = 7;

void setup() {
pinMode(pump, OUTPUT);
pinMode(LED, OUTPUT);
}

void loop() {
  int sensorValue = analogRead(A0);
}
if (sensorValue() >= 400) wateringOff();
if (sensorValue() <= 200) wateringOn();*****************************
}


void wateringOff() {
// where you do what you need to turn off the water
digitalWrite(pump, LOW)
digitalWrite(LED, LOW);
}

void wateringOn() {
// where you do what you need to turn on the water
digitalWrite(pump, HIGH);
digitalWrite(LED, HIGH);
)


Using this code, I get an error message saying "Expected unqualified-id before 'if'" (I've put stars on the highlighted line. :/

PaulS

You can not have function calls outside of a function. Pay attention to where loop() ends. You fat-fingered the code that perehama posted.
The art of getting good answers lies in asking good questions.

Perehama

Using this code, I get an error message saying "Expected unqualified-id before 'if'" (I've put stars on the highlighted line. :/
If you have nothing but analogRead() as your condition, then your loop() should be as follows:
Code: [Select]
void loop() {
if (analogRead(A0) >= 400) wateringOff();
if (analogRead(A0) <= 200) wateringOn();
}
The slender snake hissed as she slithered slowly over the rocks, grass and leaves.

Go Up