Averaging not working

Hi I have a problem averaging. I am trying to average water usage in Arduino and am having problems.

These are my integers.

 DateTime now = rtc.now();
  const int nowy = (now.year()- 2000);
  const int nowyy = (now.year());
  const int nowm = (now.month());
  const int nowd = (now.day());  
  const int nowh = (now.hour());
  const int nowmin = (now.minute());
  const int nowsec = (now.second()); 

  int datav = ((29 - nowd) + (31- EEPROM.read(72)));
  int datav1 = (31 - nowd) + (29 - EEPROM.read(72));
  int datav2 = (30 - nowd) + (31 - EEPROM.read(72));
  int datav3 = (31 - nowd) + (30 - EEPROM.read(72));
  int datav4 = (30 - nowd) + (31 - EEPROM.read(72));
  int datav5 = (31 - nowd) + (30 - EEPROM.read(72));
  int datav6 = (31 - nowd) + (31 - EEPROM.read(72));
  int datav7 = (30 - nowd) + (31 - EEPROM.read(72));
  int datav8 = (31 - nowd) + (30 - EEPROM.read(72));
  int datav9 = (30 - nowd) + (31 - EEPROM.read(72));
  int datav10 = (30 - nowd) + (31 - EEPROM.read(72));
  int datav11 = (31 - nowd) + (31 - EEPROM.read(72));
  int datav001 = ((nowd) - (EEPROM.read(72)));

This is where it takes the measurement.

//Averging system
if (now.hour() == 6)
if (now.minute()== 1) 
 if (now.second() == 1)
  
  {
    
    EEPROM.write(addr, depthping);
    address = address + 1;
    delay(999);
    
  }

if (now.hour() == 21)
 if (now.minute()== 1) 
 if (now.second() == 1)
  {

    EEPROM.write(addr, depthping);
    address = address + 1;
    delay(999);


  }

This is my menu, with keypad interaction.

 lcd.setCursor(0,0);
lcd.print("Press A for depth,");
lcd.print("  Press B for Average,");
lcd.setCursor(0,1);
lcd.println("Press C when refilled & D for next order");
  
