Trying to combine code to run a 2-zone radiant floor heating system. The menu code works perfectly. When I select MenuC 'Run main' it executes to RadiantMain prints test and then returns to menu options again. Been working on this for weeks and don't how to fix it. The RadiantMain code works ok as a stand-alone.
The second sketch starts where it says //START RUN RADIANT MAIN CODE. The menu sketch and the Radiant Main are 2 separate sketches written by 2 different developers. They do run as stand-alone sketches. I modified the menu sketch to operate my relays (MenuA and MenuB) for the manual mode it works fine. I need to be able to use the menu sketch to run 3 different functions. MenuA & MenuB to manually run the system in manual mode and a third menu item (MenuC) to run the 2nd sketch in auto mode.
I need to combine the 2 sketches together into one code
I am using a D1 ROBOT LCD KEYPAD shield. A1 through A5 are connected to temp sensors. 4 relay shield controls 2 water valves and a pump, 4th relay is unused. the menu sketch controls the 2 water valves with MenuA and MenuB. I need MenuC to run the second sketch which reads sensors sets temps and controls 2 water valves and a pump.
//MENU CODE STARTS HERE
#include <LiquidCrystal.h>
LiquidCrystal lcd(8,9,4,5,6,7);
int keypad_pin = A0;
int keypad_value = 0;
int keypad_value_old = 0;
int pinCount = 4;
int relay_1 = 12;//zone 1
int relay_2 = 11;//zone 2
int relay_3 = 3;//pump
//int relay_4 = 2;//open
char btn_push;
byte mainMenuPage = 1;
byte mainMenuPageOld = 1;
byte mainMenuTotal = 3;
void setup() {
Serial.begin(9600);
pinMode(relay_1, OUTPUT);//zone 1
pinMode(relay_2, OUTPUT);//zone 2
pinMode(relay_3, OUTPUT);//pump
//pinMode(relay_4, OUTPUT);
lcd.begin(16,2); //Initialize a 2x16 type LCD
MainMenuDisplay();
delay(1000);
}
void loop()
{
btn_push = ReadKeypad();
MainMenuBtn();
if(btn_push == 'S')//enter selected menu
{
WaitBtnRelease();
switch (mainMenuPage)
{
case 1:
Zone1();
break;
case 2:
Zone2();
break;
case 3:
Radiant();
break;
/*case 4:
MenuD();
*/break;
}
MainMenuDisplay();
WaitBtnRelease();
}
delay(10);
}//--------------- End of loop() loop ---------------------
void MainMenuDisplay()
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Choose function");
lcd.setCursor(1,1);
switch (mainMenuPage)
{
case 1:
lcd.print("Run zone 1");
break;
case 2:
lcd.print("Run zone 2");
break;
case 3:
lcd.print("Run main");
break;
case 4:
lcd.print("4. Menu D");
break;
}
}
void Zone1()
{
{
digitalWrite(relay_1, HIGH);
digitalWrite(relay_3, HIGH);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" Running zone 1");
lcd.setCursor(0,1);
lcd.print(" Left to end");
while(ReadKeypad()!= 'L');
digitalWrite(relay_1, LOW);
digitalWrite(relay_3, LOW);
}
}
void Zone2()
{
digitalWrite(relay_2, HIGH);
digitalWrite(relay_3, HIGH);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" Running zone 2");
lcd.setCursor(0,1);
lcd.print(" Left to end");
while(ReadKeypad()!= 'L');
digitalWrite(relay_2, LOW);
digitalWrite(relay_3, LOW);
}
void Radiant()
{
RadiantMain();
}
void MenuD()
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Inside Menu D");
while(ReadKeypad()!= 'L')
{
//Insert Task for Menu D here
}
}
void MainMenuBtn()
{
WaitBtnRelease();
if(btn_push == 'U')
{
mainMenuPage++;
if(mainMenuPage > mainMenuTotal)
mainMenuPage = 1;
}
else if(btn_push == 'D')
{
mainMenuPage--;
if(mainMenuPage == 0)
mainMenuPage = mainMenuTotal;
}
if(mainMenuPage != mainMenuPageOld) //only update display when page change
{
MainMenuDisplay();
mainMenuPageOld = mainMenuPage;
}
}
char ReadKeypad()
{
/* Keypad button analog Value
no button pressed 1023
select 741
left 503
up 326
down 142
right 0
*/
keypad_value = analogRead(keypad_pin);
if(keypad_value < 100)
return 'R';
else if(keypad_value < 200)
return 'D';
else if(keypad_value < 400)
return 'U';
else if(keypad_value < 600)
return 'L';
else if(keypad_value < 800)
return 'S';
else
return 'N';
}
void WaitBtnRelease()
{
while( analogRead(keypad_pin) < 800){}
}
//END MENU CODE
// Title: 2 zone Radiant Heat Controller
// Date:12/01/14
// Author: Chris Biblis (My First Adruino Project )
//RADIANT MAIN CODE STRTS HERE_________________________________________________________
// #include <LiquidCrystal.h>
// LiquidC//rystal lcd(8, 9, 4, 5, 6, 7);
// define some values used by the panel and buttons
int lcd_key = 0;
int adc_key_in = 0;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
// read the buttons
int read_LCD_buttons(){
adc_key_in = analogRead(0);
// buttons when read are centered at these valies: 0, 144, 329, 504, 741
// we add approx 50 to those values and check to see if we are close
if (adc_key_in > 1000) return btnNONE;
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 250) return btnUP;
if (adc_key_in < 450) return btnDOWN;
if (adc_key_in < 625) return btnLEFT; //Changed value to 625 do to double read from select button Default is 650.
if (adc_key_in < 850) return btnSELECT;
return btnNONE;
}
// Setup relay shield pin array.
int relayPin[] = {2, 3, 11, 12};
//int pinCount = 4;
// Set On/Off Differentials for collector/tank temperatures.
const int diffON = 5;
const int diffOFF = 2;
// Set Default Thermostat temperature
int setTemp = 60;
int setTemp2 = 60;
//Used for calibration
int offSet[] = {-2, -1, 0, 1, 2};
//Button Variables.
int select = 0;
int up = 0;
int down= 0;
int backLight = 10;
boolean BLbutton = false;
//void RadiantMain()//RadiantMain setup
{
lcd.begin(16, 2);
lcd.setCursor(0,0);
//Introduction Screen
lcd.print("2 zone");
lcd.setCursor(0,1);
lcd.print("Radiant floor");
delay(2000);
for (int thisPin = 0; thisPin < pinCount; thisPin++)
{ pinMode(relayPin[thisPin], OUTPUT);}
}
// Read average voltage from sensors
int ReadSens(int x)
{
int i;
int sval = 0;
for (i = 0; i < 10; i++)
{ sval = sval + analogRead(x); }
sval = sval / 10;
return sval;
}
// Calculate temperature in F from voltage of sensors.
int getTemp(int y)
{
//Convert to Celcius
float a = (((y / 1024.0) * 5000)/10)-273.15;
//Convert Celcius to Fahrenheit
float b = a * 9.0 / 5.0 + 32.0;
return b;
}
void RadiantRun()//need RadiantRun loop
{
// Get average Voltage
int z1flr = ReadSens(1);//zone 1 floor temp
int z2flr = ReadSens(2);//zone2 floor temp
int z1line = ReadSens(3);//zone 1 return line temp
int z2line = ReadSens(4);//zone 2 return line temp
int pmp = ReadSens(5);
// Get Temperatures & Apply OffSet { -2, -1 , 0, 1, 2 }
int zone1floortemp = (getTemp(z1flr) + offSet[0]);
int zone2floortemp = (getTemp(z2flr) + offSet[0]);
int zone1linetemp = (getTemp(z1line) + offSet[0]);
int zone2linetemp = (getTemp(z2line) + offSet[0]);
int pump = (getTemp(pmp) + offSet[0]);
//Backlight button selection...
if (BLbutton == true){pinMode(backLight, INPUT);} //turn backlight off
else{pinMode(backLight,OUTPUT);} //turn backlight on
// Display screens
if (select == 3){select = 0;}
if (select == 0){
lcd.clear(); lcd.setCursor(0,0);
lcd.print(" Ambient Temp ");
lcd.setCursor(6,1);
lcd.print(zone1floortemp);
lcd.print(" F");
}
if (select == 1)
{
lcd.clear(); lcd.setCursor(0,0);
lcd.print("Set Ambient To:");
lcd.setCursor(6,1);
lcd.print(setTemp);
lcd.print(" F");
}
if (select == 2)
{
lcd.clear(); lcd.setCursor(0,0);
lcd.print("Z1F ");
lcd.print(zone1floortemp);
lcd.print("F");
lcd.setCursor(9,0);
lcd.print("Z2F ");
lcd.print(zone1floortemp);
lcd.print("F");
lcd.setCursor(9,1);
lcd.print("Z2L ");
lcd.print(zone2linetemp);
lcd.print("F");
lcd.setCursor(0,1);
lcd.print("Z1L ");
lcd.print(zone1linetemp);
lcd.print("F");
}
lcd_key = read_LCD_buttons(); // read the buttons
switch (lcd_key)
{
case btnSELECT:{ BLbutton = !BLbutton; delay(100); break; }
case btnUP: { setTemp++; select = 1; delay(100); break; }
case btnDOWN: { setTemp--; select = 1; delay(100); break; }
case btnRIGHT: { select++; delay(100); break; }
case btnLEFT: { select--; delay(100); break; }
}
// only 1 zone at a time_____________NOT IMPLEMENTED YET____________
// zone 1
// if floor temp zone 1 = less then set temp then open valve zone 1 turn on pump
// if(z1flr <= (setTemp + diffOFF))
// {digitalWrite(relayPin[3], HIGH);} // pump on
// {digitalWrite(relayPin[12], HIGH);} // valve 1 on
// {digitalWrite(relayPin[11], LOW);} // valve 2 off
// if floor temp zone 1 = set temp then close valve zone 1 turn off pump
// if(z1flr = (setTemp + diffOFF))
// {digitalWrite(relayPin[3], LOW);} // pump off
// {digitalWrite(relayPin[12], LOW);} // valve 1 off
// // if floor temp zone 2 = less then set temp and zone 1 = less then set temp zone 2 valve off
// if(z1flr <= (setTemp + diffOFF) && (z1flr < (setTemp)))
// {digitalWrite(relayPin[11], LOW);} // valve 2 off
// zone 2
// if floor temp zone 1 = less then set temp then open valve zone 1 turn on pump
// if(z1flr <= (setTemp + diffOFF))
// {digitalWrite(relayPin[3], HIGH);} // pump on
// {digitalWrite(relayPin[11], HIGH);} // valve 2 on
// {digitalWrite(relayPin[12], LOW);} // valve 1 off
// if floor temp zone 1 = set temp then close valve zone 1 turn off pump
// if(z1flr = (setTemp + diffOFF))
// {digitalWrite(relayPin[3], LOW);} // pump off
// {digitalWrite(relayPin[11], LOW);} // valve 2 off
// if floor temp zone 1 = less then set temp and zone 2 = less then set temp zone 1 valve off
// if(z1flr <= (setTemp + diffOFF) && (z1flr < (setTemp)))
// {digitalWrite(relayPin[12], LOW);} // valve 1 off
delay(500);
}
//RADIANT MAIN CODE ENDS HERE___________________________________________________
//relay pinouts
//Digital 12 – controls RELAY 1 COM1 pin zone 1 valve
//Digital 11 – controls RELAY 2 COM2 pin zone 2 valve
//Digital 3 – controls RELAY 3 COM3 pump
//Digital 2 – controls RELAY 4 COM4 open
//sensors:
//flr1 = floor temp zone 1 sensor
//flr2 = floor temp zone 2 sensor
//pmp = pump OFF/ON
//z1= zone 1 line temp sensor
//z2 = zone 2 line temp sensor