Stap 5: De Code
De void lus controleert de sondes elke 60 minuten en als de grond droog begint de waterOn functie, die de pomp inschakelt totdat de vochtige hoog is bereikt of 5 seconden verledens.
als vochtige niveau schakelt pomp bereikt en terug naar de void loop, gaat als 5 seconden verledens en het niveau niet bereikt dan schakelt pomp en blink fout (pumpErr functie) tot vochtige niveau reched (handmatige drenken)
Opmerking: De seriële communicatie opdrachten voor foutopsporing redenen werden toegevoegd en kunnen worden geschrapt.
/* 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); }}