{ 
 //keycheck
  char key = kpd.getKey(); 
  if(key)  





  {
    switch (key)
    {

This is where the avergaing takes place.

case 'B': 
{
value1 = EEPROM.read(0);  
  value2 = EEPROM.read(1); 
  value3 = EEPROM.read(2);
  value4 = EEPROM.read(3);
  value5 = EEPROM.read(4);
  value6 = EEPROM.read(5);
  value7 = EEPROM.read(6);
  value8 = EEPROM.read(7);
  value9 = EEPROM.read(8);
  value10 = EEPROM.read(9);
  value11 = EEPROM.read(10);
  value12 = EEPROM.read(11);
  value13 = EEPROM.read(12);
  value14 = EEPROM.read(13);
  value15 = EEPROM.read(14);
  value16 = EEPROM.read(15);
  value17 = EEPROM.read(16);
  value18 = EEPROM.read(17);
  value19 = EEPROM.read(18);
  value20 = EEPROM.read(19);
  value21 = EEPROM.read(20);
  value22 = EEPROM.read(21);
  value23 = EEPROM.read(22);
  value24 = EEPROM.read(23);
  value25 = EEPROM.read(24);
  value26 = EEPROM.read(25);
  value27 = EEPROM.read(26);
  value28 = EEPROM.read(27);
  value29 = EEPROM.read(28);
  value30 = EEPROM.read(29);
  value31 = EEPROM.read(30);
  value32 = EEPROM.read(31);
  value33 = EEPROM.read(32);
  value34 = EEPROM.read(33);
  value35 = EEPROM.read(34);
  value36 = EEPROM.read(35);
  value37 = EEPROM.read(36);
  value38 = EEPROM.read(37);
  value39 = EEPROM.read(38);
  value40 = EEPROM.read(39);
  value41 = EEPROM.read(40);
  value42 = EEPROM.read(41);
  value43 = EEPROM.read(42);
  value44 = EEPROM.read(43);
  value45 = EEPROM.read(44);
  value46 = EEPROM.read(45);
  value47 = EEPROM.read(46);
  value48 = EEPROM.read(47);
  value49 = EEPROM.read(48);
  value50 = EEPROM.read(49);
  value51 = EEPROM.read(50);
  value52 = EEPROM.read(51);
  value53 = EEPROM.read(52);
  value54 = EEPROM.read(53);
  value55 = EEPROM.read(54);
  value56 = EEPROM.read(55);
  value57 = EEPROM.read(56);
  value58 = EEPROM.read(57);
  value59 = EEPROM.read(58);
  value60 = EEPROM.read(59);
  value61 = EEPROM.read(60);
  value62 = EEPROM.read(61);
  value63 = EEPROM.read(62);   
  value64 = EEPROM.read(63);
  value65 = EEPROM.read(64); 

int allbytes = (value1) + (value2) + (value3) + (value4) + (value5) + (value6) + (value7) + (value8) + (value9) + (value10) + (value11) + (value12) + (value13) + (value14) + (value15) + (value16) + (value17) + (value18) + (value19) + (value20) + (value21) + (value22) + (value23) + (value24) + (value25) + (value26) + (value27) + (value28) + (value29) + (value30) + (value31) + (value32) + (value33) + (value34) + (value35) + (value36) + (value37) + (value38) + (value39) + (value40) + (value41) + (value42) + (value43) + (value44) + (value45) + (value46) + (value47) + (value48) + (value49) + (value50) + (value51) + (value52) + (value53) + (value54) + (value55) + (value56) + (value57) + (value58) + (value59) + (value60) + (value61) + (value62) + (value63) + (value64) + (value65);



if (nowm - EEPROM.read(71) == 1)
{


  if (EEPROM.read(71) ==1) { 
    if(nowm == 2)
      { 
      lcd.print(allbytes / ((datav)*2));
      delay (5000);
      }}

  else if (EEPROM.read(71) == 2) { 
    if (nowm == 3)
      {
      lcd.print(allbytes/(datav1*2));
      delay(5000);}}

  else if (EEPROM.read(71) == 3) {
    if (nowm == 4)
      {
      lcd.print(allbytes/((datav2)*2));
      delay(5000);}}

  else if (EEPROM.read(71) == 4) {
    if (nowm == 5)
      {
      lcd.print(allbytes/((datav3)*2));
      delay(5000);
      }}

  else if (EEPROM.read(71) == 5){ 
    if(nowm == 6)
      {
      lcd.print(allbytes/((datav4)*2));
      delay(5000);
      }}

  else if (EEPROM.read(71) == 6) {
    if (nowm == 7)
      {
      lcd.print(allbytes/((datav5)*2));
      delay(5000);}}

  else if (EEPROM.read(71) == 7) { 
    if (nowm == 8)
      {
      lcd.print(allbytes/((datav6)*2));
      delay(5000);}}

  else if (EEPROM.read(71) == 8) {
    if(nowm == 9)
       {
       lcd.print(allbytes/((datav7)*2));
       delay(5000);}}

  else if (EEPROM.read(71) == 9) {
    if (nowm == 10)
      {
      lcd.print(allbytes/((datav8)*2));
      delay(5000);
      }}

  else if (EEPROM.read(71) == 10) {
    if (nowm == 11)
      {
      lcd.print(allbytes/((datav9)*2));
      delay(5000);}}

  else if (EEPROM.read(71) == 11) {
    if (nowm == 12)
      {
      lcd.print(allbytes/((datav10)*2));
      delay(5000);}}

  else if (EEPROM.read(71) == 12) {
    if (nowm == 1)
      {
      lcd.print(allbytes/((datav11)*2));
      delay(5000);}}

    }


else if  (nowm - EEPROM.read(72) == 0) 
  {
    
    lcd.print(allbytes/((datav001)*2));
  
  }
}

The code above is checking the month. For different months there is different amount of days. I then have it read the Ultrasonic sensor, depthping, and calculate the average. The keypad, ultrasound, and rtc all work.

The problem that I am having is when I press B nothing happens; the screen is still on the menu page. Does any one now what is wrong. Sorry that is a lot of code above.

Average is the sum of items divided by number of items.

I am adding the depthpings taken at certain times, twice a day, and dividing them by the amount of days that have passed.

int allbytes = (value1) + (value2) + (value3) + (value4) + (value5) + (value6) + (value7) + (value8) + (value9) + (value10) + (value11) + (value12) + (value13) + (value14) + (value15) + (value16) + (value17) + (value18) + (value19) + (value20) + (value21) + (value22) + (value23) + (value24) + (value25) + (value26) + (value27) + (value28) + (value29) + (value30) + (value31) + (value32) + (value33) + (value34) + (value35) + (value36) + (value37) + (value38) + (value39) + (value40) + (value41) + (value42) + (value43) + (value44) + (value45) + (value46) + (value47) + (value48) + (value49) + (value50) + (value51) + (value52) + (value53) + (value54) + (value55) + (value56) + (value57) + (value58) + (value59) + (value60) + (value61) + (value62) + (value63) + (value64) + (value65);

Should be allbytes/65 shouldn't it. Make sure you know what average means.

The integer allybytes adds all depthpings taken and then it divides by the the amount of days. In other words I am taking of the EEPROM values up to 65 since that is all I will need in a month and adding them even if some are still 0. Because I added some that are still 0 I cannot divide by 65 so I divide by the amount of days that have passed, since I first started data logging.

for (int i = 0; i < 4095; i++)
EEPROM.write(i, 0);
EEPROM.write(70, nowy);

EEPROM.write(71, nowm);

EEPROM.write(72, nowd);}
break;

value1 = EEPROM.read(0);  
  value2 = EEPROM.read(1); 
  value3 = EEPROM.read(2);
  value4 = EEPROM.read(3);
  value5 = EEPROM.read(4);
  value6 = EEPROM.read(5);
  value7 = EEPROM.read(6);
  value8 = EEPROM.read(7);
  value9 = EEPROM.read(8);
  value10 = EEPROM.read(9);
  value11 = EEPROM.read(10);
  value12 = EEPROM.read(11);
  value13 = EEPROM.read(12);
  value14 = EEPROM.read(13);
  value15 = EEPROM.read(14);
  value16 = EEPROM.read(15);
  value17 = EEPROM.read(16);
  value18 = EEPROM.read(17);
  value19 = EEPROM.read(18);
  value20 = EEPROM.read(19);
  value21 = EEPROM.read(20);
  value22 = EEPROM.read(21);
  value23 = EEPROM.read(22);
  value24 = EEPROM.read(23);
  value25 = EEPROM.read(24);
  value26 = EEPROM.read(25);
  value27 = EEPROM.read(26);
  value28 = EEPROM.read(27);
  value29 = EEPROM.read(28);
  value30 = EEPROM.read(29);
  value31 = EEPROM.read(30);
  value32 = EEPROM.read(31);
  value33 = EEPROM.read(32);
  value34 = EEPROM.read(33);
  value35 = EEPROM.read(34);
  value36 = EEPROM.read(35);
  value37 = EEPROM.read(36);
  value38 = EEPROM.read(37);
  value39 = EEPROM.read(38);
  value40 = EEPROM.read(39);
  value41 = EEPROM.read(40);
  value42 = EEPROM.read(41);
  value43 = EEPROM.read(42);
  value44 = EEPROM.read(43);
  value45 = EEPROM.read(44);
  value46 = EEPROM.read(45);
  value47 = EEPROM.read(46);
  value48 = EEPROM.read(47);
  value49 = EEPROM.read(48);
  value50 = EEPROM.read(49);
  value51 = EEPROM.read(50);
  value52 = EEPROM.read(51);
  value53 = EEPROM.read(52);
  value54 = EEPROM.read(53);
  value55 = EEPROM.read(54);
  value56 = EEPROM.read(55);
  value57 = EEPROM.read(56);
  value58 = EEPROM.read(57);
  value59 = EEPROM.read(58);
  value60 = EEPROM.read(59);
  value61 = EEPROM.read(60);
  value62 = EEPROM.read(61);
  value63 = EEPROM.read(62);   
  value64 = EEPROM.read(63);
  value65 = EEPROM.read(64);

What’s all that about?
Just sum them (make sure the sum variable is big enough) in a for loop.

for (int i = 0; i < 4095; i++)
    EEPROM.write(i, 0);
    EEPROM.write(70, nowy);
 
    EEPROM.write(71, nowm);

    EEPROM.write(72, nowd);}

Looks like a sure-fire way to wear out your EEPROM.

Huge sets of variables that only differ by having consecutive integers in the name SCREAM “Make an array!”. For example:

value1 = EEPROM.read(0);  
  value2 = EEPROM.read(1); 
  value3 = EEPROM.read(2);
  value4 = EEPROM.read(3);
  value5 = EEPROM.read(4);
  value6 = EEPROM.read(5);
  value7 = EEPROM.read(6);
  value8 = EEPROM.read(7);
  value9 = EEPROM.read(8);
  value10 = EEPROM.read(9);
  value11 = EEPROM.read(10);
  value12 = EEPROM.read(11);
  value13 = EEPROM.read(12);
  value14 = EEPROM.read(13);
  value15 = EEPROM.read(14);
  value16 = EEPROM.read(15);
  value17 = EEPROM.read(16);
  value18 = EEPROM.read(17);
  value19 = EEPROM.read(18);
  value20 = EEPROM.read(19);
  value21 = EEPROM.read(20);
  value22 = EEPROM.read(21);
  value23 = EEPROM.read(22);
  value24 = EEPROM.read(23);
  value25 = EEPROM.read(24);
  value26 = EEPROM.read(25);
  value27 = EEPROM.read(26);
  value28 = EEPROM.read(27);
  value29 = EEPROM.read(28);
  value30 = EEPROM.read(29);
  value31 = EEPROM.read(30);
  value32 = EEPROM.read(31);
  value33 = EEPROM.read(32);
  value34 = EEPROM.read(33);
  value35 = EEPROM.read(34);
  value36 = EEPROM.read(35);
  value37 = EEPROM.read(36);
  value38 = EEPROM.read(37);
  value39 = EEPROM.read(38);
  value40 = EEPROM.read(39);
  value41 = EEPROM.read(40);
  value42 = EEPROM.read(41);
  value43 = EEPROM.read(42);
  value44 = EEPROM.read(43);
  value45 = EEPROM.read(44);
  value46 = EEPROM.read(45);
  value47 = EEPROM.read(46);
  value48 = EEPROM.read(47);
  value49 = EEPROM.read(48);
  value50 = EEPROM.read(49);
  value51 = EEPROM.read(50);
  value52 = EEPROM.read(51);
  value53 = EEPROM.read(52);
  value54 = EEPROM.read(53);
  value55 = EEPROM.read(54);
  value56 = EEPROM.read(55);
  value57 = EEPROM.read(56);
  value58 = EEPROM.read(57);
  value59 = EEPROM.read(58);
  value60 = EEPROM.read(59);
  value61 = EEPROM.read(60);
  value62 = EEPROM.read(61);
  value63 = EEPROM.read(62);   
  value64 = EEPROM.read(63);
  value65 = EEPROM.read(64); 

int allbytes = (value1) + (value2) + (value3) + (value4) + (value5) + (value6) + (value7) + (value8) + (value9) + (value10) + (value11) + (value12) + (value13) + (value14) + (value15) + (value16) + (value17) + (value18) + (value19) + (value20) + (value21) + (value22) + (value23) + (value24) + (value25) + (value26) + (value27) + (value28) + (value29) + (value30) + (value31) + (value32) + (value33) + (value34) + (value35) + (value36) + (value37) + (value38) + (value39) + (value40) + (value41) + (value42) + (value43) + (value44) + (value45) + (value46) + (value47) + (value48) + (value49) + (value50) + (value51) + (value52) + (value53) + (value54) + (value55) + (value56) + (value57) + (value58) + (value59) + (value60) + (value61) + (value62) + (value63) + (value64) + (value65);

This should look more like:

int allbytes;
const int NumberOfSamples = 66;
allbytes = 0;
for (int i=0; i<NumberOfSamples; i++)
    allbytes += EEPROM.read(i);

Same effect in 5 lines instead of 68 lines.

I think your bug may be in the part of the code you DIDN’T show. If you post your code I can re-write it for you using arrays. That will make it much more compact and easier to maintain. I might find a coding mistake and fix that, too.

Okay thank you I am going to have to use to posts though because it exceeds 900 characters.

//Ultrasound setup
#include <NewPing.h>// includes Ultrasonic sensor libraray in this sketch
#define TRIGGER_PIN  22  // Arduino pin connected to trigger pin on Ultrasound.
#define ECHO_PIN     23  // Arduino pin connected to echo pin on Ultrasound.
#define MAX_DISTANCE 182 // The max distence of the Ultrasound

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // Configures top three definitions, NewPing setup of pins and maximum distance.

//Keypad Setup
#include <Keypad.h> //includes keypad library in this sketch

const byte rows = 4; //four rows
const byte cols = 4; //three columns
char keys[rows][cols] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[rows] = {41, 43, 45, 47}; //connect to the row pinouts of the keypad
byte colPins[cols] = {33, 35, 37, 39}; //connect to the column pinouts of the keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, rows, cols );


//LCD setup
#include <LiquidCrystal.h> // includes library for the LCD commands
 
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8,9,10,11,12,13); // define our LCD and which pins to use


#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;


#include <EEPROM.h>
int address = 0;
  int addr = 0;
  byte value;
  byte value1;
  byte value2; 
  byte value3;
  byte value4;
  byte value5;
  byte value6;  
  byte value7;
  byte value8;
  byte value9;
  byte value10;
  byte value11;
  byte value12;
  byte value13;
  byte value14;
  byte value15;
  byte value16;
  byte value17;
  byte value18;
  byte value19;
  byte value20;
  byte value21;
  byte value22;  
  byte value23;
  byte value24;
  byte value25;
  byte value26;
  byte value27;
  byte value28;
  byte value29;
  byte value30;
  byte value31;
  byte value32;
  byte value33;
  byte value34; 
  byte value35;
  byte value36;
  byte value37;
  byte value38;  
  byte value39;
  byte value40;
  byte value41;
  byte value42;
  byte value43;
  byte value44;
  byte value45;
  byte value46;
  byte value47;
  byte value48;
  byte value49;
  byte value50;
  byte value51;
  byte value52;
  byte value53;
  byte value54;  
  byte value55;
  byte value56;
  byte value57;
  byte value58;
  byte value59;
  byte value60;
  byte value61;
  byte value62;
  byte value63;
  byte value64;
  byte value65; 
 


//Global Variables
   // Ds1307 setup 
 
  int POT_VOLTAGE = 5;
  int tankdepth = 182 ;
  int val = 5;
  int sensorValue = analogRead(A0);
  float voltage = sensorValue * (POT_VOLTAGE / 1023.0);
  int depthcircuit = (sensorValue*.178);
  unsigned int uS = sonar.ping();
  int depthping = (tankdepth)-(uS / US_ROUNDTRIP_CM);


void setup () {
   Serial.begin(57600);
#ifdef AVR
  Wire.begin();
#else
  Wire1.begin(); 
#endif
  rtc.begin();

  
// configuring the LCD
  lcd.begin(40, 2); // need to specify how many columns and rows are in the LCD unit
  lcd.clear();      // this clears the LCD. You can use this at any time
 lcd.print("Welcome to Resource Management with     Arduino               ");
delay(2500);
lcd.clear();
}
  
  
  
void loop() 
{
 DateTime now = rtc.now();
  const int nowy = (now.year()- 2000);
  const int nowyy = (now.year());
  const int nowm = (now.month());
  const int nowd = (now.day());  
  const int nowh = (now.hour());
  const int nowmin = (now.minute());
  const int nowsec = (now.second()); 

  int datav = ((29 - nowd) + (31- EEPROM.read(72)));
  int datav1 = (31 - nowd) + (29 - EEPROM.read(72));
  int datav2 = (30 - nowd) + (31 - EEPROM.read(72));
  int datav3 = (31 - nowd) + (30 - EEPROM.read(72));
  int datav4 = (30 - nowd) + (31 - EEPROM.read(72));
  int datav5 = (31 - nowd) + (30 - EEPROM.read(72));
  int datav6 = (31 - nowd) + (31 - EEPROM.read(72));
  int datav7 = (30 - nowd) + (31 - EEPROM.read(72));
  int datav8 = (31 - nowd) + (30 - EEPROM.read(72));
  int datav9 = (30 - nowd) + (31 - EEPROM.read(72));
  int datav10 = (30 - nowd) + (31 - EEPROM.read(72));
  int datav11 = (31 - nowd) + (31 - EEPROM.read(72));
  int datav001 = ((nowd) - (EEPROM.read(72)));
  
// mass wate usage alert system
if ((nowh) == 9)
  {
    unsigned int uS = sonar.ping();
    int depthping = (tankdepth)-(uS / US_ROUNDTRIP_CM);
    EEPROM.write(70, depthping);
  }
if ((nowh) == 21)
  {
   value = EEPROM.read(707);  
   unsigned int uS = sonar.ping();
   int depthping = (tankdepth)-(uS / US_ROUNDTRIP_CM);
   (value -(tankdepth)-(uS / US_ROUNDTRIP_CM));
   if (value -(tankdepth)-(uS / US_ROUNDTRIP_CM)>(5))
   lcd.print("mass water usage over 5cm!");
  }

//Averging system
if (now.hour() == 6)
if (now.minute()== 40) 
 if (now.second() == 1)
  
  {
    
    EEPROM.write(addr, val);
    address = address + 1;
    delay(999);
    
  }

if (now.hour() == 21)
 if (now.minute()== 40) 
 if (now.second() == 1)
  {

    EEPROM.write(addr, val);
    address = address + 1;
    delay(999);
 
 
  }






//Menu

lcd.setCursor(0,0);
lcd.print("Press A for depth,");
lcd.print("  Press B for Average,");
lcd.setCursor(0,1);
lcd.println("Press C when refill & D for next order");
  
{ 
 //keycheck
  char key = kpd.getKey(); //checks for keystroke in keypad
  if(key)  // Checking for a correct key.
// mass wate usage alert system

 
 
 
  {
    switch (key)
    {
     
case 'A':
 {
 lcd.clear();
  unsigned int uS = sonar.ping();
 int depthping = (tankdepth)-(uS / US_ROUNDTRIP_CM);
 lcd.print("Amount Remaining is ");
 lcd.print((tankdepth)-(uS / US_ROUNDTRIP_CM));
 lcd.print("cm");
 delay(5000);
 }
 break;
case 'B': 
{
value1 = EEPROM.read(0);  
  value2 = EEPROM.read(1); 
  value3 = EEPROM.read(2);
  value4 = EEPROM.read(3);
  value5 = EEPROM.read(4);
  value6 = EEPROM.read(5);
  value7 = EEPROM.read(6);
  value8 = EEPROM.read(7);
  value9 = EEPROM.read(8);
  value10 = EEPROM.read(9);
  value11 = EEPROM.read(10);
  value12 = EEPROM.read(11);
  value13 = EEPROM.read(12);
  value14 = EEPROM.read(13);
  value15 = EEPROM.read(14);
  value16 = EEPROM.read(15);
  value17 = EEPROM.read(16);
  value18 = EEPROM.read(17);
  value19 = EEPROM.read(18);
  value20 = EEPROM.read(19);
  value21 = EEPROM.read(20);
  value22 = EEPROM.read(21);
  value23 = EEPROM.read(22);
  value24 = EEPROM.read(23);
  value25 = EEPROM.read(24);
  value26 = EEPROM.read(25);
  value27 = EEPROM.read(26);
  value28 = EEPROM.read(27);
  value29 = EEPROM.read(28);
  value30 = EEPROM.read(29);
  value31 = EEPROM.read(30);
  value32 = EEPROM.read(31);
  value33 = EEPROM.read(32);
  value34 = EEPROM.read(33);
  value35 = EEPROM.read(34);
  value36 = EEPROM.read(35);
  value37 = EEPROM.read(36);
  value38 = EEPROM.read(37);
  value39 = EEPROM.read(38);
  value40 = EEPROM.read(39);
  value41 = EEPROM.read(40);
  value42 = EEPROM.read(41);
  value43 = EEPROM.read(42);
  value44 = EEPROM.read(43);
  value45 = EEPROM.read(44);
  value46 = EEPROM.read(45);
  value47 = EEPROM.read(46);
  value48 = EEPROM.read(47);
  value49 = EEPROM.read(48);
  value50 = EEPROM.read(49);
  value51 = EEPROM.read(50);
  value52 = EEPROM.read(51);
  value53 = EEPROM.read(52);
  value54 = EEPROM.read(53);
  value55 = EEPROM.read(54);
  value56 = EEPROM.read(55);
  value57 = EEPROM.read(56);
  value58 = EEPROM.read(57);
  value59 = EEPROM.read(58);
  value60 = EEPROM.read(59);
  value61 = EEPROM.read(60);
  value62 = EEPROM.read(61);
  value63 = EEPROM.read(62);   
  value64 = EEPROM.read(63);
  value65 = EEPROM.read(64); 

int allbytes = (value1) + (value2) + (value3) + (value4) + (value5) + (value6) + (value7) + (value8) + (value9) + (value10) + (value11) + (value12) + (value13) + (value14) + (value15) + (value16) + (value17) + (value18) + (value19) + (value20) + (value21) + (value22) + (value23) + (value24) + (value25) + (value26) + (value27) + (value28) + (value29) + (value30) + (value31) + (value32) + (value33) + (value34) + (value35) + (value36) + (value37) + (value38) + (value39) + (value40) + (value41) + (value42) + (value43) + (value44) + (value45) + (value46) + (value47) + (value48) + (value49) + (value50) + (value51) + (value52) + (value53) + (value54) + (value55) + (value56) + (value57) + (value58) + (value59) + (value60) + (value61) + (value62) + (value63) + (value64) + (value65);



if (nowm - EEPROM.read(71) == 1)
{


  if (EEPROM.read(71) ==1) { 
    if(nowm == 2)
      { 
      lcd.print(allbytes / ((datav)*2));
      delay (5000);
      }}

  else if (EEPROM.read(71) == 2) { 
    if (nowm == 3)
      {
      lcd.print(allbytes/(datav1*2));
      delay(5000);}}

  else if (EEPROM.read(71) == 3) {
    if (nowm == 4)
      {
      lcd.print(allbytes/((datav2)*2));
      delay(5000);}}

  else if (EEPROM.read(71) == 4) {
    if (nowm == 5)
      {
      lcd.print(allbytes/((datav3)*2));
      delay(5000);
      }}

  else if (EEPROM.read(71) == 5){ 
    if(nowm == 6)
      {
      lcd.print(allbytes/((datav4)*2));
      delay(5000);
      }}

  else if (EEPROM.read(71) == 6) {
    if (nowm == 7)
      {
      lcd.print(allbytes/((datav5)*2));
      delay(5000);}}

  else if (EEPROM.read(71) == 7) { 
    if (nowm == 8)
      {
      lcd.print(allbytes/((datav6)*2));
      delay(5000);}}

  else if (EEPROM.read(71) == 8) {
    if(nowm == 9)
       {
       lcd.print(allbytes/((datav7)*2));
       delay(5000);}}

  else if (EEPROM.read(71) == 9) {
    if (nowm == 10)
      {
      lcd.print(allbytes/((datav8)*2));
      delay(5000);
      }}

  else if (EEPROM.read(71) == 10) {
    if (nowm == 11)
      {
      lcd.print(allbytes/((datav9)*2));
      delay(5000);}}

  else if (EEPROM.read(71) == 11) {
    if (nowm == 12)
      {
      lcd.print(allbytes/((datav10)*2));
      delay(5000);}}

  else if (EEPROM.read(71) == 12) {
    if (nowm == 1)
      {
      lcd.print(allbytes/((datav11)*2));
      delay(5000);}}

    }


else if  (nowm - EEPROM.read(72) == 0) 
  {
    
    lcd.print(allbytes/((datav001)*2));
  
  }
}
break;

case 'C':
{lcd.clear();
   for (int i = 0; i < 4095; i++)
    EEPROM.write(i, 0);
    EEPROM.write(70, nowy);
 
    EEPROM.write(71, nowm);

    EEPROM.write(72, nowd);}
    break;

case 'D':
if (nowm - EEPROM.read(71) == 1)
{
int allbytes = (value1) + (value2) + (value3) + (value4) + (value5) + (value6) + (value7) + (value8) + (value9) + (value10) + (value11) + (value12) + (value13) + (value14) + (value15) + (value16) + (value17) + (value18) + (value19) + (value20) + (value21) + (value22) + (value23) + (value24) + (value25) + (value26) + (value27) + (value28) + (value29) + (value30) + (value31) + (value32) + (value33) + (value34) + (value35) + (value36) + (value37) + (value38) + (value39) + (value40) + (value41) + (value42) + (value43) + (value44) + (value45) + (value46) + (value47) + (value48) + (value49) + (value50) + (value51) + (value52) + (value53) + (value54) + (value55) + (value56) + (value57) + (value58) + (value59) + (value60) + (value61) + (value62) + (value63) + (value64) + (value65);



  if (EEPROM.read(71) ==1) { 
    if(nowm == 2)
      { 
        lcd.print(depthping / (allbytes / ((datav)*2)));
      delay (5000);
      }}

  else if (EEPROM.read(71) == 2) { 
    if (nowm == 3)
      {
      lcd.print(depthping /(allbytes/(datav1*2)));
      delay(5000);}}

  else if (EEPROM.read(71) == 3) {
    if (nowm == 4)
      {
      lcd.print(depthping / (allbytes/((datav2)*2)));
      delay(5000);}}

  else if (EEPROM.read(71) == 4) {
    if (nowm == 5)
      {
      lcd.print(depthping / (allbytes/((datav3)*2)));
      delay(5000);
      }}

  else if (EEPROM.read(71) == 5){ 
    if(nowm == 6)
      {
      lcd.print(depthping/ (allbytes/((datav4)*2)));
      delay(5000);
      }}

  else if (EEPROM.read(71) == 6) {
    if (nowm == 7)
      {
      lcd.print(depthping/(allbytes/((datav5)*2)));
      delay(5000);}}

  else if (EEPROM.read(71) == 7) { 
    if (nowm == 8)
      {
      lcd.print(depthping/(allbytes/((datav6)*2)));
      delay(5000);}}

  else if (EEPROM.read(71) == 8) {
    if(nowm == 9)
       {
       lcd.print(depthping/(allbytes/((datav7)*2)));
       delay(5000);}}

  else if (EEPROM.read(71) == 9) {
    if (nowm == 10)
      {
      lcd.print(depthping/(allbytes/((datav8)*2)));
      delay(5000);
      }}

  else if (EEPROM.read(71) == 10) {
    if (nowm == 11)
      {
      lcd.print(depthping/(allbytes/((datav9)*2)));
      delay(5000);}}

  else if (EEPROM.read(71) == 11) {
    if (nowm == 12)
      {
      lcd.print(depthping/(allbytes/((datav10)*2)));
      delay(5000);}}

  else if (EEPROM.read(71) == 12) {
    if (nowm == 1)
      {
      lcd.print(depthping/(allbytes/((datav11)*2)));
      delay(5000);}}

    }


else if  (nowm - EEPROM.read(72) == 0) 
  {
    int allbytes = (value1) + (value2) + (value3) + (value4) + (value5) + (value6) + (value7) + (value8) + (value9) + (value10) + (value11) + (value12) + (value13) + (value14) + (value15) + (value16) + (value17) + (value18) + (value19) + (value20) + (value21) + (value22) + (value23) + (value24) + (value25) + (value26) + (value27) + (value28) + (value29) + (value30) + (value31) + (value32) + (value33) + (value34) + (value35) + (value36) + (value37) + (value38) + (value39) + (value40) + (value41) + (value42) + (value43) + (value44) + (value45) + (value46) + (value47) + (value48) + (value49) + (value50) + (value51) + (value52) + (value53) + (value54) + (value55) + (value56) + (value57) + (value58) + (value59) + (value60) + (value61) + (value62) + (value63) + (value64) + (value65);

    lcd.print(depthping /(allbytes/((datav001)*2)));
  
  }

break;
}}}
}

