Skip to content

Instantly share code, notes, and snippets.

Created January 25, 2013 19:22
Show Gist options
  • Save anonymous/4637055 to your computer and use it in GitHub Desktop.
Save anonymous/4637055 to your computer and use it in GitHub Desktop.
rolling average with intermittent off by one
#define digitalvaluesize 50
int digitalVoltage[digitalvaluesize];  // array for storing Voltage ADC values
int digitalMinusVoltage[digitalvaluesize];  // array for storing MinusVoltage ADC values
int digitalACAmps[digitalvaluesize];  // array for storing ACAmps ADC values
int digitalMinusAmps[digitalvaluesize];  // array for storing MinusAmps ADC values
int digitalvalueindex = 0;   // where are we in the indices
unsigned long digitalvalueadder;  //this is used to sum each array before division
void getVoltages(){
  digitalVoltage[digitalvalueindex] = analogRead(voltPin);
  digitalMinusVoltage[digitalvalueindex] = analogRead(minusVoltPin);
  digitalACAmps[digitalvalueindex] = analogRead(ACAmpsPin);
  digitalMinusAmps[digitalvalueindex] = analogRead(MinusAmpsPin);
  if (++digitalvalueindex >= digitalvaluesize) digitalvalueindex = 0;
  digitalvalueadder = 0;
  for(int adder = 0 ; adder < digitalvaluesize; adder++) {
    digitalvalueadder += digitalVoltage[adder];
  };
  voltage = digitalvalueadder / digitalvaluesize / voltCoeff;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment