Hi all,
I am fairly new to coding and I have given it a good bash,
but I need help with conditions, I'm trying to make the following work
- Day/Light sensor to turn water feature on and off (Millis to allow period between light cut off point to prevent rapid switching of the relay)
- Soil sensor, control watering of plants (need a buffer period so watering is triggered when below 33% and continues until 66% but not triggered again until 33%).
- PIR to work with Day Light sensor so only triggers lighting when it is night time and there is motion, and allow at least 25 seconds after last motion is detected.
Misting light and water feature to be triggered when it is night, and motion is detected. - LCD display, showing current Temp, Humidity, index, soil wetness, when Motion triggered and motion stopped.
- symbol showing light detected
Thanks in advance!
// include the library code:
#include <LiquidCrystal.h> //for the display
#include "DHT.h" //lib for the temperature and humidity sensor
#define DHTPIN 8 // what pin we're connected to Temp & Humid
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor for normal 16mhz Arduino
#include <math.h> //to work out percentages
const int relay1 = 30; //fixed relay Pins Plant watering relay
const int relay2 = 31; //fixed relay Pins Lights on relay
const int relay3 = 32; //fixed relay Pins waterfeature relay
const int relay4 = 33;//fixed relay Pins Misting unit relay
int sensorValue = 0;
const int ltsensorPin = 9; //lightsensor pin
int ml = 0; //moisture sensor converted to percentage value
int lt = 0;
int ds = LOW;
int light = LOW;
int ltsensorValue = 0;
long previousMillis1 = 0; // will store last time LED was updated
long previousMillis2 = 0;
long previousMillis3 = 0;
long previousMillis4 = 0;
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval1 = 20000; // interval at which to relay1 on time (milliseconds)
long interval2 = 15000; // interval at which to relay2 on time (milliseconds)
long interval3 = 2000; // interval for lcd readings
long intervallt = 10000;
byte thermo[8] = //icon for termometer
{
B00100,
B01010,
B01010,
B01110,
B01110,
B11111,
B11111,
B01110
};
byte hydro[8] = //icon for water droplet
{
B00100,
B00100,
B01010,
B01010,
B10001,
B10001,
B10001,
B01110,
};
byte plant[8] = //icon for Plant moisture level
{
0b10101,
0b01110,
0b11011,
0b01110,
0b10101,
0b00100,
0b11111,
0b01110
};
byte sun[8] = {
B00100,
B10101,
B01110,
B01010,
B11011,
B01110,
B10101,
B00100
};
// initialize thelibrary with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int ledPin =13;
/*
/////////////////////////////
//VARS
int calibrationTime = 30; //the time we give the sensor to calibrate itself (10-60 secs according to the datasheet)
long unsigned int lowIn; //the time when the sensor outputs a low impulse
long unsigned int pause = 5000; //the amount of milliseconds the sensor has to be low before we assume all motion has stopped
boolean lockLow = true;
boolean takeLowTime;
int pirPin = 7; //the digital pin connected to the PIR sensor's output
/////////////////////////////
//SETUP
void setup(){
lcd.createChar(0,thermo);
lcd.createChar(1, hydro);
lcd.createChar(2, plant);
lcd.createChar(3, sun);
pinMode(ltsensorPin, INPUT);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
pinMode(ltsensorPin, INPUT);
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);
digitalWrite(relay4, HIGH);
int sensorValue = analogRead(A0); // map it to the range of the analog out:
int ltsensorValue = digitalRead(9);
lcd.begin(16,2);
lcd.clear();
Serial.begin(9600);
dht.begin();
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(pirPin, LOW);
//give the sensor some time to calibrate
Serial.print("calibrating sensor ");
lcd.setCursor(0,0);
lcd.print("Calibrating ");
lcd.setCursor(1,1);
for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
lcd.print(".");
delay(1000);
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Done ");
lcd.setCursor(0,1);
lcd.print("SENSORS ACTIVE");
delay(50);
}
int rs1 = HIGH;
int rs2 = HIGH;
int rs3 = HIGH;
int rs4 = HIGH;
int ds1 = LOW; //display wait & clear
////////////////////////////
//LOOP
void loop(){
sensorValue = analogRead(A0);
unsigned long currentMillis = millis();
ltsensorValue = digitalRead(ltsensorPin);
if(digitalRead(ltsensorPin) == HIGH){
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);
}
if(currentMillis - previousMillis4> intervallt){
previousMillis4 = currentMillis;
if(digitalRead(ltsensorPin) == LOW){
digitalWrite(relay2, LOW);
digitalWrite(relay3, LOW);
}
}
if(digitalRead(pirPin) == HIGH){
digitalWrite(ledPin, HIGH); //the led visualizes the sensors output pin state
if(lockLow){
lockLow = false; //make sure we wait for a transition to LOW before any further output is made
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis()/1000);
Serial.println(" sec");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("motion detected at ");
lcd.print(" at ");
lcd.setCursor(0,1);
lcd.print(millis()/1000);
lcd.print(" sec");
digitalWrite(relay1, LOW);
}
takeLowTime = true;
}
if(digitalRead(pirPin) == LOW){
digitalWrite(ledPin, LOW); //the led visualizes the sensors output pin state
if(takeLowTime){
lowIn = millis(); //save the time of the transition from high to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
}
if(!lockLow && millis() - lowIn > pause){ //if the sensor is low for more than the given pause, we assume that no more motion is going to happen
lockLow = true; //makes sure this block of code is only executed again after a new motion sequence has been detected
Serial.print("motion ended at "); //output
Serial.print((millis() - pause)/1000);
Serial.println(" sec");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("motion Stopped");
lcd.setCursor(0,1);
lcd.print(" at ");
lcd.print(millis()/1000);
lcd.print(" sec");
digitalWrite(rs1,LOW);
if(currentMillis - previousMillis1 > interval1){
previousMillis1 = currentMillis;
rs1 = HIGH;
digitalWrite(relay1, rs1);
}
}
}
if (currentMillis - previousMillis3 > interval3){
previousMillis3 = currentMillis;
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius
float t = dht.readTemperature();
// Read temperature as Fahrenheit
float f = dht.readTemperature(true);
ml = map(sensorValue, 1023, 0, 0, 100);
lt = map(ltsensorValue,1, 0, 0, 1);
// Check if any reads failed and exit early (to try again).
// if (isnan(h) || isnan(t) || isnan(f)) {
// Serial.println("Failed to read from DHT sensor!");
// return;
// Compute heat index
// Must send in temp in Fahrenheit!
float hi = dht.computeHeatIndex(f, h);
lcd.clear();
lcd.setCursor(0, 0);
lcd.write(byte(1));
lcd.print((h),0);
lcd.setCursor(4,0);
lcd.print("ind ");
lcd.print(hi);
lcd.print("%");
lcd.setCursor(0,1);
lcd.write(byte(0));
lcd.print((t),0);
lcd.print((char)223);
lcd.print("C ");
lcd.setCursor(6,1);
lcd.write(byte(2));
lcd.print(ml);
lcd.print("%");
if(ml < 33){
digitalWrite(relay4, HIGH);
}
else
{
digitalWrite(relay4, LOW);
}
if(lt == 1){
lcd.setCursor(10,1);
lcd.write(byte(3));
}
else {
lcd.setCursor (10,1);
lcd.print(" ");
}
}
}