One question: Over what period did you want to average? It looks like maybe you are trying to average from the the same day the previous month. That gets very hairy, particularly if the previous month doesn't have the same day (like March 30th). Couldn't you just have a 30-day moving average?

Hello The period of the average is determined by how many days have passed. But I think you are right that it gets difficult when you have to have all those variables, so I suppose it would be best to have a 30-day moving average.

    EEPROM.write(addr, depthping);
    address = address + 1;
    delay(999);

.. addr vs address ?!

addr is referring to the writing, address is referring to the reading

Here is a version using a 30-day moving average. If it has been less than one day since a reset (‘C’ button) the averages (‘B’ and ‘D’ buttons) will say “Not enough data.”. If your tank is a constant cross-section you can display the numbers in liters by multiplying depth in cm by the cross section in square cm.

//Ultrasound setup
#include <NewPing.h>// includes Ultrasonic sensor libraray in this sketch
#define TRIGGER_PIN  22  // Arduino pin connected to trigger pin on Ultrasound.
#define ECHO_PIN     23  // Arduino pin connected to echo pin on Ultrasound.
#define MAX_DISTANCE 182 // The max distence of the Ultrasound

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // Configures top three definitions, NewPing setup of pins and maximum distance.

//Keypad Setup
#include <Keypad.h> //includes keypad library in this sketch

