Function for incrementing variable every 1000 ms

Is making this Function for incrementing variable every 1000 ms good for measuring realtime power consumption in Wh . If it is what is the best way to vrite that kind of function. Thanks

Don't use a function. Just put a section of loop that fires every 1000 ms. Check out Blink without delay and just increment your variable instead of blinking.

Not sure if i know how to do it I am just beginner :S

steskaniarduino: Not sure if i know how to do it I am just beginner :S

Did you try?

The IDE includes the blink without delay example. Load it and read it.

This is code for measuring power consumption and current

#include <LiquidCrystal.h>

const int sensorIn = A0;
int mVperAmp = 66; 

double Voltage = 0;
double Power = 0;
double VRMS = 0;
int time = 0;
double AmpsRMS = 0;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
/*time counter*/
unsigned long previousMillis = 0; 
const long interval = 1000; 

void setup(){

lcd.begin(16,2);

}

void loop()

{
    
Voltage = getVPP();
VRMS = (Voltage/2.0) *0.707;
AmpsRMS = (VRMS * 1000)/mVperAmp;
lcd.setCursor(0,0);
lcd.print("I=");
lcd.print(AmpsRMS);
lcd.print(" A");
Power = 220*AmpsRMS;
  lcd.setCursor(7,0);
  lcd.print("P=");
  lcd.print(Power,2);
  lcd.print(" W");
  
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >=interval){
	time++;
}
lcd.setCursor(0,1);
lcd.print(whC(int time, float Power ),2)
}



float getVPP()

{

float result;

int readValue; //value read from the sensor

int maxValue = 0; // store max value here

int minValue = 1023; // store min value here

uint32_t start_time = millis();

while((millis()-start_time) < 1000) //sample for 1 Sec

{

readValue = analogRead(sensorIn);

// see if you have a new maxValue

if (readValue > maxValue)

{

/*record the maximum sensor value*/

maxValue = readValue;

}

if (readValue < minValue)

{

/*record the maximum sensor value*/

minValue = readValue;

}

}

// Subtract min from max

result = ((maxValue - minValue) * 5.0)/1023.0;

return result;
}
float whC(int time, float Power ){
	float result;
	result = (Power * time)/3600;
	return result;
}

Looks pretty good, you need to reset previousmillis inside the time check loop so it happens again 1 second later