WattHour Meter Lag 6Watts / Minute

I Try to Build a Power Consumption meter For my e-Bike With Arduino and Blynk to find out remaining charge left in my. I Try to Calculate the Power every Quarter of a second and Add it to The BalanceWatts .

I Can Calculate Everything Except Watt Hour / 250 Microseconds

#include "SimpleTimer.h"
#include <BlynkSimpleSerialBLE.h>



#define voltPIN 6
#define ampPIN 7


char auth[] = "404212715de241cd977f7c2f98e00a10";

// i Choose float for every variable because if i choose int for any of the following it return wrong result


float adcVolt,adcAmp,Volt,Amp,Watt,wattPerQuatersecond;
float balanceWatt = 1780;   // Battery Capacity at Full charge
float refVolt = 5.00; 
float ampOffset = 2500;
float milliPerAmp = 66.00;   // ACS712 Referece
float R1=147000;             // 147k
float R2=10000;              // 10k arduino output impedance match 

    

SimpleTimer myTimer;
WidgetLCD lcd(5);       // Blynk LCD Widget Initiate



  void setup()
      {
        pinMode(voltPIN, INPUT);
        pinMode(ampPIN,INPUT);
      
        Serial.begin(9600);
        Blynk.begin(Serial,auth);

      

        lcd.clear();
        myTimer.setInterval(250,calculate);  // do job every Quater of a secand
     
      }
  
  
  void loop()
      {
        Blynk.run();
        myTimer.run();
      }


    void calculate()
    {
      adcVolt = 900 ; // Tempravory Value
      adcAmp = 200;   // Temp value
      
      float readVolt = ((adcVolt * refVolt) / 1024.00);
       Volt = readVolt/(R2/(R1+R2));

      float readAmp =  (((adcAmp * 5020.00)/1024.00) - ampOffset);           // INSTEAD OF 5000 I PUT 5020 (20) FOR CORRECTION IN AMPS 
       Amp = readAmp / milliPerAmp; 

       Watt = (Volt * Amp); // 1500 W

       wattPerQuatersecond = (Watt / (3600.00*4)); // 0.110W/quaterSecand

       balanceWatt = balanceWatt - wattPerQuatersecond;

        Blynk.virtualWrite(1, Volt);     //Gauge 1
        Blynk.virtualWrite(2, Amp);
        Blynk.virtualWrite(3, Watt);
        Blynk.virtualWrite(4, balanceWatt);  // Gauge 4
      
    }

According to my Code every if my Hub motor consume 1500W then Watt per Quarter Seconds is

1500 / (3600 X 4) = 0.110W/quarterSecand

After one Minute my Battery Balance Power must increased to 1780 + (0.110460) = 1780 + 26.4 = 1806.4

But The Display Shows 1800. and Every Minute it Lag 6W . I Try to Solve this problem From the Last Week but I cannot find out my mistake. please anyone point out my problem and help me to solve. sorry for my bad English,

I Attach my code with this post

// ACS 712 Takes 200 as -27Amps (Reverse Current as Battery Get Charge From a Charger )

Forum_watt_6W_error.ino (1.73 KB)

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile:

TomGeorge:
Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile:

Thanks Tom , I edit My post according to the rule you mentioned.

Hi,
I'm not sure but if

float ampOffset = 2500;

you should;

float ampOffset = 2500.0;

You have defined quite a few variables without decimal point.
Worth a try.

Where are you using " void calculate() "

Tom... :slight_smile:

Where are you using " void calculate() "

   myTimer.setInterval(250,calculate);  // do job every Quater of a secand

Tom... :slight_smile:
[/quote]

Problem Solved . Thankyou so much Sir, you Really Save my lots of Time . and one more thing why need to put 2500.00 instead of 2500.

I read about int and float mathematical operation that shows float of int and float is return float . then why this mistakes. me

float ampOffset = 2500.0;

You have defined quite a few variables without decimal point.
Worth a try.

I change all the integer values to float .

#include "SimpleTimer.h"
#include <BlynkSimpleSerialBLE.h>



#define voltPIN 6
#define ampPIN 7

char auth[] = "404212715de241cd977f7c2f98e00a10";

// i Choose float for every variable because if i choose int for any of the following it return wrong result


float adcVolt,adcAmp,Volt,Amp,Watt,wattPerQuatersecond;
float balanceWatt = 1780.00;   // Battery Capacity at Full charge
float refVolt = 5.00; 
float ampOffset = 2500.00;
float milliPerAmp = 66.00;   // ACS712 Referece
float R1=147000.00;             // 147k
float R2=10000.00;              // 10k arduino output impedance match 

    

