Moving Average

Hi, I’m Gokul… i’m working on an open energy meter… here the outputs are voltage, current and powerfactor where i’m getting high amount of samples per second…

So i thought of taking moving average for 60 seconds and displays as one minute sample… but the problem is the number of samples keep varying… I have attached the energy meter code with this post… can any one help me with this…

Thanks in advance… :slight_smile:

New Text Document.txt (18.8 KB)

New Text Document.txt (18.8 KB)

When you find yourself adding numeric suffixes to variable names, it's time to start reading about arrays.

long readVcc() {

/*ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); // for ADK
  ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);  //for UNO

  delay(2); // Wait for Vref to settle
  ADCSRA |= _BV(ADSC); // Start conversion
  
  while (bit_is_set(ADCSRA,ADSC)); // measuring
  uint8_t low  = ADCL; // must read ADCL first - it then locks ADCH  
  uint8_t high = ADCH; // unlocks both
 
  long result = (high<<8) | low;
 
  result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
  return result; // Vcc in millivolts */
  
  long result;
  // Read 1.1V reference against AVcc
  ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  delay(2); // Wait for Vref to settle
  ADCSRA |= _BV(ADSC); // Convert
  while (bit_is_set(ADCSRA,ADSC));
  result = ADCL;
  result |= ADCH<<8;
  result = 1126400L / result; // Back-calculate AVcc in mV
  return result;

}

What do you imagine a “return” statement does?
Why does this function have two?

Actually… this energy meter code is written for UNO as well as ADK… so the adk mux are commented…!

New Text Document.txt (18.8 KB)

Gokulshriyam: Actually.. this energy meter code is written for UNO as well as ADK... so the adk mux are commented...!

And that answers my question . . . how?

Edit: Oh - you hid the comments.

If you want to ignore a block of code, #ifdef/#if...#endif is much simpler and more visible.

. . . and now you're cross-posting.

I just lost interest in your problem.

Cross-post deleted.

This below code works with Mega Adk… I have deleted all the uno codes …

long readVcc() {

ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); // for ADK

delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Start conversion

while (bit_is_set(ADCSRA,ADSC)); // measuring
uint8_t low = ADCL; // must read ADCL first - it then locks ADCH
uint8_t high = ADCH; // unlocks both

long result = (high<<8) | low;

result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.110231000
return result; // Vcc in millivolts */

}