const byte rows = 4; //four rows
const byte cols = 4; //three columns
const char keys[rows][cols] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[rows] = {41, 43, 45, 47}; //connect to the row pinouts of the keypad
byte colPins[cols] = {33, 35, 37, 39}; //connect to the column pinouts of the keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, rows, cols );


//LCD setup
#include <LiquidCrystal.h> // includes library for the LCD commands

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 10, 11, 12, 13); // define our LCD and which pins to use


#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;


#include <EEPROM.h>

const int TANK_DEPTH = 182;
const int MAX_DEPTH_COUNT = 60;  // 60-sample (30 day) circular buffer

/* EEPROM address values */
const int EEPROM_ADDR_DepthBuffer = 0; // Circular buffer: 0 through 59
const int EEPROM_ADDR_DepthIndex = 68; // 0-59, current index in circular buffer
const int EEPROM_ADDR_PeakDepthIndex = 69; // 0-59, peak number of samples in circular buffer
const int EEPROM_ADDR_ResetYear = 70;
const int EEPROM_ADDR_ResetMonth = 71;
const int EEPROM_ADDR_ResetDay = 72;

byte DepthIndex = EEPROM.read(EEPROM_ADDR_DepthIndex);
byte PeakDepthIndex = EEPROM.read(EEPROM_ADDR_PeakDepthIndex);
byte ResetYear = EEPROM.read(EEPROM_ADDR_ResetYear);
byte ResetMonth = EEPROM.read(EEPROM_ADDR_ResetMonth);
byte ResetDay = EEPROM.read(EEPROM_ADDR_ResetDay);

