Code posted properly below. Please read the "How to use this forum" post and follow the directions.
I don't know what you mean by "PULSES get stuck when any jerk comes in rotation", but
NEVER, EVER PRINT WITHIN AN INTERRUPT.
///button constants
const int startbuttonPin = 8;
const int searchbuttonPin = 9;
int startbuttonState; // the current reading from the input pin
int laststartButtonState = LOW; // the previous reading from the input pin
int searchbuttonState; // the current reading from the input pin
int lastsearchButtonState = LOW; // the previous reading from the input pin
// the following variables are unsigned long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
unsigned long laststartDebounceTime = 0; // the last time the output pin was toggled
unsigned long lastsearchDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
/////encoder constants
int pulses, A_SIG = 0, B_SIG = 1;
/////centring constants
int count;
int highrange;
int lowrange;
int i = 0;
int range = 0;
int led = 13;
int search = 0;
int sub = 0;
int dsub = 0;
//////lcd display
// include the library code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
void setup()
{
pinMode(led, OUTPUT);
//////button setup
pinMode(startbuttonPin, INPUT);
pinMode(searchbuttonPin, INPUT);
/////encoder setup
attachInterrupt(0, A_RISE, RISING);
attachInterrupt(1, B_RISE, RISING);
Serial.begin(115200);
//////lcd setup
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print("Vinesh Saini -AF");
lcd.display();
delay(1500);
lcd.noDisplay();
delay(500);
lcd.setCursor(0, 1);
lcd.print("Centring Project");
lcd.display();
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Vinesh's Project" );
lcd.display();
delay(1000);
}
void loop()
{
/////lcd logic
// Print a message to the LCD.
lcd.setCursor(0, 0);
lcd.print("Vinesh's Project" );
if (i == 0)
{
lcd.setCursor(0, 1);
lcd.print(pulses);
}
if (pulses < 1010 && pulses > 990)
{
lcd.clear();
lcd.setCursor(0, 1);
lcd.print(pulses);
}
if (pulses < 100 && pulses > 95)
{
lcd.clear();
lcd.setCursor(0, 1);
lcd.print(pulses);
}
if (pulses < 3 && pulses > -3 && pulses != 0)
{
lcd.clear();
lcd.setCursor(0, 1);
lcd.print(pulses);
}
if (pulses < -90 && pulses > -110)
{
lcd.clear();
lcd.setCursor(0, 1);
lcd.print(pulses);
}
if (pulses < -995 && pulses > -1010)
{
lcd.clear();
lcd.setCursor(0, 1);
lcd.print(pulses);
}
if (i == 2)
{
lcd.setCursor(5, 1);
lcd.print(search);
if (search < 1050 && search > 950)
{
lcd.clear();
lcd.setCursor(0, 1);
lcd.print(count);
lcd.setCursor(5, 1);
lcd.print(search);
}
if (search < 100 && search > 90)
{
lcd.clear();
lcd.setCursor(0, 1);
lcd.print(count);
lcd.setCursor(5, 1);
lcd.print(search);
}
if (search < 10 && search > -10)
{
lcd.clear();
lcd.setCursor(0, 1);
lcd.print(count);
lcd.setCursor(5, 1);
lcd.print(search);
}
if (search < -90 && search > -110)
{
lcd.clear();
lcd.setCursor(0, 1);
lcd.print(count);
lcd.setCursor(5, 1);
lcd.print(search);
}
if (search < -990 && search > -1100)
{
lcd.clear();
lcd.setCursor(0, 1);
lcd.print(count);
lcd.setCursor(5, 1);
lcd.print(search);
}
}
///////button debouncing logic
// read the state of the switch into a local variable:
int startreading = digitalRead(startbuttonPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (startreading != laststartButtonState)
{
// reset the debouncing timer
laststartDebounceTime = millis();
}
if ((millis() - laststartDebounceTime) > debounceDelay)
{
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
// if the button state has changed:
if (startreading != startbuttonState)
{
startbuttonState = startreading;
// only toggle the LED if the new button state is HIGH
if (startbuttonState == HIGH)
{
count = 0;
pulses = 0;
i = 0;
delay(500);
int startbuttonState = LOW;
lcd.clear();
}
}
}
///////////////////////////
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
laststartButtonState = startreading;
/////////////////////////////////////////////////////////
// read the state of the switch into a local variable:
int searchreading = digitalRead(searchbuttonPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (searchreading != lastsearchButtonState)
{
// reset the debouncing timer
lastsearchDebounceTime = millis();
}
if ((millis() - lastsearchDebounceTime) > debounceDelay)
{
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
// if the button state has changed:
if (searchreading != searchbuttonState)
{
searchbuttonState = searchreading;
// only toggle the LED if the new button state is HIGH
if (searchbuttonState == HIGH)
{
i = 1;
count = pulses;
delay(200);
range = count / 2 ;
highrange = range + 5;
lowrange = range - 5;
delay(500);
i++;
int searchbuttonState = LOW;
}
}
}
///////////////////////////
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastsearchButtonState = searchreading;
if (i == 2 && highrange >= pulses && lowrange <= pulses)
{
digitalWrite(led, HIGH);
lcd.setCursor(0, 0);
lcd.print("Vinesh's Project" );
lcd.setCursor(0, 1);
lcd.print(count);
lcd.setCursor(10, 1);
lcd.print("CENTRE");
delay(1000);
}
digitalWrite(led, LOW);
sub = count - pulses;
dsub = sub * 2;
search = count - dsub;
}
////////encoder's reading_logic
void A_RISE()
{
detachInterrupt(0);
A_SIG = 1;
if (B_SIG == 0)
pulses++;//moving forward
if (B_SIG == 1)
pulses--;//moving reverse
Serial.println(pulses);
attachInterrupt(0, A_FALL, FALLING);
}
void A_FALL()
{
detachInterrupt(0);
A_SIG = 0;
if (B_SIG == 1)
pulses++;//moving forward
if (B_SIG == 0)
pulses--;//moving reverse
Serial.println(pulses);
attachInterrupt(0, A_RISE, RISING);
}
void B_RISE()
{
detachInterrupt(1);
B_SIG = 1;
if (A_SIG == 1)
pulses++;//moving forward
if (A_SIG == 0)
pulses--;//moving reverse
Serial.println(pulses);
attachInterrupt(1, B_FALL, FALLING);
}
void B_FALL()
{
detachInterrupt(1);
B_SIG = 0;
if (A_SIG == 0)
pulses++;//moving forward
if (A_SIG == 1)
pulses--;//moving reverse
Serial.println(pulses);
attachInterrupt(1, B_RISE, RISING);
}