Hi all! I've been using the arduino mega 2560 to count the number pulses (from an optical incremental encoder) occurring in 2 millisecond slots. This encoder is coupled to a relatively small dc motor.
Every 10 seconds, my arduino code gets the arduino to send the open-loop speed (in RPM) information (via serial), which I am able to see in the serial monitor window.
Those speed values look just fine except for cases where I introduce a "particular" Serial.print lines.
Let me explain my situation at bit. If I put a whole bunch of the same Serial.print lines for displaying the speed in RPM, then there appears to be no problem with the speed. The displayed speed is what I want to 'see' (ie. approximately 1500 RPM) every 10 seconds. So I do get (consistently) close to 1500 RPM every 10 seconds. For example, the following code has no problem.
if ( micros() - r_time >= 10000000 ) {
r_time = micros();
Serial.print("rpm is "); Serial.print( ((levdiff / (float) cpr) / (T * 1e-6)) * 60.0 ); Serial.print(" pwm_level is "); Serial.println( cpr );
Serial.print("rpm is "); Serial.print( ((levdiff / (float) cpr) / (T * 1e-6)) * 60.0 ); Serial.print(" pwm_level is "); Serial.println( cpr );
Serial.print("rpm is "); Serial.print( ((levdiff / (float) cpr) / (T * 1e-6)) * 60.0 ); Serial.print(" pwm_level is "); Serial.println( cpr );
//Serial.print("pwm return = ");Serial.print(pwm_equiv);Serial.print(" error pwm = ");Serial.println(error_pwm);
}
The resulting output is around 1500 RPM every 10 seconds (just has values like the ones show below indefinitely ---never gets above values of around 1550),
rpm is 1508.79 pwm_level is 2048
rpm is 1508.79 pwm_level is 2048
rpm is 1508.79 pwm_level is 2048
rpm is 1494.14 pwm_level is 2048
rpm is 1494.14 pwm_level is 2048
rpm is 1494.14 pwm_level is 2048
rpm is 1552.73 pwm_level is 2048
rpm is 1552.73 pwm_level is 2048
rpm is 1552.73 pwm_level is 2048
rpm is 1494.14 pwm_level is 2048
rpm is 1494.14 pwm_level is 2048
rpm is 1494.14 pwm_level is 2048
rpm is 1538.09 pwm_level is 2048
rpm is 1538.09 pwm_level is 2048
rpm is 1538.09 pwm_level is 2048
The reason why I inserted replicated lines of code was just to see if extra serial print lines was causing an issue. I found that I could add many of the same replicated serial print lines, and no problem with the displayed speed....of around 1500 RPM.
But.....as soon as I add a different Serial.print line, some of the 'displayed' RPM values tend to be higher eg... up to 1655.
I believe that the "actual" RPM is still actually hanging around 1500 RPM. I'm just trying to figure out how to get my code under control
if ( micros() - r_time >= 10000000 ) {
r_time = micros();
Serial.print("rpm is "); Serial.print( ((levdiff / (float) cpr) / (T * 1e-6)) * 60.0 ); Serial.print(" pwm_level is "); Serial.println( cpr );
Serial.print("rpm is "); Serial.print( ((levdiff / (float) cpr) / (T * 1e-6)) * 60.0 ); Serial.print(" pwm_level is "); Serial.println( cpr );
Serial.print("rpm is "); Serial.print( ((levdiff / (float) cpr) / (T * 1e-6)) * 60.0 ); Serial.print(" pwm_level is "); Serial.println( cpr );
Serial.print("pwm return = ");Serial.print(pwm_equiv);Serial.print(" error pwm = ");Serial.println(error_pwm);
}
rpm is 1552.73 pwm_level is 2048
rpm is 1552.73 pwm_level is 2048
rpm is 1552.73 pwm_level is 2048
pwm return = 111.64 error pwm = -4
rpm is 1538.09 pwm_level is 2048
rpm is 1538.09 pwm_level is 2048
rpm is 1538.09 pwm_level is 2048
pwm return = 110.78 error pwm = -3
rpm is 1538.09 pwm_level is 2048
rpm is 1538.09 pwm_level is 2048
rpm is 1538.09 pwm_level is 2048
pwm return = 110.78 error pwm = -3
rpm is 1640.62 pwm_level is 2048
rpm is 1640.62 pwm_level is 2048
rpm is 1640.62 pwm_level is 2048
pwm return = 116.79 error pwm = -9
rpm is 1625.98 pwm_level is 2048
rpm is 1625.98 pwm_level is 2048
rpm is 1625.98 pwm_level is 2048
pwm return = 115.93 error pwm = -8
rpm is 1655.27 pwm_level is 2048
rpm is 1655.27 pwm_level is 2048
rpm is 1655.27 pwm_level is 2048
pwm return = 117.65 error pwm = -10
rpm is 1523.44 pwm_level is 2048
rpm is 1523.44 pwm_level is 2048
rpm is 1523.44 pwm_level is 2048
pwm return = 109.93 error pwm = -2
rpm is 1655.27 pwm_level is 2048
rpm is 1655.27 pwm_level is 2048
rpm is 1655.27 pwm_level is 2048
pwm return = 117.65 error pwm = -10
rpm is 1655.27 pwm_level is 2048
rpm is 1655.27 pwm_level is 2048
rpm is 1655.27 pwm_level is 2048
pwm return = 117.65 error pwm = -10
rpm is 1567.38 pwm_level is 2048
rpm is 1567.38 pwm_level is 2048
rpm is 1567.38 pwm_level is 2048
pwm return = 112.50 error pwm = -5
The pulse counting is done by interrupts.
The variables used are straight forward.... eg..
levdiff is an 'int', which stores the number of counts (in each 2 millisec window).
cpr is an 'int' as well, which is the number of counts per revolution I'm using... which is 2048
pwm_equiv and error_rpm are 'float'.
After all this time of thinking, and unable to yet figure out how to fix up this one, I thought I'd better ask for some help and recommendations on this.
I know that Serial.print can add delay to my loop. Does anyone have any ideas about why the very last 'additional/extra' (different) Serial.print line appears to have a noticeable influence on the displayed RPM?
Thanks for any recommendations in advance!!