Paso 5: El código
El bucle void comprueba las sondas cada 60 minutos y si la tierra está seca inicia la función waterOn que encienda la bomba hasta alcanzar el alto nivel de humedad o pasados 5 segundos.
Si nivel húmedo llega a la bomba se apaga y vuelve al bucle vacío, si 5 segundos pasados y el nivel no lo alcanzaron luego se apaga la bomba y abrir y cerrar error (función pumpErr) hasta cercanías de nivel húmedo (riego manual)
Nota: Los comandos de comunicación serial fueron agregados por razones de depuración y pueden descartarse.
/* The AutoPlant reads the humidity level through 2 probes, one sends 5V pulse through the ground (Digital Pin 8) the outher reads the pulse from the ground (A3), the weter the ground the more pulse is transfered through it. when the ground is dry the AutoPlant turns a 5V pump to add water to the plant (through Digital Pin 9) */int probeVal;#define wetnessLow 500#define wetnessHigh 800#define waterTime 5000
void setup() { pinMode (13, OUTPUT); /* Led Indecator */ pinMode (9, OUTPUT); /* Pump on/off */ pinMode (8, OUTPUT); /* Probe Transmiter */ pinMode (A3, INPUT); /* Probe Reciver */ Serial.begin (9600);}
void loop() { Serial.println ("loop"); digitalWrite (8, HIGH); probeVal = analogRead (A3); digitalWrite (8, LOW); if (probeVal <= wetnessLow) { waterOn(); } delay (60000);}
void waterOn() { Serial.println ("waterOn"); digitalWrite (13, HIGH); digitalWrite (9, HIGH); digitalWrite (8, HIGH); long Time = (millis()); long pumpTime = millis(); /* Pump on, fills the water until probe reciver matches waternessHigh */ while (analogRead (A3) <=wetnessHigh) { if (millis() == Time+700) { digitalWrite (13, !digitalRead (13)); Time = millis(); } /* If no matching of the waternessHigh until waterTime runs the pumpErr function */ if (millis() >= pumpTime + waterTime){ Serial.println ("iffff"); pumpErr() ; } } digitalWrite (13, LOW); digitalWrite (9, LOW); digitalWrite (8, LOW);}
void pumpErr() { Serial.println ("pump Err"); digitalWrite (9, LOW); while (analogRead (A3) <=wetnessHigh) { digitalWrite (13, HIGH); delay (200); digitalWrite (13,LOW); delay (200); digitalWrite (13, HIGH); delay (200); digitalWrite (13,LOW); delay (1000); }}