Created
November 27, 2018 20:09
-
-
Save pudelosha/a072c389250d9554ca0079b5494632ea to your computer and use it in GitHub Desktop.
Plant Watering Machine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SimpleDHT.h> | |
#define readingInterval 1000 // change this later | |
#define readingLimit 10 // change later | |
int pinMoistureSensor1 = A0; | |
int pinMoistureSensor2 = A1; | |
int pinPump = 7; | |
int pinDHT22 = 6; | |
int readingSum1 = 0; | |
int readingSum2 = 0; | |
int readingCount = 0; | |
unsigned long previousTime = 0; | |
SimpleDHT22 dht22; | |
void setup() { | |
pinMode(pinPump, OUTPUT); | |
pinMode(pinDHT22, INPUT); | |
digitalWrite(pinPump, HIGH); | |
Serial.begin(9600); | |
} | |
void loop() { | |
unsigned long currentTime = millis(); | |
float temperature = 0; | |
float humidity = 0; | |
int err = SimpleDHTErrSuccess; | |
if ((err = dht22.read2(pinDHT22, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) { | |
Serial.print("Read DHT22 failed, err="); Serial.println(err);delay(2000); | |
return; | |
} | |
Serial.print("Current temperature: "); | |
Serial.println(temperature); | |
Serial.print("Current humidity: "); | |
Serial.println(humidity); | |
Serial.print("Current time: "); | |
Serial.println(currentTime); | |
if(currentTime - previousTime > readingInterval) { | |
previousTime = currentTime; | |
Serial.print("Current time changed to: "); | |
Serial.println(previousTime); | |
readingSum1 += analogRead(pinMoistureSensor1); | |
readingSum2 += analogRead(pinMoistureSensor2); | |
readingCount++; | |
if(readingCount >= readingLimit) { | |
int averageMoisture1 = ceil(readingSum1 / readingCount); | |
int averageMoisture2 = ceil(readingSum2 / readingCount); | |
Serial.print("Moisture 1: "); | |
Serial.println(averageMoisture1); | |
Serial.print("Moisture 2: "); | |
Serial.println(averageMoisture2); | |
// Dry: (520 430] | |
// Wet: (430 350] | |
// Water: (350 260] | |
if (averageMoisture1 > 500 || averageMoisture2 > 500) { | |
digitalWrite(pinPump, LOW); | |
Serial.println("Punping water!"); | |
delay(5000); | |
digitalWrite(pinPump, HIGH); | |
} | |
readingSum1 = 0; | |
readingSum2 = 0; | |
readingCount = 0; | |
} | |
} | |
delay(500); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment