RPM will not update after first loop also weird characters in lcd prints

Hi all

firstly i am a newbie, so please do not flame me for not knowing as much as you do !

in my own field i wouldnt do it to you,

I have tried multiple ways and spent the last 2days trying to get a very simple operation (part of the code) to occur, read the RPM every loop,

i have tried everything i can think of replaced it with other code… but in the end the same

also theier weird characters in some of my lcd.prints …

Any help would be gretaly appreciated :slight_smile:

float current=0;
float voltage=0;
float power=0;
float acpower=0;
float value=0;
float kw=0;
float getVPP();
const int sensorIn = A3;
int mVperAmp = 66;              // change this value for calibration - use 100 for 20A Module and 66 for 30A Module
float rev=0;
int rpm=0;
int oldtime=0;
int time;
float torque=0;
float torque1=0;

void isr()                      //interrupt service routine, only in pin number 2 or pin number 3
{
rev++;
}
void setup()
{ 
pinMode(A0,INPUT);
pinMode(A1,INPUT);
pinMode(A3,INPUT);

  // initialize the LCD
  lcd.begin();

  // Turn on the blacklight and print a message.
  lcd.backlight();
  lcd.print("MY POWER!");
  delay(5000);
  lcd.clear();                  
  lcd.setCursor(0,0);
  lcd.print("Motor  POWERED");    //printing on LCD
  lcd.setCursor(0,1);             //sets the position where to print
  lcd.print("   GENERATOR");
  delay(5000);
  lcd.clear();
  Serial.begin(9600); // To see the values on the serial monitor
attachInterrupt(0,isr,RISING);  //attaching the interrupt
}

void loop()
// taking the average for stable readings
{
delay(1000);
{
  detachInterrupt(0);         
}
time=millis()-oldtime;        //finding total time for one rev 
rpm=(rev/time)*60000;         //calculating the rpm
oldtime=millis();             
rev=0;

current=current + analogRead(A0);
voltage=voltage + analogRead(A1);
current=(current/20); current=current * 0.07595 * 5.0; // calibration value,to be changed according to components used
voltage=(voltage/20); voltage=voltage* 0.2075 * 5.0; // calibration value,to be changed according to components used
power= voltage*current;
Voltage1 = getVPP();
VRMS = (Voltage1/2.0) *0.707;  //root 2 is 0.707
AmpsRMS = (VRMS * 1000)/mVperAmp;
InputPower = 240 * AmpsRMS;
kw = (240 / 1000);
torque1 = rpm * 9.5488;
torque = kw / torque1;              

lcd.setCursor(4,0);
lcd.print("RPM TESTER");    //printing on LCD
lcd.setCursor(5,1);             //sets the position where to print
lcd.print(rpm);
lcd.println(" RPM");
delay(6000);

lcd.setCursor(0,0);
lcd.print("VOLTS: ");
lcd.print(voltage);
lcd.println(" VDC");
lcd.setCursor(0,1);
lcd.print("AMPS: ");
lcd.print(current);
lcd.println("A     ");
delay(4000);
lcd.clear();                  
lcd.setCursor(1,0);
lcd.print("OUTPUT POWER:");
lcd.setCursor(3,1);
lcd.print(power);
lcd.println("W"    );
delay(2000);
lcd.clear();      
lcd.setCursor(0, 0);
lcd.print("Input = 240VAC");
lcd.setCursor(2, 1);
lcd.println("Amps"    );
lcd.print(  AmpsRMS);
delay(4000);
lcd.clear(); 
lcd.setCursor(2, 0);
lcd.println("INPUT POWER"    );
lcd.setCursor(2, 1);
lcd.print(InputPower);
lcd.println("Watts"       );
delay(4000);
lcd.clear();                  
lcd.setCursor(2,0);
lcd.print("INPUT TORQUE:");
lcd.setCursor(3,1);
lcd.print(torque);
lcd.println("Nm"    );
delay(2000);
lcd.clear();
}
float getVPP()
{
  float result;
  int readValue;             //value read from the sensor
  int maxValue = 0;          // store max value here
  int minValue = 1024;          // store min value here
  
   uint32_t start_time = millis();
   while((millis()-start_time) < 2000) //sample for 2 Sec
   {
       readValue = analogRead(sensorIn);
       // see if you have a new maxValue
       if (readValue > maxValue) 
       {
           /*record the maximum sensor value*/
           maxValue = readValue;
       }
       if (readValue < minValue) 
       {
           /*record the minimum sensor value*/
           minValue = readValue;
       }
   }
   // Subtract min from max
   result = ((maxValue - minValue) * 5.0)/1024.0;   
   return result;
 }

As the "rev" variable is used inside an interrupt handler and in normal code you have to declare it volatile, otherwise the compiler might optimize some code away.

Declaring "rev" as float doesn't make sense, it just counts up. A uint32_t or similar does a better job.

Remove that code, it doesn't make sense:

{
  detachInterrupt(0);        
}

Don't use lcd.println(), replace it by lcd.print().

Please correct the code indentation. If you don't know how to do that, use the IDE function "Auto Format" (Tools menu).

If you need more help, post a wiring diagram of your setup.

Thanks, for your assistance it is appreciated !

I will attend to a come back with outcomes today ..