Greetings people, since im back to my project of home semi-automations i was wondering if anyone can help me pass the delay() with millis() to my pumps. Since i'm aiming for a safe automation, my main target is to keep the system under control, to help prevent disasters from newbie users.
Now im using an arduino uno, connected to a DHT11 hum/temp sensor, a 16ch 5v relay to switch devices etc. bellow is the code, but i haven't any luck while i was reading others posts, since i find it difficult to edit the code as im using the "case" to use serial commands for actions.
#include <dht.h>
#define DHT11_PIN 7
dht DHT;
int userStart = 0;
//define the pump pins and time
int p1 = A0; // pin that turns on the motor
int p2 = 2;
int p3 = 3;
int p4 = 4;
int p5 = 5;
int p6 = 6;
int p7 = A1;
int p8 = 8;
int p9 = 9;
int p10 = 10;
int p11 = 11;
int p12 = 12;
int p13 = 13;
int watertime = 10; // how long to water in seconds
int waittime = 60; // how long to wait between watering, in minutes
void setup() {
pinMode(p1, OUTPUT);
pinMode(p2, OUTPUT);
pinMode(p3, OUTPUT);
pinMode(p4, OUTPUT);
pinMode(p5, OUTPUT);
pinMode(p6, OUTPUT);
pinMode(p7, OUTPUT);
pinMode(p8, OUTPUT);
pinMode(p9, OUTPUT);
pinMode(p10, OUTPUT);
pinMode(p11, OUTPUT);
pinMode(p12, OUTPUT);
pinMode(p13, OUTPUT);
digitalWrite(p1, HIGH);
digitalWrite(p2, HIGH);
digitalWrite(p3, HIGH);
digitalWrite(p4, HIGH);
digitalWrite(p5, HIGH);
digitalWrite(p6, HIGH);
digitalWrite(p7, HIGH);
digitalWrite(p8, HIGH);
digitalWrite(p9, HIGH);
digitalWrite(p10, HIGH);
digitalWrite(p11, HIGH);
digitalWrite(p12, HIGH);
digitalWrite(p13, HIGH);
// Setup serial plotter.
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
switch(Serial.read())
{
case '1':
Serial.print("pump 1 is ON");
Serial.print('\n');
digitalWrite(p1, LOW); // turn on the motor
delay(watertime*1000); // multiply by 1000 to translate seconds to milliseconds
digitalWrite(p1, HIGH); // turn off the motor
Serial.print('\n');
Serial.print("pump 1 is OFF");
break;
case '2':
Serial.flush();
Serial.print("pump 2 is ON");
digitalWrite(p2, LOW); // turn on the motor
delay(watertime*1000); // multiply by 1000 to translate seconds to milliseconds
digitalWrite(p2, HIGH); // turn off the motor
Serial.print('\n');
Serial.print("pump 2 is OFF");
break;
case '3':
Serial.flush();
Serial.print("pump 3 is ON");
digitalWrite(p3, LOW); // turn on the motor
delay(watertime*1000); // multiply by 1000 to translate seconds to milliseconds
digitalWrite(p3, HIGH); // turn off the motor
Serial.print('\n');
Serial.print("pump 3 is OFF");
break;
}
}
//RH & temp
int chk = DHT.read11(DHT11_PIN);
Serial.print("Room Temp. c");
Serial.println(DHT.temperature);
Serial.print("RH %");
Serial.println(DHT.humidity);
delay(2000);
}