Program Discrepancy

Hello all,

Quick background:

I'm new to the Arduino forums as well as the Arduino interface.I have only been using it for about two months so far. I have a very light background in programming but my main area is electronics. So basically I haven't had too many problems so far. I'm working on a project that controls a system based on temperature. I have everything I need and so far everything seems to be working fine. For hardware, I'm using an Arduino Mega 2560 Rev.3, LCD Keypad shield (DFRobot 16 x 2), 2 of the OneWire temperature sensors, an N-Channel MOSFET and an LED. However I have run into a small discrepancy. The code is posted below:

#include <OneWire.h> //The Temperature Sensor Library
#include <DallasTemperature.h> //The Temperature Sensor Libary (From Manufacturer) 
#include <LiquidCrystal.h> //The Liquid Crystal Library

//Initializations
#define ONE_WIRE_BUS 52 //Sets the temp sensors to read from pin 52
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
OneWire oneWire(ONE_WIRE_BUS); //Both Sensors on the same pin
DallasTemperature sensors(&oneWire); 
int ledPin = 50; //Initialize the digital port using the LED
int FETPin = 53;  //Initialize the voltage port
int beepPin = 21;
int pin;
int i;
int j;
double ftemp1;
double ftemp2;

unsigned long start, finished, elapsed, over;
float hours, minutes ,seconds, millisec; 

long previousLCDMillis = 0;    // for LCD screen update
long lcdInterval = 4000;

// screen to show 
int screen = 0;    
int screenMax = 2;
bool screenChanged = true;   // initially we have a new screen,  by definition 
// defines of the screens to show
#define TEMPERATURE   0
#define SYSTEM        1
#define kWH           2



DeviceAddress  amb = { 0x28, 0xE3, 0xEA, 0x51, 0x04, 0x00, 0x00, 0xC0 }; //Define Address of Ambient Temperature Sensor
DeviceAddress sys  = { 0x28, 0x1D, 0xF3, 0x52, 0x04, 0x00, 0x00, 0xA6 }; //Defne Address of System Temperature Sensor


//Setup
void setup()
{
  
  //Temp Sensor
  sensors.begin(); //Read from the sensor library
  sensors.setResolution(amb, 9); //Ambient Sensor has 9-Bit Resolution
  sensors.setResolution(sys, 9); //System Sensor has 9-Bit Resolution
  
  //Pins
  pinMode (50,OUTPUT); //Turns off 33k pullup resistor
  pinMode (53, OUTPUT); //Turns off 33k pullup resistor
  pinMode (21, OUTPUT);

  return initial();
  
}//Setup Closure

//Loop
void loop()
{
  
    switch(screen)
    {
    case TEMPERATURE: 
      temp();
      delay(1000);
    case SYSTEM: 
      SystemON();
      delay(1000);
    case kWH:
      delay(1000);
      break;
    default:
      // cannot happen -> showError() ?
      break;
    }
}//Loop Closure

//functions

void temp()
{  
  
  lcd.clear();
  lcd.begin(16, 2); //Position of LCD text in the proper area
  lcd.print("Ambient: "); //Print the first measurement's designation
  lcd.setCursor(14,0); //Position the cursor
  lcd.setCursor(0, 1); //Position the second line of text
  lcd.print("System: "); //Print the second measurement's designation
  lcd.setCursor(14,1); //Position the cursor  
  //Ambient Temp Sensor Reading/Writing
  sensors.requestTemperatures(); // Send the command to get temperatures
  lcd.setCursor(9, 0);
  ftemp1 = ((sensors.getTempCByIndex(1)* 1.8) + 32.0);  //Converts the temp reading F
  lcd.print(ftemp1); //print the first reading from the first sensor
  lcd.print("\337F");
  delay(100); //10ms delay of each measurement

  //System Temp Sensor Reading/Writing
  sensors.requestTemperatures(); // Send the command to get temperatures
  lcd.setCursor(9, 1); //position the lcd text
  ftemp2 = ((sensors.getTempCByIndex(0)* 1.8) + 32.0);  //Converts the temp reading F
  lcd.print(ftemp2); //print the second reading from the second sensor
  lcd.print("\337F");
  delay(100); //10ms delay of each measurement
  
  //Return control functions
  return ledindicator();
  return fet();
}