SimpleTimer myTimer;
WidgetLCD lcd(5);       // Blynk LCD Widget Initiate



  void setup()
      {
        pinMode(voltPIN, INPUT);
        pinMode(ampPIN,INPUT);
      
        Serial.begin(9600);
        Blynk.begin(Serial,auth);

      

        lcd.clear();
        myTimer.setInterval(250,calculate);  // do job every Quater of a secand
     
      }
  
  
  void loop()
      {
        Blynk.run();
        myTimer.run();
      }


    void calculate()
    {
      adcVolt = 900.00 ; // Tempravory Value
      adcAmp = 200.00;   // Temp value
      
      float readVolt = ((adcVolt * refVolt) / 1024.00);
       Volt = readVolt/(R2/(R1+R2));

      float readAmp =  (((adcAmp * 5020.00)/1024.00) - ampOffset);           // INSTEAD OF 5000 I PUT 5020 (20) FOR CORRECTION IN AMPS 
       Amp = readAmp / milliPerAmp; 

       Watt = (Volt * Amp); // 1500 W

       wattPerQuatersecond = (Watt / (3600.00*4.00)); // 0.110W/quaterSecand

       balanceWatt = balanceWatt - wattPerQuatersecond;

        Blynk.virtualWrite(1, Volt);     //Gauge 1
        Blynk.virtualWrite(2, Amp);
        Blynk.virtualWrite(3, Watt);
        Blynk.virtualWrite(4, balanceWatt);  // Gauge 4
      
    }

PROBLEM COMES AGAIN IF I ADD TIMER WITH ABOVE PROGRAM , WITH OUT myClock() Function it Works Perfect. Any Suggestions ?

#include "SimpleTimer.h"
#include <BlynkSimpleSerialBLE.h>
#include <DHT.h>
#include <EEPROM.h>


#define VOLTPIN 6
#define AMPPIN 7
#define DHTPIN 12
#define DHTTYPE DHT11

char auth[] = "404212715de241cd977f7c2f98e00a10";

float adcVolt,adcAmp,Volt,Amp,Watt,wattPerQuaterSecand;
float balanceWatt = 1780.00;
float refVolt = 5.00; 
float ampOffset = 2500.00;
float milliPerAmp = 66.00;
float R1=147000.00;
float R2=10000.00; 
float Fahrenheit = 0.00;

 unsigned long timeSecands = 0;
 unsigned long timeMinutes = 0;
 unsigned long timeHours = 0;
    

SimpleTimer myTimer;
WidgetLCD lcd(5);
DHT dht(DHTPIN, DHTTYPE);


  void setup()
      {
        pinMode(VOLTPIN, INPUT);
        pinMode(AMPPIN,INPUT);
      
        Serial.begin(9600);
        Blynk.begin(Serial,auth);

        balanceWatt = EEPROMReadint(0);

        lcd.clear();
        myTimer.setInterval(250,calculate);
        myTimer.setInterval(1000,myClock);
        myTimer.setInterval(5000,boardTemp);
        myTimer.setInterval(10000,updateEEPROM);
      }
  
  
 void loop()
      {
        Blynk.run();
        myTimer.run();
      }


 void calculate()
    {
       unsigned int sampleVolt = 0;
       unsigned int sampleAmp = 0;
          
      for(int v=0;v<50;v++)                                          
         {
           sampleVolt = sampleVolt+analogRead(VOLTPIN);   //50 samples for voltage
           sampleAmp = sampleAmp+analogRead(AMPPIN);     //50 samples for current
         }
         adcVolt =  sampleVolt / 50;               //average voltage                   
         adcAmp =   sampleAmp / 50;                //average current
      
      float readVolt = ((adcVolt * refVolt) / 1023.00);
       Volt = readVolt/(R2/(R1+R2));

      float readAmp =  (((adcAmp * 5000.00)/1023.00) - ampOffset);            //INSTEAD OF 5000 I PUT 5020 (20) FOR CORRECTION IN AMPS 
       Amp = readAmp / milliPerAmp; 

       Watt = (Volt * Amp); 

       wattPerQuaterSecand = (Watt / (3600.00*4.00));

       balanceWatt = balanceWatt - wattPerQuaterSecand;

        Blynk.virtualWrite(1, Volt); 
        Blynk.virtualWrite(2, Amp);
        Blynk.virtualWrite(3, Watt);  

       
       lcd.print(1,1,balanceWatt);
      
      
    }

 void myClock()
      {
        timeSecands ++;
        timeMinutes = timeSecands / 60;
        timeHours = timeMinutes / 60;
        

            lcd.print(8,0,(timeHours%24)/10);
            lcd.print(9,0,timeHours%4);
              lcd.print(10,0,":");         
            lcd.print(11,0,(timeMinutes%60)/10);
            lcd.print(12,0,timeMinutes%10);
              lcd.print(13,0,":");   
            lcd.print(14,0,(timeSecands%60)/10);
            lcd.print(15,0,timeSecands%10);
      }


    
 void boardTemp()
      {      
           Fahrenheit = dht.readTemperature(true);
           Blynk.virtualWrite(7, Fahrenheit);
      }


 void updateEEPROM()
      {
        
        EEPROMWriteint(0,balanceWatt);
        Blynk.virtualWrite(4, (int)balanceWatt);
         
      }
    

 void EEPROMWriteint(int address, int value)
      {
 // Decomposition from a long to 2 bytes by using bitshift.
 // One = Most significant -> Four = Least significant byte
      byte two = (value & 0xFF);
      byte one = ((value >> 8) & 0xFF);
    

 // Write the 2 bytes into the eeprom memory.
      EEPROM.write(address, two);
      EEPROM.write(address + 1, one);
      }



int EEPROMReadint(int address)
      {
   // Read the 2 bytes from the eeprom memory.
        long two = EEPROM.read(address);
        long one = EEPROM.read(address + 1);
        

   // Return the recomposed long by using bitshift.
      return ((two << 0) & 0xFF) + ((one << 8) & 0xFFFF);
      }