Commit 315f4285 authored by Paul Martin's avatar Paul Martin

Added running average

parent 48f3279c
......@@ -4,6 +4,8 @@
#include <math.h>
#define WINDOW_SIZE 128
Adafruit_ADS1115 ads; /* Use this for the 16-bit version */
//Adafruit_ADS1015 ads; /* Use thi for the 12-bit version */
......@@ -49,20 +51,32 @@ void print_digits(double value, short unsigned digits){
u8g2.print(value, digits - 1);
}
int16_t results[WINDOW_SIZE] = {0};
int current_result = 0;
void loop(void)
{
int16_t results;
int time = millis();
/* Be sure to update this value based on the IC and the gain settings! */
//float multiplier = 3.0F; /* ADS1015 @ +/- 6.144V gain (12-bit results) */
float multiplier = 0.1875F; /* ADS1115 @ +/- 6.144V gain (16-bit results) */
double voltage_multiplier = 5333;
results = ads.readADC_Differential_0_1();
for (int i = 0; i < 16; i++){
results[current_result] = ads.readADC_Differential_0_1();
current_result++;
current_result = current_result % WINDOW_SIZE;
}
double result = 0;
int i;
for (i = 0; i < WINDOW_SIZE; i++){
result = result + results[i];
}
result = result / (double) WINDOW_SIZE;
/*Serial.print("Differential: "); Serial.print(results); */
//Serial.print("(");
Serial.println(results * multiplier, 5);
Serial.println(result * multiplier, 5);
//Serial.println("mV)");
u8g2.firstPage();
......@@ -73,7 +87,7 @@ void loop(void)
// other font options can be found at https://github.com/olikraus/u8g2/wiki/fntlistall
u8g2.setFont(u8g2_font_6x12_tn);
if (results > 0){
if (result > 0){
u8g2.setCursor(0,16);
u8g2.print("+");
} else {
......@@ -83,13 +97,15 @@ void loop(void)
u8g2.setFont(u8g2_font_fur20_tr); // large 20px tall font
u8g2.setCursor(7,26 );
double volts = results / voltage_multiplier;
double volts = result / voltage_multiplier;
print_digits(volts, 5);
u8g2.print("V");
} while ( u8g2.nextPage() );
// wait 100ms between readings.
delay(100);
int d = (1000 / 16) - (millis() - time);
if (d > 0){
delay(d);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment