Hi Have just made the setup as simple as possible to see if the problem is still there, i have removed LCDDisplay code EEPROM code and anything else.
Problem still there.
But as soon i remove PWM from code RPM is 100% consistent.
i made a video for you helpful people to see.
Here is the code. made as simple as possible. i have ewen tryed also to remove Serial communication, no help.
----------------------------------------------------code---------------------------------------------
unsigned long LcdLoopTime =0;
unsigned long RpmNew2 =0;
unsigned long RpmNewMin =0;
unsigned long RpmNewMax =0;
unsigned long RpmTOld2 =0;
unsigned long RpmTNew2 =0;
unsigned long rpm_motor =0;
void setup()
{
Serial.begin(9600);
pinMode (21,INPUT); //RPM interupt interupt
digitalWrite(21, HIGH); //interupt interupt skal måske væk
attachInterrupt(2, rpm_fun, RISING); //RISING LOW FALLING CHANGE
pinMode(11, OUTPUT); // sets the pin as output
}
void loop(){
if (rpm_motor < RpmNewMin) {RpmNewMin = rpm_motor;}
if (rpm_motor > RpmNewMax) {RpmNewMax = rpm_motor;}
if(millis()>LcdLoopTime)
{
LcdLoopTime+=200;
Serial.print(rpm_motor); Serial.print(" "); Serial.print(RpmNewMin); Serial.print(" "); Serial.println(RpmNewMax);
RpmNewMin =rpm_motor;
RpmNewMax =rpm_motor;
}
if (RpmNewMax > 1200) {analogWrite(11, 254);} //0 - 255
if (RpmNewMax < 1200) {analogWrite(11, rpm_motor /5);} //0 - 255
}
void rpm_fun()
{
RpmTOld2 = RpmTNew2; RpmTNew2 = micros(); RpmNew2 = ( RpmTNew2 - RpmTOld2 );
rpm_motor = (10800000 /RpmNew2); // 10 800 000 / 16129 microsec = 669 omdr/min ved 62 HZ
}
----------------------------------------------------code END-----------------------------------------