# energy calculation (math and programming problem)

Hello guys , I have a project at my university where i need to read 2 currents and 1 voltage , and display them than i need to calculate the instant power and total energy consumption , i’m stuck at a math problem .I lerned that the energy=powertime so i used arduino internal second counter “millis” to calculate the energy , the problem is with my that my project need to run 10 min then stop for 5 than run another 5 min.So for a voltage of 12V and a current of 2 amps i should get 6 Wh (energy=power3600/time) but i’m getting 8 Wh , my time dosen’t stop when the motor stop , any ideea why?

`````` #include "LCD_Launchpad.h"
#define LED RED_LED

int sensorPin = 23;
int sensorValue = 0;

int sensorPin1 = 24;
int sensorValue1 = 0;

int sensorPin2 = 25;
int sensorValue2 = 0;

int sample=0;
int T_amp1;
int T_amp2;
int T_amp1_h;
int T_amp2_h;
int M_amp1;
int M_amp2;

int P1;
int P2;

int W1;
int W2;

int volt;
long int temp;

int amp1;
long int temp1;

int amp2;
long int temp2;

int value=1;
const int buttonPin=PUSH2;
int buttonState=0;

void setup() {
myLCD.init();
myLCD.clear();
pinMode(buttonPin,INPUT_PULLUP);
pinMode(LED, OUTPUT);
}

void loop() {
// read the value from the sensor:

if(buttonState==LOW){
value=value+1;
}

long milisec = millis(); // calculate time in milliseconds
long time1=milisec/1000; // convert milliseconds to seconds

temp = sensorValue;
temp = temp*330;
temp = (temp >> 12);
volt = temp;
volt=volt*6.98;

if(value==1)
{   if(volt<90)
{ myLCD.clear();
myLCD.println(volt);}
if(volt>100){
myLCD.clear();
myLCD.showSymbol(LCD_SEG_DOT1, 1);
myLCD.println(volt);}
if(volt>1000){
myLCD.clear();
myLCD.showSymbol(LCD_SEG_DOT2, 1);
myLCD.println(volt);}
if(volt>10000){
myLCD.clear();
myLCD.showSymbol(LCD_SEG_DOT3, 1);
myLCD.println(volt);}
}
delay(200);

temp1 = sensorValue1;
temp1 = temp1*330;
temp1 = (temp1 >> 12);
amp1 = temp1;

T_amp1=T_amp1+amp1;
M_amp1=T_amp1/sample;
T_amp1_h=M_amp1*3600/time1;
P1=M_amp1*volt;

if(value==2)

{   if(P1<9)
{ myLCD.clear();
myLCD.println(P1);}
if(P1>10){
myLCD.clear();
myLCD.showSymbol(LCD_SEG_DOT1, 1);
myLCD.println(P1);}
if(P1>100){
myLCD.clear();
myLCD.showSymbol(LCD_SEG_DOT2, 1);
myLCD.println(P1);}
if(P1>1000){
myLCD.clear();
myLCD.showSymbol(LCD_SEG_DOT3, 1);
myLCD.println(P1);}
}

temp2 = sensorValue2;
temp2 = temp2*330;
temp2 = (temp2 >> 12);
amp2 = temp2;

T_amp2=T_amp2+amp2;
M_amp2=T_amp2/sample;
T_amp2_h=M_amp2*3600/time1;
P2=volt*M_amp2;

if(value==3)
{   if(P2<9)
{ myLCD.clear();
myLCD.println(P2);}
if(P2>10){
myLCD.clear();
myLCD.showSymbol(LCD_SEG_DOT1, 1);
myLCD.println(P2);}
if(P2>100){
myLCD.clear();
myLCD.showSymbol(LCD_SEG_DOT2, 1);
myLCD.println(P2);}
if(P2>1000){
myLCD.clear();
myLCD.showSymbol(LCD_SEG_DOT3, 1);
myLCD.println(P2);}
}

if(value==3)
{value=1;
myLCD.clear();}
}
``````

so i used arduino internal second counter “millis” to calculate the energy

but you didn’t.
millis() returns the number of milliseconds since the Arduino started you can not use it directly, you have to take a reference point at the start of the measurements and subtract this from the time the millis gives you at the end.
In fact I can’t see how that code in any way does what you describe you want to do.

mihaita212:
I lerned that the energy=power*time

That sounds like a very superficial understanding of the subject. Make sure you know the physics thoroughly before you worry about programming. At the very least you need to know about volts, amps, watts and watt-hrs.

And to start with write a simple program with none of the LCD stuff. Just show the results on the Serial Monitor. When you have the calculations working properly you can then start thinking about fancy displays.

...R

You have to display an instantaneous value of power (units: say Watts) and a value of Energy (units: say Watt Hours).

I guess I'd have set up a routine (probably called by a timer) and sample the volts and amps (say every 100mS) .
This routine maintains 2 global variables:
instantaneousPower: volts * amps
cumulativeEnergyConsumption (assuming 100mS sample rate): cumulativeEnergyConsumption_previous + instantaneousPower / ( 3600 * 10 )

This separates the electrical units calculation from the display logic.
You can choose how often you update the display.

I'd base it all on the SI unit of energy - the joule .

1 watt-second.

Allan