Étape 5: Le Code
La boucle Sub vérifie les sondes toutes les 60 minutes et si le sol est sec démarre la fonction waterOn qui allume la pompe jusqu'à atteindre le haut niveau humide ou passés de 5 secondes.
si humide niveau atteint s’arrête la pompe et remonte à la boucle Sub, si 5 secondes passés et le niveau n’ont pas atteint puis désactive la pompe et le clignotement erreur (fonction pumpErr) humide reched niveau (arrosage manuel)
Remarque : Les commandes de communication série ont été ajoutés pour des raisons de débogage et peuvent être supprimées.
/* 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); }}