Changing parameters in function

Not sure of the syntax for changing parameters provided to a function, this is what I have, can you help with the correct syntax please?

// *****************************************************************************
//                       Add to running average
// *****************************************************************************
void AddToRunningAverage(long *CurrentAverage,long *CountOfItems,int NewValue) {
     
     *CountOfItems++ ;    
     *CurrentAverage = (*CurrentAverage * *CountOfItems + NewValue) / (*CountOfItems + 1) ;
     
}

Called with :-

 AddToRunningAverage(&TempRunningAverage,&TempAverageCount,Temp) ;

Thanks!

Syntax looks fine to me.

Still cant figure it, Light is averaging but temperature isnt, I get 1 for the average, 70.8 - 71.2 are the max an min, no clue why!

// *****************************************************************************
//                             LIBRARY INCLUDES
// *****************************************************************************
#include "LedControl.h"
// *****************************************************************************
//                          VARIABLE INITIALIZATION
// *****************************************************************************
unsigned long LightRunningAverage = 0;
unsigned long LightAverageCount   = 0 ;

unsigned long TempRunningAverage = 0 ;
unsigned long TempAverageCount   = 0 ;

int MaxLight = 0 ;
int MinLight = 9999 ;

int MaxTemp = 0 ;
int MinTemp = 9999 ;

int TempSensor = 3 ;
int LightSensor =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() ;
}
// *****************************************************************************
//                       Process Light Readings
// *****************************************************************************
void ProcessLight() {
     int Light = readLight(LightSensor) ;
     AddToRunningAverage(&LightRunningAverage,&LightAverageCount,Light) ;

     if (Light > MaxLight) {
          MaxLight = Light ; 
     }
     if (Light < MinLight) { 
          MinLight = Light ;
     }
     printMin(MinLight) ;
     printMax(MaxLight) ;
     delay(1000) ;
     printAve(LightRunningAverage) ;
     delay(1000) ;
}
// *****************************************************************************
//                       Process Temperature Readings
// *****************************************************************************
void ProcessTemp() {
     int Temp = readTempF() ;

     AddToRunningAverage(&TempRunningAverage,&TempAverageCount,Temp) ;

     if (Temp > MaxTemp) {
          MaxTemp = Temp ; 
     }
     if (Temp < MinTemp) { 
          MinTemp = Temp ;
     }
     printMax(MaxTemp) ;
     printMin(MinTemp) ;
     delay(1000) ;
     printAve(TempRunningAverage) ;
     delay(1000) ;
}
// *****************************************************************************
//                       Add to running average
// *****************************************************************************
void AddToRunningAverage(unsigned long *CurrentAverage,unsigned long *CountOfItems,int NewValue) {

     *CountOfItems++ ;    
     *CurrentAverage = (*CurrentAverage * *CountOfItems + NewValue) / (*CountOfItems + 1) ;
}

any ideas, I’m new to C syntax so perhaps there is something I am missing!

TIA

Would you be able to post readTempF()?

Have you tried printing out the value that this returns?

void AddToRunningAverage(long *CurrentAverage,long *CountOfItems,int NewValue) {
     *CurrentAverage = (*CurrentAverage * *CountOfItems + NewValue) / (*CountOfItems + 1) ;
     *CountOfItems++;
}

switch the order of your operations like the above, otherwise your are multiplying the old average with the new count of items.

You would get better precision by storing the sum rather than the average, (i.e. dividing with count only when you are going to print the result)

int Temp = readTempF() ;

How can you get 70.8 as min temperature when you're storing it in an int? Does readTempF return deci-Fahrenheits?

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) ;
     }
}