LCD with interrupts

I am using Arduino mega 2560 to interface a Piezoelectric sensor and encoder with an LCD to display the results. I am using interrupts to read the value of piezo but I have noticed that the LCD doesn't give a proper display when i use the interrupts
Is there anyway I can overcome this issue
I am using the following code
#include <Filters.h>
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
float EMA_a = 0.1; //initialization of EMA alpha
int EMA_S = 0; //initialization of EMA S
volatile int interruptPin = 3;
//const byte interruptPin = 2;
float filterFrequency= 4;
#include <LiquidCrystal.h>
#include <Keypad.h>
FilterOnePole lowpassFilter( LOWPASS, filterFrequency );

int PIEZO_PIN = 7;
volatile int A = HIGH;
volatile int B = HIGH;
int val = 0;
int a = 0;
int val1 = 0;
float rpmMax11=0;
float rpmMax=0;
float rpmMin11=1024;
int angle11 = 0;
//float Farray [32];
const unsigned int numReadings = 32;
unsigned int piezo11[numReadings];

int i =1;
float value1 = 0;
int c=0;
int d=0;
int e=0;
int RA[4]={0};
int D[4]={0};
int W[4]={0};
String instring=" ";
int Contrast=60;
float radius;
float distance;
float width;
LiquidCrystal lcd(20, 18, 17, 16, 15, 14);
const byte ROWS = 4;
const byte COLS = 3;
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3};
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'.', '0', '#'}
};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
char customKey = customKeypad.getKey();
void setup() {

attachInterrupt(digitalPinToInterrupt(interruptPin), piezo, RISING);
analogWrite(21,Contrast);
lcd.begin(20, 4);

Serial.begin(2000000);

sbi (ADCSRA, 1);
cbi (ADCSRA, 0);
cbi (ADCSRA, 0);
}

void loop() {

lcd.clear();

if (a >= numReadings)
{
detachInterrupt(1);

for (int a= 0; a<32 ; a++)
{
Serial.println(piezo11[a]);
if (piezo11[a]> rpmMax11)
{
rpmMax11 = piezo11[a];
angle11 = a;
}
if (piezo11[a] < rpmMin11)
{ rpmMin11 = piezo11[a];
}

}
Serial.println ("final results");
Serial.println(rpmMax11);
Serial.println(rpmMin11);

Serial.println(angle11);
a = 0;

}
/digitalWrite(32,LOW);
lcd.setCursor(0, 0);
lcd.print("MASS:");
lcd.setCursor(5, 0);
lcd.print("3g");
lcd.setCursor(0, 1);
lcd.print("I Angle:");
lcd.setCursor(9, 1);
lcd.print("Imb ANGle ");
lcd.setCursor(0, 2);
lcd.print("C Angle:");
lcd.setCursor(9, 2);
lcd.print("counter");
/
}

void piezo() {
val1 = lowpassFilter.input( analogRead(PIEZO_PIN ) );
EMA_S = (EMA_a*val1) + ((1-EMA_a)*EMA_S);
piezo11[a]= EMA_S;
a++;

// Serial.println(a);

}

Is there anyway I can overcome this issue

Whatever is actually causing the problem, the obvious way to stop it happening is not to use an interrupt. Are you sure that you need to ?

Is this a PWM pin on your Mega ?

analogWrite(21,Contrast);
    sbi (ADCSRA, 1);

cbi (ADCSRA, 0);
  cbi (ADCSRA, 0);

This appears to set 2 of the 3 analog clock prescaler bits. One of them twice. Why?

attachInterrupt(digitalPinToInterrupt(interruptPin), piezo, RISING);

...
detachInterrupt(1);

These are different interrupts. It fails to disengage the interrupt you are actually using and that is why the LCD is messed up.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.... :slight_smile: