Current sensor issue

Hi guys i've ran into some problems while trying to build my software. I have a current sensor in my Arduino Leonardo that monitors the electricity usage and serial prints the data every 0.1 sec but when I add another current sensor, my data takes longer to print despite the software having a TimerOne running every 0.1 secs.

Below is my coding, any help would be appreciated, thanks!

#include "EmonLib.h"
#include <TimerOne.h>
EnergyMonitor emon1;
EnergyMonitor emon0;

int analogInPin1 = A1;
int analogInPin0 = A0;
Timer1.initialize(100000);
Timer1.attachInterrupt(timerAction); // run every 0.1 seconds
emon0.current(A0, 28.1);
emon1.current(A1, 28.1); // Current: input pin, calibration.

unsigned short int timerCount =0;

void setup()
{
analogRead (A1) ;
analogRead (A0) ;
}

void loop()
{

readRms0();
readRms1();
timerTriger = 1;

if(timerTriger == 1)
{

ActiveLed = ~ActiveLed;
digitalWrite(13, ActiveLed);
rf_Data = RmsStr0 + "," + RmsStr1;
Serial.println(rf_Data);
timerTriger = 0;

if (timerTriger == 0){
rf_Data = "";
}

}
}

void readRms0(){

Irms0 = emon0.calcIrms(500);
RmsStr0 = String(Irms0);

}

void readRms1()
{

double Irms1= emon1.calcIrms(500);

RmsStr1= String(Irms1);
wdt_reset();
}

void timerAction(void)
{
timerCount = timerCount + 1; // increase when LED turns on
}

If I were to print Rms0 only, the software works flawlessly and I am able to see the results every 0.1 secs.

But when I include Rms1 into the software, my data comes in but it is astonishingly slow when I want it to show every 0.1 sec.

No one here can help me? :frowning:

timerTriger is not declared anywhere.

What is the point of code like this:

   timerTriger = 1;

  if(timerTriger == 1)

the condition can never be false.

timerCount should be declared volatile.

You haven't provided a link the precise versions of the libraries you are using - people
need to be able to recreate the problem (and that involves compiling your sketch).

studyboy1:
No one here can help me? :frowning:

Has this board become so good that replies are expected within three hours?? While being bored, start by reading this and learn how to properly post your code, for starters.

Your code is incomplete. I miss a Serial.begin() call. I was looking for that as it may be a major clue on why you have a problem with your timing. At the default 9600 bps there's not much you can write out in 1/10th of a second.

MarkT:
timerTriger is not declared anywhere.

What is the point of code like this:

   timerTriger = 1;

if(timerTriger == 1)



the condition can never be false.

timerCount should be declared volatile.

You haven't provided a link the precise versions of the libraries you are using - people
need to be able to recreate the problem (and that involves compiling your sketch).

wvmarle:
Has this board become so good that replies are expected within three hours?? While being bored, start by reading this and learn how to properly post your code, for starters.

Your code is incomplete. I miss a Serial.begin() call. I was looking for that as it may be a major clue on why you have a problem with your timing. At the default 9600 bps there's not much you can write out in 1/10th of a second.

This is the full code below,

#include "EmonLib.h"
#include <TimerOne.h>
EnergyMonitor emon1;
EnergyMonitor emon0;

volatile unsigned long timerCount = 0; // use volatile for shared variables
int analogInPin1 = A1;
int analogInPin0 = A0;
unsigned short int timerTriger = 0;
int ActiveLed=0;
String rf_Data;
String RmsStr0;
String RmsStr1;

void setup()
{
analogRead (A1) ;
analogRead (A0) ;
emon0.current(A0, 28.1);
emon1.current(A1, 28.1); // Current: input pin, calibration.
Serial.begin(115200);
Timer1.initialize(100000);
Timer1.attachInterrupt(timerAction); // run every 0.1 seconds

}

void loop()
{

readRms0();
readRms1();
timerTriger = 1;

if(timerTriger == 1)
{

ActiveLed = ~ActiveLed;
digitalWrite(13, ActiveLed);
rf_Data = RmsStr0 + "," + RmsStr1;
Serial.println(rf_Data);
timerTriger = 0;

if (timerTriger == 0){
rf_Data = "";
}

}
}

void readRms0(){

double Irms0 = emon0.calcIrms(500);
RmsStr0 = String(Irms0);

}

void readRms1()
{

double Irms1= emon1.calcIrms(500);

RmsStr1= String(Irms1);
}

void timerAction(void)
{
timerCount = timerCount + 1; // increase when LED turns on
}

This is a list of libraries I'm using:

https://playground.arduino.cc/Code/Timer1

Pardon me for being rude at the beginning, thank you so much for the help.

If i were to comment out readRms1() in the void loop, the timer functions as normal at the rate of 0.1secs.

But if I were to include readRms1(), I am able to receive data from both my current sensors but it is not transmitting at the speed i desired, set at my timer.

Vice versa, if i were to comment out readRms0() and only include readRms1(), the timer also functions as per normal.

Your timer is doing nothing in the code apart from wasting a few CPU cycles (the variable timerCount is only referred to in the interupt routine, nowhere else). Your main loop is simply running as fast as it can and will naturally take twice as long to do 2 sensor readings instead of one.

Your only real option to speed things up (apart from deleting the timer interupt routine) is to reduce the number of samples you take when reading a sensor (eg change emon1.calcIrms(500) to something like emon1.calcIrms(250))

If you want a useful timer then you need something like

volatile unsigned long timerTriger = 0;
void loop()
{

   readRms0();
   readRms1();

  if(timerTriger == 1)
  {
   
    ActiveLed = ~ActiveLed;
    digitalWrite(13, ActiveLed);
    rf_Data = RmsStr0 + "," + RmsStr1;
    Serial.println(rf_Data);
    timerTriger = 0;       
   
    if (timerTriger == 0){
      rf_Data = "";
    }
   
  }
}

void timerAction(void)
{
  timerTriger=1;
}

If I want my

rw950431:
Your timer is doing nothing in the code apart from wasting a few CPU cycles (the variable timerCount is only referred to in the interupt routine, nowhere else). Your main loop is simply running as fast as it can and will naturally take twice as long to do 2 sensor readings instead of one.

Your only real option to speed things up (apart from deleting the timer interupt routine) is to reduce the number of samples you take when reading a sensor (eg change emon1.calcIrms(500) to something like emon1.calcIrms(250))

If you want a useful timer then you need something like

volatile unsigned long timerTriger = 0;

void loop()
{

readRms0();
  readRms1();

if(timerTriger == 1)
  {
 
    ActiveLed = ~ActiveLed;
    digitalWrite(13, ActiveLed);
    rf_Data = RmsStr0 + "," + RmsStr1;
    Serial.println(rf_Data);
    timerTriger = 0;     
 
    if (timerTriger == 0){
      rf_Data = "";
    }
 
  }
}

void timerAction(void)
{
  timerTriger=1;
}

If I want my current sensors to take in 500 samples no matter what, is there a way to make them serial print the data at a speed of 0.1 secs?

How long does a reading of the sensor take? You have only about 40-45 ms per sensor.

Sorry, what do you mean by 40ms - 45ms reading per sensor?

Your 0.1 s divide by two and deduct some time for the Serial print and you end up at maybe 40-45 ms per sensor (ms = milliseconds).

So how long does it take to read a sensor?

The millis() function is your friend here. Simply print out the value of millis() at the start of every loop as shown in the example and you will be able to calculate how long your current readings are taking.