Run calcIrms in a function

I have a relay power module that supplies power to 4 lights via 4 relays, the relays all use a common 240 Vac supply. I wanted to monitor the incoming supply current. I have built my Arduino circuit with a 5A CT and the current readings obtained are within +/- 1 mA when compared to my Fluke true RMS meter. I used the basic example sketch from the Emon library. All good!
However the realys are controlled by a serial input the triggers a call back, the callback can occur randomly relative to the ‘Irms1 = emon1.calcIrms(1480);’ call, thus the callback could occur in the middle of the current data aquisition. I measured the aquisition to take approx. 250 mSec. It seems that if the callback occurs in the middle of the aquisition the Irms value is corrupted.
Ideally I would like to have the code, to obtain Irms, run as a function that can be called on demand immediately after a call back occurs or at any other time. Is this possible? If so, how would it be implemented please.

You are talking about emonLib, and not emonLibCM?

If you are, I don’t understand your problem. You can call calcIrms( ) or calcVI( ) whenever you like, each will sample over the prescribed period, NUMBER_OF_SAMPLES in the case of calcIrms( ) or crossings in the case of calcVI( ) and then do the calculations and make the result available. It’s only because they are called in the main loop of your sketch that they are called every 10 s or so.

I suspect you’ve only told us half the story - what controls when your realys [sic] operate? If you want, you can use the callback to trigger the measurement, or delay a few hundred milliseconds for the inrush to subside and then trigger the measurement.

I suspect your corruption is either interference caused by switching the relay coil circuit, or inrush.

Hi Robert, thank s for the prompt reply. My test set up is a single conductor thro the CT carrying a fixed current of 300 mA. It is not connected to any lights or any relay, it is connected to a constant 44R resistive load. I am using calcRMS with the default 1480 samples. I set up the Arduino code to get a new data reading every 5 seconds and it works perfectly. I also tried what you suugested in the callback, triggering the mesurement 500 mS after the relays were turned on. I turn the relays on in sequence and it I change one relay state it still works fine. If I change two relay states in quick succesion I get an erroneous reading for Irms. So, I thought there may be a timing issue with calcIrms.
I’ll do further testing because, based on your reply, I should be able to call calcIrms anytime.

If you’re going to switch two relays in quick succession, you need to wait until everything is stable, i.e. either delay the second relay, or send the callback when all changes have happened.

Hi Robert, I have found the problem! I had originally thought that something I was doing in my code was interfering with “Irms = emon1.calcIrms(1480);” getting the correct value. That thought was completely wrong and that line of code can be used in a callback or function as you said.
The project I am working on uses opto-coupled relays with the option to supply the relay coils with a different voltage source to the one powering Arduino. So, I supplied 12 Vdc into the project box and dropped that to 8 Vdc with an SMPS to power the Arduino via its Raw input and on board regulator, I then used an AMS1117-5 ( 0.8A max) to supply 5 Vdc to the relay coils. I set up a test with a fixed AC load fed via the CT, the measured value was 300 mA. The relays had no linkage to the fixed AC load at all.
The 1117 supplying the relay coils was faulty, with one relay coil energised @ 65 mA I got the expected
300 mA reading. When I energised the second relay (130 mA total coil current) I got a completely wrong mA reading for the same fixed load, but it seemed to be consistently wrong. Checking various voltage I found faulty ams1117 was dragging the 12 volt supply down, causing the Arduino voltage to drop to 3.5 volts and thus the ADC reference was flawed.
The reason for supplying the power in the way I have is because I try to make my projects universal and other variants require 12V for some solenoid valves.

Ha! The Golden Rule: Check the power supplies first.

Sage advice, it was a stroke of luck I noticed the display on my bench PSU, supplying the 12 Vdc drop. I had just changed some code so that threw me. Anyway all is good now.

Hi Bob,

Thw “Raw” input you referred to… Is that the Barrel jack, the Vin pin, or the 5 Volt pin?