Is it possible to use serial monitor and LCD in one program

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. :confused:
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. :cold_sweat:
Thank you!

I am having two different programs one is having EC and Temperature value and other is about pump.
The program with pump shows if pump is on or off on and LCD and EC, Temp. one shows values on the serial monitor. Is it possible that I can combine these two programs and see the values on their respective outputs viewer.

You can use Serial and an LCD at the same time. If you're able to combine the sketches, I don't know...

Please, help me as soon as possible I'm having a deadline.

Re-read your post. "I'm having problems" without telling us what the problems are was a waste of typing.

Splattering curly braces around, in the hopes that they will act like magic pixie dust and fix the compiler errors is a waste of typing, too.

delay(0) is stupid. Get rid of ALL such crap.

//everything between these curly braces will loop

But there are no curly braces, and nothing that makes looping happen.

There is also NOTHING in your post about what the combined program is supposed to do. "You guys get busy coding while I go see what the requirements are" is funny in the comics. In real life, it's stupid.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile:

Moderator: merged topics.

Hi,
The easiest way to merg you codes is to make each void loop() a function, combine the two void setup() scripts together as one void setup() script.

Then in the void loop(), you call the two functions you have made.

for example
Code 1;

//code 1

bool a;
int b = 0;

void setup() {
  Serial.begin(9600);
  pinMode(8, OUTPUT);
  pinMode(9, INPUT);
}
void loop() {
  a = digitalRead(9);
  if (a == true)
  {
    b = 100;
  }
  else
  {
    b = 0;
  }
}

Code 2;

//code 2

int c;
int d;

void setup() {
  Serial.begin(9600);
  pinMode(3, OUTPUT);
  pinMode(A0, INPUT);
}
void loop() {
  c = analogRead(A0);
  d = c / 4;
  analogWrite(3, d);
}

Combined Code1and2;

//code 1and2
bool a;  // from code1
int b = 0; // from code1
int c;  // from code 2
int d;  // from code 2

void setup() {
  Serial.begin(9600);
  pinMode(8, OUTPUT);  // from code1
  pinMode(9, INPUT);   //from code1
  pinMode(3, OUTPUT);  //from code2
  pinMode(A0, INPUT);  //from code2

}
void loop() {
  code1();
  code2();
}
//===functions
void code1() {  // code1 turned into a function
  a = digitalRead(9);
  if (a == true)
  {
    b = 100;
  }
  else
  {
    b = 0;
  }
}

void code2() {   // code2 turned into a function
  c = analogRead(A0);
  d = c / 4;
  analogWrite(3, d);
}

I hope it helps explain the process.

Tom... :slight_smile:

Thank you Tom