hello,
Im trying to estimate the time needed to run some piece of code that has to do a calculation (and adaptation to specific limits) of the phase values on a three phase generator, on different rotor angles.
#include <math.h>
float angle1 = 40.0;
int ph1 = 0;
int ph2 = 0;
int ph3 = 0;
int count = 0;
unsigned long microsold = 0;
unsigned long deltamicros = 0;
String txtmsg = "";
void setup() {
Serial.begin(115200);
}
void loop() {
microsold = micros();
ph1 = 2000 + int(2000 * sin(radians(angle1)));
ph2 = 2000 + int(2000 * sin(radians(angle1 + 120)));
ph3 = 2000 + int(2000 * sin(radians(angle1 + 240)));
deltamicros = micros() - microsold;
Serial.println(deltamicros);
// Serial.print(ph3);
//Serial.print(" ");
//Serial.print(angle1);
//Serial.print(" ");
//Serial.println(count);
count++;
ReadSerial();
}
void ReadSerial(void) {
int BytePos = 0;
char inchar = ' ';
if (Serial.available() > 0) {
while (Serial.available() > 0)
{
inchar = Serial.read();
txtmsg += inchar;
}
if (inchar == 10) {//line feed?
if (txtmsg.substring(0, 5) == "angle") {
int intval = txtmsg.substring(5, txtmsg.indexOf(".") ).toInt();
int decval = txtmsg.substring(txtmsg.indexOf(".") + 1, txtmsg.indexOf(".") + 2 ).toInt();
angle1 = float(intval) + float(decval) / 10;
}
txtmsg = "";
}
}
}
As you can see,
ph1, ph2 and ph3 is the target code
deltamicros should be [ONLY] the calculation time
ReadSerial is a function to set the desired angle.
Calculation runs ok.
time estimate is strange. If ReadSerial() statement is NOT icluded, 4 or 8 usec is displayed. If ReadSerial() statement IS included, deltamicros time displayed is around 480usec.
Also, if I change a little the code and do a Serial.print(micros()) before and after calculation, the values are also strange, for example (as displayed at one time)
WITH ReadSerial :micros before 1925596 micros after 1926416 delta 820
WITHOUT ReadSerial :micros before 916336 micros after 916932 delta 596 (not 4 or 8)
Any ideas for the problem (or how can I do an acceptable measurement)?