Hey, I am working on automated hydroponics project. I have two different programs one for getting values of water temperature and ec value and other one is for switching on and off the 12v water pump which is connected through a relay to the arduino and it is controlled using values from ultrasonic sensor (HC Sr 04).
I am having trouble combining these two programs. Please, help me out.
And I am having one concern will the delay happening in one program affect the next one. ![]()
Program 1: For EC and Temperature values in serial monitor
//Include libraries
#include <OneWire.h>
#include <DallasTemperature.h>
//#include <LiquidCrystal.h>
//LiquidCrystal lcd(12,11,5,4,3,2);
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 6
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
const int switchPin = 6;
int switchState = 0;
int condVal;
void setup(void)
{
Serial.begin(9600); //Begin serial communication
// Serial.println("Arduino Digital Temperature // Serial Monitor Version"); //Print a message
//lcd.begin(16,2); //instructs the lcd to begin displaying
pinMode(switchPin, INPUT); //sets analog 6 as an input pin
sensors.begin();
}
void loop(void)
{
// Send the command to get temperatures
sensors.requestTemperatures();
Serial.print("Temperature ");
Serial.println(sensors.getTempCByIndex(0)); // Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
//Update value every 1 sec.
delay(1000);
//everything between these curly braces will loop
condVal = analogRead(A0);
float siemens = condVal*(5.0/1023.0); //calculation of relative conductivity
Serial.print("EC");
Serial.print (siemens );
Serial.print(" ");
//lcd.setCursor(0,0); //instructs LCD to go to the first line, first space
//lcd.print("Rel Conductance"); //instructs LCD to display "Rel Conductance" beginning on the first line, first space of the LCD display.
//lcd.setCursor(0,1); //instructs LCD display to go to the second line, first space.
//lcd.print(voltage); //display the relative conductivity from the probe on the second line, first space
delay(1000); //delay before looping
}
program 2: Pump operation
#include<LiquidCrystal.h> // include the library code for lcd
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //
#define echopin 9 // echo pin
#define trigpin 8 // Trigger pin
int maximumRange = 50;
long duration, distance;
void setup() {
lcd.begin(16,2);
Serial.begin (9600);
pinMode (trigpin, OUTPUT);
pinMode (echopin, INPUT );
pinMode (4, OUTPUT);
pinMode (13,OUTPUT);
}
void loop ()
{
{
digitalWrite(trigpin,LOW);
delayMicroseconds(2);
digitalWrite(trigpin,HIGH);
delayMicroseconds(10);
duration=pulseIn (echopin,HIGH);
distance= duration/58.2;
delay (50);
Serial.println(distance);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("water level :");
lcd.print(distance);
delay(0);
}
if (distance <= 3 ){
digitalWrite (13,LOW);// connect to relay(motor)
digitalWrite (7,LOW);
lcd.setCursor(0,1);
lcd.print("Tank is Full");
delay(0);
}
else if (distance >=9) {
digitalWrite (7,HIGH); // connect to relay(motor)
digitalWrite (13,HIGH);
lcd.setCursor(0,1);
lcd.print("Motor Started");
delay(0);
}
}
Please, help me as soon as possible I'm having a deadline. ![]()
Thank you!