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);
}