void setup () {
  Serial.begin(57600);

  Wire.begin();
  rtc.begin();

  // configuring the LCD
  lcd.begin(40, 2); // need to specify how many columns and rows are in the LCD unit
  lcd.clear();      // this clears the LCD. You can use this at any time
  lcd.print("Welcome to Resource Management with     Arduino               ");
  delay(2500);
  lcd.clear();
}

void loop() {
  DateTime now = rtc.now();

  int currentDepth = TANK_DEPTH - sonar.convert_cm(sonar.ping_median(10));

  // Store samples for future averaging
  // Take a samples at 6:40:01 AM and 9:40:01 PM
  if ((now.hour() == 6 && now.minute() == 40 && now.second() == 1) ||
      (now.hour() == 21 && now.minute() == 40 && now.second() == 1)) {

    EEPROM.write(EEPROM_ADDR_DepthBuffer + DepthIndex, currentDepth);
    delay(1000);

    // mass water usage alert system
    byte previousDepth;
    if (PeakDepthIndex > 0) { // This is not the first sample since reset
      if (DepthIndex == 0) // We just wrapped from 59 to 0
        previousDepth = EEPROM.read(EEPROM_ADDR_DepthBuffer + MAX_DEPTH_COUNT - 1);
      else
        previousDepth = EEPROM.read(EEPROM_ADDR_DepthBuffer + DepthIndex - 1);
      // check for depth going down more than 5 cm since last sample.
      if (previousDepth - currentDepth > 5)
        lcd.print("mass water usage over 5cm!");
    }

    // Now increment the index
    DepthIndex = (DepthIndex + 1) % MAX_DEPTH_COUNT;
    EEPROM.write(EEPROM_ADDR_DepthIndex, DepthIndex);
    if (DepthIndex > PeakDepthIndex) {
      PeakDepthIndex = DepthIndex;
      EEPROM.write(EEPROM_ADDR_PeakDepthIndex, PeakDepthIndex);
    }
    delay(1000);
  }


  //Menu

  lcd.setCursor(0, 0);
  lcd.print("Press A for depth, Press B for Average,");
  lcd.setCursor(0, 1);
  lcd.println("Press C when refill & D for next order");

  // Act based on keypad input.
  switch (kpd.getKey()) {

    case 0:  // No key pressed
      break;

    case 'A':  // Display current reading
      lcd.clear();
      lcd.print("Amount Remaining is ");
      lcd.print(currentDepth);
      lcd.print("cm");
      delay(5000);
      break;

    case 'B':  // Show average remaining over the past (up to) 30 days
      if (PeakDepthIndex < 2) {
        lcd.print("Not enough data");
        break;
      }
      {
        int averageDepth = 0;
        for (int i = 0; i <= PeakDepthIndex; i++)
          averageDepth += EEPROM.read(EEPROM_ADDR_DepthBuffer + i);
        averageDepth /= PeakDepthIndex + 1;

        lcd.print(averageDepth);
        delay (5000);
      }
      break;

    case 'C':
      lcd.clear();
      // Empty the sample buffer
      for (int i = 0; i < MAX_DEPTH_COUNT; i++)
        EEPROM.write(EEPROM_ADDR_DepthBuffer, 0);
      DepthIndex = 0;
      EEPROM.write(EEPROM_ADDR_DepthIndex, DepthIndex);
      PeakDepthIndex = 0;
      EEPROM.write(EEPROM_ADDR_PeakDepthIndex, PeakDepthIndex);
      EEPROM.write(EEPROM_ADDR_ResetYear, now.year() - 2000);
      EEPROM.write(EEPROM_ADDR_ResetMonth, now.month());
      EEPROM.write(EEPROM_ADDR_ResetDay, now.day());
      break;

    case 'D':  // Show days remaining at average daily usage
      if (PeakDepthIndex < 2) {
        lcd.print("Not enough data");
        break;
      }
      {
        int oldestDepthIndex = (PeakDepthIndex > DepthIndex) ? DepthIndex : 0;
        // Subtrace the current depth from the oldest depth
        float averageUsage = EEPROM.read(EEPROM_ADDR_DepthBuffer + oldestDepthIndex);
        averageUsage -= currentDepth;
        // Calculate the number of days since the oldest sample
        float days = (PeakDepthIndex + 1) / 2.0;
        // Calculate the average usage per day
        averageUsage /= days;
        // Display the current depth divided by the average daily usage
        lcd.print(currentDepth / averageUsage);
        delay (5000);
      }
      break;

  }
}

