Multi-Tasking?

Hi, I’ve had my Arduino a couple of days now so fairly new to this. I’ve just put my first project together which is plant moisture level sensor, which turns on a relay to control a pump once the moisture level is too low…but…what I want to do now is use the Arduino to monitor two moisture sensors (one for each plant) and also control a second relay/pump when the second plant becomes dry i.e. water two plants independently. I’ve written this so far, which works for one sensor/replay/pump but I’m unsure if the Arduino is even capable of multi-tasking? There are loads of inputs, i’m just not sure how (if it’s at all possible) to write the code so that it’ll do different outputs for each of the sensors. Anyway, here’s what I’ve written so far:

int sensorPin = 0;

int redPin2=2;

int redPin3=3;

int yellowPin4=4;

int yellowPin5=5;

int greenPin6=6;

int greenPin7=7;

int waterPump = 8;

int sensorValue=0;

void setup()

{

Serial.begin(9600);

pinMode(redPin2, OUTPUT);

pinMode(redPin3, OUTPUT);

pinMode(yellowPin4, OUTPUT);

pinMode(yellowPin5, OUTPUT);

pinMode(greenPin6, OUTPUT);

pinMode(greenPin7, OUTPUT);

pinMode (waterPump, OUTPUT);

}

void loop()

{

sensorValue = analogRead(sensorPin);

Serial.println(sensorValue);

delay(250);

if (sensorValue <= 400) // VERY WET

{

digitalWrite(greenPin7, HIGH);

digitalWrite(greenPin6, LOW);

digitalWrite(yellowPin5, LOW);

digitalWrite(yellowPin4, LOW);

digitalWrite(redPin3, LOW);

digitalWrite(redPin2, LOW);

}

else if (sensorValue <=500) // WET

{

digitalWrite(greenPin7, LOW);

digitalWrite(greenPin6, HIGH);

digitalWrite(yellowPin5, LOW);

digitalWrite(yellowPin4, LOW);

digitalWrite(redPin3, LOW);

digitalWrite(redPin2, LOW);

}

else if (sensorValue <=600) // MOIST

{

digitalWrite(greenPin7, LOW);

digitalWrite(greenPin6, LOW);

digitalWrite(yellowPin5, HIGH);

digitalWrite(yellowPin4, LOW);

digitalWrite(redPin3, LOW);

digitalWrite(redPin2, LOW);

}

else if (sensorValue <=700) // MILDY MOIST

{

digitalWrite(greenPin7, LOW);

digitalWrite(greenPin6, LOW);

digitalWrite(yellowPin5, LOW);

digitalWrite(yellowPin4, HIGH);

digitalWrite(redPin3, LOW);

digitalWrite(redPin2, LOW);

}

else if (sensorValue <=800) // GETTING DRY

{

digitalWrite(greenPin7, LOW);

digitalWrite(greenPin6, LOW);

digitalWrite(yellowPin5, LOW);

digitalWrite(yellowPin4, LOW);

digitalWrite(redPin3, HIGH);

digitalWrite(redPin2, LOW);

}

else // DRY

{

digitalWrite(greenPin7, LOW);

digitalWrite(greenPin6, LOW);

digitalWrite(yellowPin5, LOW);

digitalWrite(yellowPin4, LOW);

digitalWrite(redPin3, LOW);

digitalWrite(redPin2, HIGH);

digitalWrite(waterPump, HIGH); // Pump on for 10 Seconds

delay(10000);

digitalWrite(waterPump, LOW); // Pump off for 1 Second

delay(1000);

}

}

You just need to poll all of the sensors in the main loop.

The code to handle the SensorData could be in a function so that the main loop has less code.

Try to avoid software delays and maybe use a hardware timer to produce a ‘system tick’ that the main loop uses to know when to poll inputs. There is some info on the web on how to do Multitasking with small micro-controllers. There are many ways to do this.

You need to familiarize yourself with the Blink Without Delay example in the IDE.

Also Google “arduino state machine.”

in your code you do this;

  1. read sensor

  2. make shiny led light up according to sensor value

  3. add water to plant if dry.

if you want to have more plants, how about doing this:

1.read first sensor

2.read second sensor

  1. umm, shiny leds maybe.

  2. act on first sensor if dry

  3. act on second sensor if dry.

i belive the whole program to take less than a thousand of a second to run, so i belive your plant will not dry out if it have to wait a second or two.

:slight_smile:

If you need more help in how to code this, just ask :slight_smile: