LOL, thanks drhex, I guess I shouldn't write code when I wake up at 2am!
That seems to have solved the problem!! Full code is below
// Hardware :
// 2x 4 digit LED display connected to MAX7219 on pins 9,10,11
// LM35 precision temp sensor connected to Analog 3
// LDR connected to + , 26.8K resistance to ground connected in middle to Analog 0 ;
// *****************************************************************************
// LIBRARY INCLUDES
// *****************************************************************************
#include "LedControl.h"
// *****************************************************************************
// VARIABLE INITIALIZATION
// *****************************************************************************
int delayFactor = 1000 ;
int MaxLight = 0 ;
int MinLight = 9999 ;
int MaxTemp = 0 ;
int MinTemp = 9999 ;
int TempSensor = 3 ;
int LightSensor =0 ;
unsigned long LightRunningAverage = 0;
unsigned long TempRunningAverage = 0 ;
unsigned long LightAverageCount = 0 ;
unsigned long TempAverageCount = 0 ;
LedControl lc=LedControl(9,10,11,1);
// *****************************************************************************
// SETUP FUNCTION
// *****************************************************************************
void setup() {
Serial.begin(9600) ;
analogReference(INTERNAL) ;
lc.shutdown(0,false);
lc.setIntensity(0,1);
lc.clearDisplay(0);
}
// *****************************************************************************
// LOOP FUNCTION
// *****************************************************************************
void loop() {
ProcessLight() ;
ProcessTemp() ;
displayReadings() ;
}
// *****************************************************************************
// Display Heat
// *****************************************************************************
void DisplayHeat() {
lc.setChar(0,2,'h',false) ;
lc.setChar(0,3,'e',false) ;
lc.setChar(0,4,'a',false) ;
lc.setRow(0,5,B00001111) ;
}
// *****************************************************************************
// Display Light
// *****************************************************************************
void DisplayLight() {
lc.setChar(0,2,'l',false) ;
lc.setChar(0,3,'1',false) ;
lc.setChar(0,4,'9',false) ;
lc.setChar(0,5,'h',false) ;
lc.setRow(0,6,B00001111) ;
}
// *****************************************************************************
// Send Readings To Serial Port
// *****************************************************************************
void SendToSerial() {
Serial.print("T :") ;
Serial.print("Min ") ;
Serial.print(MinTemp) ;
Serial.print(" Max ") ;
Serial.print(MaxTemp) ;
Serial.print(" Ave ") ;
Serial.print(TempRunningAverage) ;
Serial.print(" L :") ;
Serial.print("Min ") ;
Serial.print(MinLight) ;
Serial.print(" Max ") ;
Serial.print(MaxLight) ;
Serial.print(" Ave ") ;
Serial.println(LightRunningAverage) ;
}
// *****************************************************************************
// Output Stats
// *****************************************************************************
void OutputStats(int Min,int Max,unsigned long Average) {
delay(delayFactor) ;
printMax(Max) ;
printMin(Min) ;
delay(3*delayFactor) ;
printAve(Average) ;
delay(3*delayFactor) ;
}
// *****************************************************************************
// Display Readings
// *****************************************************************************
void displayReadings() {
SendToSerial() ;
DisplayHeat() ;
OutputStats(MinTemp,MaxTemp,TempRunningAverage) ;
DisplayLight() ;
OutputStats(MinLight,MaxLight,LightRunningAverage) ;
}
// *****************************************************************************
// Process Light Readings
// *****************************************************************************
void ProcessLight() {
int Light = readLight(LightSensor) ;
AddToRunningAverage(&LightRunningAverage,&LightAverageCount,Light) ;
if (Light > MaxLight) {
MaxLight = Light ;
}
if (Light < MinLight) {
MinLight = Light ;
}
}
// *****************************************************************************
// Process Temperature Readings
// *****************************************************************************
void ProcessTemp() {
int Temp = readTempF() ;
AddToRunningAverage(&TempRunningAverage,&TempAverageCount,Temp) ;
if (Temp > MaxTemp) {
MaxTemp = Temp ;
}
if (Temp < MinTemp) {
MinTemp = Temp ;
}
}
// *****************************************************************************
// Add to running average
// *****************************************************************************
void AddToRunningAverage(unsigned long *CurrentAverage,unsigned long *CountOfItems,int NewValue) {
*CurrentAverage = (*CurrentAverage * *CountOfItems + NewValue) / (*CountOfItems + 1) ;
*CountOfItems++ ;
}
// *****************************************************************************
// PLACE LONG ON left 4 Digits, fixed decimal xxx.x
// *****************************************************************************
void printLED(unsigned long Number,int StartDigit,int DPDigit) {
unsigned long TempNum = Number % 100000000 ;
int CurrentDigit = StartDigit ;
while (TempNum > 0) {
if (CurrentDigit == DPDigit) {
lc.setDigit(0,CurrentDigit,TempNum % 10,true) ;
}
else {
lc.setDigit(0,CurrentDigit,TempNum % 10,false) ;
}
TempNum = TempNum / 10 ;
CurrentDigit-- ;
}
for (CurrentDigit ; CurrentDigit >= 0 ; CurrentDigit--) {
lc.setRow(0,CurrentDigit,0) ;
}
}
// *****************************************************************************
// PLACE LONG ON LED DISPLAY VIA 7219
// *****************************************************************************
void printLEDLong(unsigned long Number) {
printLED(Number,7,8) ;
}
// *****************************************************************************
// PLACE LONG ON left 4 Digits, fixed decimal xxx.x
// *****************************************************************************
void printAve(unsigned long Number) {
printLEDLong(0) ;
printLED(Number,5,4) ;
}
// *****************************************************************************
// PLACE LONG ON left 4 Digits, fixed decimal xxx.x
// *****************************************************************************
void printMin(unsigned long Number) {
printLED(Number,3,2) ;
}
// *****************************************************************************
// PLACE LONG ON Right 4 Digits, fixed decimal xxx.x
// *****************************************************************************
void printMax(unsigned long Number) {
printLED(Number,7,6) ;
}
// *****************************************************************************
// Read value from light sensor re-scale 0-1000
// *****************************************************************************
int readLight(int analogPort) {
SetDefaultReference() ;
return(readAnalog(LightSensor,20)*1000/1023 ) ;
}
//*********************************************************************
// Read Tempreature from LM35 in F
//*********************************************************************
int readTempF() {
return(readTempC() * 9 / 5 + 320);
}
//*********************************************************************
// Read Tempreature from LM35 in C
//*********************************************************************
int readTempC() {
SetInternalReference();
return(((100*1.1*readAnalog(TempSensor,20))/1024)*10);
}
//*********************************************************************
// Averaging Read From Port
//*********************************************************************
long readAnalog(int Port, int Samples) {
long Accumulator = 0 ;
for (int i = 0; i < Samples ; i++) {
Accumulator = Accumulator + analogRead(Port);
}
return(Accumulator / Samples) ;
}
//*********************************************************************
// Set internal reference discard next results
//*********************************************************************
void SetInternalReference() {
analogReference(INTERNAL) ;
for (int j =0 ; j<10 ; j++) {
analogRead(0) ;
delay(40) ;
}
}
//*********************************************************************
// Set DEFAULT reference discard next results
//*********************************************************************
void SetDefaultReference() {
analogReference(DEFAULT) ;
for (int j =0 ; j<10 ; j++) {
analogRead(0) ;
delay(40) ;
}
}