void ledindicator()
{
   if (ftemp1 <= 77) //Turns the system on less/equal to this value
  {
    digitalWrite(ledPin, HIGH);     // sets the LED to 1 (system on indicator)
    delay(100);             // waits for half a second
  }
  
   else if (ftemp1 > 77)
  {
    digitalWrite(ledPin, LOW); //Turns the system off if above condition is not met
    delay(100); //100ms delay
  }
 
}

void fet()
{
  if (ftemp1 <= 77)
  {
    digitalWrite(FETPin, HIGH);
    delay(100);
  }
  else if (ftemp1 > 77)
  {
    digitalWrite(FETPin, LOW);
    delay(100);
  }
}

void SystemON()
{
  finished = millis();
  elapsed = finished-start;
  hours = int(elapsed/3600000);
  over = elapsed%3600000;
  minutes = int(over/60000);
  over=over%60000;
  seconds=int(over/1000);
  millisec=over%1000;
  return systemONread();
  delay(1000);
 
}

void systemONread()
{
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Sys.ON Time:");
  lcd.setCursor(0,1);
  lcd.print(hours,0);
  lcd.print("H ");
  lcd.print(minutes,0);
  lcd.print("M ");
  lcd.print(seconds,0);
  lcd.print("S ");
}

void initial()
{
  lcd.clear();
  lcd.print("Initializing...");
  delay(2000);  
}

I get all of the values I need and everything is returned properly. The problem is that the values for the temperature and system counter do not update in real time. They only refresh after each cycle of the LCD. It must be something to do with the loop but I don't know what. Please keep in mind that the code is unfinished and there might be some superfluous things laying about. Any help that I can get would be greatly appreciated. Thank you!

You have calls to "delay()", and you wonder about real-time?

Nothing is given a chance of changing 'screen' so, well - you get the result you've got!

void loop()
{
    switch(screen)
    {
    case TEMPERATURE: 
      temp();
      delay(1000);
    case SYSTEM: 
      SystemON();
      delay(1000);
    case kWH:
      delay(1000);
      break;
    default:
      // cannot happen -> showError() ?
      break;
    }
}//Loop Closure
//Return control functions
  return ledindicator();
  return fet();

A function has one return value.
Guess which one that is.

'setup' returns 'void' otherwise known as nothing so what do you try to do?

void setup()
{

	//Temp Sensor
	sensors.begin();//Read from the sensor library
	sensors.setResolution(amb, 9);	//Ambient Sensor has 9-Bit Resolution
	sensors.setResolution(sys, 9);	//System Sensor has 9-Bit Resolution

	//Pins
	pinMode(50, OUTPUT);	//Turns off 33k pullup resistor
	pinMode(53, OUTPUT);	//Turns off 33k pullup resistor
	pinMode(21, OUTPUT);

	return initial();
}	//Setup Closure

AWOL:
You have calls to "delay()", and you wonder about real-time?

I have to delay it.

I just want the values to update right before the screen changed on each iteration of the switch case.

There is probably a default but I'm to lazy to look at the moment!

You've forgotten to call 'lcd.begin(columns, rows)' in one of your several initialization routines

EDIT: Move it from 'temp' to 'setup'

'SystemON' Just seems wrong to me. Plus no need for many of the globals it uses.

void SystemON()
{
    unsigned long   tms = millis() - start;
    hours        = tms / ONE_HOUR;
    tms         -= hours * ONE_HOUR;
    minutes      = tms / ONE_MINUTE;
    tms         -= minutes * ONE_MINUTE;
    seconds      = tms / ONE_SECOND;
    millisec    -= seconds * ONE_SECOND;
    systemONread();

    delay(ONE_SECOND);
}

I recommend some self documenting global constants vs hard coded 'what the heck is that large number' values spread through the program.

const unsigned long     ONE_SECOND  = 1000UL;
const unsigned long     ONE_MINUTE  = 60 * 1000UL;
const unsigned long     ONE_HOUR    = 60 * ONE_MINUTE;

Thank you everyone. I'll try some of the suggestions now.