Thank you so much that looks fabulous! You have been a huge help! Tom

Quick question. How is the following code in a circular buffer.

/* EEPROM address values */ const int EEPROM_ADDR_DepthBuffer = 0; // Circular buffer: 0 through 59 const int EEPROM_ADDR_DepthIndex = 68; // 0-59, current index in circular buffer const int EEPROM_ADDR_PeakDepthIndex = 69; // 0-59, peak number of samples in circular buffer const int EEPROM_ADDR_ResetYear = 70; const int EEPROM_ADDR_ResetMonth = 71; const int EEPROM_ADDR_ResetDay = 72;

byte DepthIndex = EEPROM.read(EEPROM_ADDR_DepthIndex); byte PeakDepthIndex = EEPROM.read(EEPROM_ADDR_PeakDepthIndex); byte ResetYear = EEPROM.read(EEPROM_ADDR_ResetYear); byte ResetMonth = EEPROM.read(EEPROM_ADDR_ResetMonth); byte ResetDay = EEPROM.read(EEPROM_ADDR_ResetDay);

Excuse my ignorance.

tom15: Quick question. How is the following code in a circular buffer.

/* EEPROM address values */ const int EEPROM_ADDR_DepthBuffer = 0; // Circular buffer: 0 through 59 const int EEPROM_ADDR_DepthIndex = 68; // 0-59, current index in circular buffer const int EEPROM_ADDR_PeakDepthIndex = 69; // 0-59, peak number of samples in circular buffer

byte DepthIndex = EEPROM.read(EEPROM_ADDR_DepthIndex); byte PeakDepthIndex = EEPROM.read(EEPROM_ADDR_PeakDepthIndex);

The circular buffer part is:

 int currentDepth = TANK_DEPTH - sonar.convert_cm(sonar.ping_median(10));

  // Store samples for future averaging
  // Take a samples at 6:40:01 AM and 9:40:01 PM
  if ((now.hour() == 6 && now.minute() == 40 && now.second() == 1) ||
      (now.hour() == 21 && now.minute() == 40 && now.second() == 1)) {

    // Write into the circular buffer
    EEPROM.write(EEPROM_ADDR_DepthBuffer + DepthIndex, currentDepth);

    // Now increment the index.  If it goes beyond 59, wrap around to 0. THAT'S WHAT MAKES IT CIRCULAR
    DepthIndex = (DepthIndex + 1) % MAX_DEPTH_COUNT;

   // Remember the value in case power is interrupted
    EEPROM.write(EEPROM_ADDR_DepthIndex, DepthIndex);

   // If the buffer wis not full yet (60 samples), keep track of how full it is so we can average the right number of samples
    if (DepthIndex > PeakDepthIndex) {
      PeakDepthIndex = DepthIndex;
      // Remember the value in case power is interrupted
      EEPROM.write(EEPROM_ADDR_PeakDepthIndex, PeakDepthIndex);
    }
    delay(1000); // make sure the second is over so we don't store another sample until next time.
  }

Hi,

I am adding the depthpings taken at certain times, twice a day, and dividing them by the amount of days that have passed

If you took depth pings ONCE a day and divided by days (28,29,30 or 31, which ever), then you will have the average DAILY reading over that month.

Taking TWO readings per day, you need to divide by 2 x the number of days to get the average DAILY reading over the month.

A mathematical average is the sum of a list of numbers divided by the [u]number of numbers[/u] in the list.

If your code is too big to post in code tags, then if you use the REPLY rather than Quick Reply, you will find a function that allows you to post your code as an attachment.

Tom...... :)

I just realized that this morning after testing it over night that I am getting a an average “reading” so I will fix that. I also got a negative number, 2.21 for the amount of days left until refill, investigating cause.

If your code is too big to post in code tags

. . . it almost certainly needs simplifying.