Rotary encoder output to lcd 1602a

Thanks Cattledog, I tried adding this

int value = map(getCount(), 0, MAX_ENCODER_VALUE, 0, 99)
if (value <10)
  lcd.print("0");
lcd.print(value);

and recieved this complile error

 error: expected ',' or ';' before 'if'
error: 'lcd' does not name a type
expected ',' or ';' before 'if'
#include 
#include 
#include 
#define MAX_ENCODER_VALUE 4095
const int chipSelect = 53;
const int8_t encoderDirections[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0};
volatile int16_t counter = 0;
volatile int8_t direction = 0;

byte Flag = 1;//add flag to control multiple records for same button press


const int numRows = 2;
const int numCols = 16;
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
String result;
// define some values used by the panel and buttons
int lcd_key     = 0;
int adc_key_in  = 0;
#define btnRIGHT  0
#define btnUP      1
#define btnRESULT  2
#define btnLEFT    3
#define btnSELECT  4
#define btnNONE    5
int read_LCD_buttons()
{
  adc_key_in = analogRead(0);      // read the value from the sensor


  if (adc_key_in < 50)   return btnRIGHT;
  if (adc_key_in < 195)  return btnUP;
  if (adc_key_in < 380)  return btnRESULT;
  if (adc_key_in < 555)  return btnLEFT;
  if (adc_key_in < 790)  return btnSELECT;
  if (adc_key_in < 1024) return btnNONE;//add 
}

void setup() {
  Serial.begin(9600);
  Serial.println("Initializing SD card...");
  pinMode(chipSelect, OUTPUT);//set chip select PIN as output.
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");

  pinMode(18, INPUT_PULLUP);
  pinMode(19, INPUT_PULLUP);

  // set up the LCD's number of columns and rows:
  lcd.begin(numCols, numRows);

  attachInterrupt(4, encoder_interrupt, CHANGE);
  attachInterrupt(5, encoder_interrupt, CHANGE);

}


void loop() {
  // make a string for assembling the data to log:
  String dataString = "";

  lcd_key = read_LCD_buttons();  // read the buttons
  switch (lcd_key)               // depending on which button was pushed, we perform an action
  {
    case btnRIGHT:
      if (Flag == 0)
      {
        dataString =  String("RCP \t") + String(",") + String(map(getCount(), 0, MAX_ENCODER_VALUE, 0, 99));

        File dataFile = SD.open("dataRCP.txt", FILE_WRITE);//open a file named datalog.txt.


        if (dataFile) {       // if the file is available, write to it ('datafile' is returned 1 if SD.open was successful.
          dataFile.println(dataString);//print the concatenated data string and finish the line with a carriage return (println adds the CR automatically after printing the string)
          dataFile.close();   //close the file. IT is a good idea to always open/close a file before and after writing to it. That way, if someone removes the card the file is most
          //likely o.k. and can be read with the computer.

          Serial.println(dataString);// print the string also to the serial port, so we can see what is going on.
        }

        // if SD.open is not successful it returns a 0, i.e. the else{} is executed if the file could not be opened/created successfully.
        else {
          Serial.println("error opening dataRCP.txt");//in that case print an error message
        }
        Flag = 1;
      }
      break;

    case btnLEFT:
      if (Flag == 0)
      {
        dataString =  String("LCP \t") + String(",") + String(map(getCount(), 0, MAX_ENCODER_VALUE, 0, 99));
        File dataFile = SD.open("dataLCP.txt", FILE_WRITE);//open a file named datalog.txt.


        if (dataFile) {       // if the file is available, write to it ('datafile' is returned 1 if SD.open was successful.
          dataFile.println(dataString);//print the concatenated data string and finish the line with a carriage return (println adds the CR automatically after printing the string)
          dataFile.close();   //close the file. IT is a good idea to always open/close a file before and after writing to it. That way, if someone removes the card the file is most
          //likely o.k. and can be read with the computer.

          Serial.println(dataString);// print the string also to the serial port, so we can see what is going on.
        }
        // if SD.open is not successful it returns a 0, i.e. the else{} is executed if the file could not be opened/created successfully.
        else {
          Serial.println("error opening dataLCP.txt");//in that case print an error message
        }
        Flag = 1;
      }

      break;

    case btnRESULT:
      break;

    case btnNONE:
      Flag = 0;
      break;

  }//closes switch


  lcd.setCursor(0, 0);
  lcd.print("Contact Points:  ");
  lcd.setCursor(0, 1);

  //noInterrupts();
  //int copyCounter = getCount();
  //interrupts();
  //lcd.print(map(float(getCount()), 0, MAX_ENCODER_VALUE, 0, 99));
lcd.print(map(getCount(), 0, MAX_ENCODER_VALUE, 0, 99));
}//closes loop


void encoder_interrupt() {
  static uint8_t oldEncoderState = 0;
  oldEncoderState <<= 2;
  oldEncoderState |= ((PIND >> 2) & 0x03);
  direction = encoderDirections[(oldEncoderState & 0x0F)];
  counter += direction;

  if (counter < 0) counter = MAX_ENCODER_VALUE;
  else if (counter > MAX_ENCODER_VALUE) counter = 0;
}

int value = map(getCount(), 0, MAX_ENCODER_VALUE, 0, 99)
if (value <10)
  lcd.print("0");
lcd.print(value);

int getCount ()
{
  noInterrupts();
  int copyCounter = counter;
  interrupts();
  return copyCounter;
}

You patched the code into the wrong place. It was supposed to be in the main loop where the lcd print was invoked. I'll try not to provide code fragments in the future.

#include 
#include 
#include 
#define MAX_ENCODER_VALUE 4095
const int chipSelect = 53;
const int8_t encoderDirections[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0};
volatile int16_t counter = 0;
volatile int8_t direction = 0;

byte Flag = 1;//add flag to control multiple records for same button press


const int numRows = 2;
const int numCols = 16;
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
String result;
// define some values used by the panel and buttons
int lcd_key     = 0;
int adc_key_in  = 0;
#define btnRIGHT  0
#define btnUP      1
#define btnRESULT  2
#define btnLEFT    3
#define btnSELECT  4
#define btnNONE    5
int read_LCD_buttons()
{
  adc_key_in = analogRead(0);      // read the value from the sensor


  if (adc_key_in < 50)   return btnRIGHT;
  if (adc_key_in < 195)  return btnUP;
  if (adc_key_in < 380)  return btnRESULT;
  if (adc_key_in < 555)  return btnLEFT;
  if (adc_key_in < 790)  return btnSELECT;
  if (adc_key_in < 1024) return btnNONE;//add 
}

void setup() {
  Serial.begin(9600);
  Serial.println("Initializing SD card...");
  pinMode(chipSelect, OUTPUT);//set chip select PIN as output.
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");

  pinMode(18, INPUT_PULLUP);
  pinMode(19, INPUT_PULLUP);

  // set up the LCD's number of columns and rows:
  lcd.begin(numCols, numRows);

  attachInterrupt(4, encoder_interrupt, CHANGE);
  attachInterrupt(5, encoder_interrupt, CHANGE);

}


void loop() {
  // make a string for assembling the data to log:
  String dataString = "";

  lcd_key = read_LCD_buttons();  // read the buttons
  switch (lcd_key)               // depending on which button was pushed, we perform an action
  {
    case btnRIGHT:
      if (Flag == 0)
      {
        dataString =  String("RCP \t") + String(",") + String(map(getCount(), 0, MAX_ENCODER_VALUE, 0, 99));

        File dataFile = SD.open("dataRCP.txt", FILE_WRITE);//open a file named datalog.txt.


        if (dataFile) {       // if the file is available, write to it ('datafile' is returned 1 if SD.open was successful.
          dataFile.println(dataString);//print the concatenated data string and finish the line with a carriage return (println adds the CR automatically after printing the string)
          dataFile.close();   //close the file. IT is a good idea to always open/close a file before and after writing to it. That way, if someone removes the card the file is most
          //likely o.k. and can be read with the computer.

          Serial.println(dataString);// print the string also to the serial port, so we can see what is going on.
        }

        // if SD.open is not successful it returns a 0, i.e. the else{} is executed if the file could not be opened/created successfully.
        else {
          Serial.println("error opening dataRCP.txt");//in that case print an error message
        }
        Flag = 1;
      }
      break;

    case btnLEFT:
      if (Flag == 0)
      {
        dataString =  String("LCP \t") + String(",") + String(map(getCount(), 0, MAX_ENCODER_VALUE, 0, 99));
        File dataFile = SD.open("dataLCP.txt", FILE_WRITE);//open a file named datalog.txt.


        if (dataFile) {       // if the file is available, write to it ('datafile' is returned 1 if SD.open was successful.
          dataFile.println(dataString);//print the concatenated data string and finish the line with a carriage return (println adds the CR automatically after printing the string)
          dataFile.close();   //close the file. IT is a good idea to always open/close a file before and after writing to it. That way, if someone removes the card the file is most
          //likely o.k. and can be read with the computer.

          Serial.println(dataString);// print the string also to the serial port, so we can see what is going on.
        }
        // if SD.open is not successful it returns a 0, i.e. the else{} is executed if the file could not be opened/created successfully.
        else {
          Serial.println("error opening dataLCP.txt");//in that case print an error message
        }
        Flag = 1;
      }

      break;

    case btnRESULT:
      break;

    case btnNONE:
      Flag = 0;
      break;

  }//closes switch


  lcd.setCursor(0, 0);
  lcd.print("Contact Points:  ");
  lcd.setCursor(0, 1);

  int value = map(getCount(), 0, MAX_ENCODER_VALUE, 0, 99);
  if (value <10)
    lcd.print("0");
  lcd.print(value);

  //noInterrupts();
  //int copyCounter = getCount();
  //interrupts();
  //lcd.print(map(float(getCount()), 0, MAX_ENCODER_VALUE, 0, 99));
//lcd.print(map(getCount(), 0, MAX_ENCODER_VALUE, 0, 99));

}//closes loop


void encoder_interrupt() {
  static uint8_t oldEncoderState = 0;
  oldEncoderState <<= 2;
  oldEncoderState |= ((PIND >> 2) & 0x03);
  direction = encoderDirections[(oldEncoderState & 0x0F)];
  counter += direction;

  if (counter < 0) counter = MAX_ENCODER_VALUE;
  else if (counter > MAX_ENCODER_VALUE) counter = 0;
}


int getCount ()
{
  noInterrupts();
  int copyCounter = counter;
  interrupts();
  return copyCounter;
}

Hi Cattledog, Thanks for getting back to me. That did the trick. Also implemented another change to add to decimal places. I am ready to put my project to the test but have one little glitch. When i power up the mega board using the usb serial cable it works great. I tried removing the cable and using a 12v / 1amp power adapter but it doesn't work as expected. It powers up okay and I see the Contact Points: string on the Lcd, but the number digits seem to appear behind the Contact Points: string on the same line, and it doesn't look as if the output of the encoder is being read. i assume that I have hooked something up incorrectly. Any ideas? Bill

#include 
#include 
#include 
#define MAX_ENCODER_VALUE 4095
const int chipSelect = 53;
const int8_t encoderDirections[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0};
volatile int16_t counter = 0;
volatile int8_t direction = 0;

byte Flag = 1;//add flag to control multiple records for same button press


const int numRows = 2;
const int numCols = 16;
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
String result;
// define some values used by the panel and buttons
int lcd_key     = 0;
int adc_key_in  = 0;
#define btnRIGHT  0
#define btnUP      1
#define btnRESULT  2
#define btnLEFT    3
#define btnSELECT  4
#define btnNONE    5
int read_LCD_buttons()
{
  adc_key_in = analogRead(0);      // read the value from the sensor


  if (adc_key_in < 50)   return btnRIGHT;
  if (adc_key_in < 195)  return btnUP;
  if (adc_key_in < 380)  return btnRESULT;
  if (adc_key_in < 555)  return btnLEFT;
  if (adc_key_in < 790)  return btnSELECT;
  if (adc_key_in < 1024) return btnNONE;//add 
}

void setup() {
  Serial.begin(9600);
  Serial.println("Initializing SD card...");
  pinMode(chipSelect, OUTPUT);//set chip select PIN as output.
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");

  pinMode(18, INPUT_PULLUP);
  pinMode(19, INPUT_PULLUP);

  // set up the LCD's number of columns and rows:
  lcd.begin(numCols, numRows);

  attachInterrupt(4, encoder_interrupt, CHANGE);
  attachInterrupt(5, encoder_interrupt, CHANGE);

}


void loop() {
  // make a string for assembling the data to log:
  String dataString = "";

  lcd_key = read_LCD_buttons();  // read the buttons
  switch (lcd_key)               // depending on which button was pushed, we perform an action
  {
    case btnRIGHT:
      if (Flag == 0)
      {
        dataString =  String("RCP \t") +  String(mapfloat(getCount(), 0, MAX_ENCODER_VALUE, 0, 99));

        File dataFile = SD.open("dataRCP.txt", FILE_WRITE);//open a file named dataRCP.txt.


        if (dataFile) {       // if the file is available, write to it ('datafile' is returned 1 if SD.open was successful.
          dataFile.println(dataString);//print the concatenated data string and finish the line with a carriage return (println adds the CR automatically after printing the string)
          dataFile.close();   //close the file. IT is a good idea to always open/close a file before and after writing to it. That way, if someone removes the card the file is most
          //likely o.k. and can be read with the computer.

         // Serial.println(dataString);// print the string also to the serial port, so we can see what is going on.
        }

        // if SD.open is not successful it returns a 0, i.e. the else{} is executed if the file could not be opened/created successfully.
        else {
          Serial.println("error opening dataRCP.txt");//in that case print an error message
        }
        Flag = 1;
      }
      break;

    case btnLEFT:
      if (Flag == 0)
      {
        dataString =  String("LCP \t") +  String(mapfloat(getCount(), 0, MAX_ENCODER_VALUE, 0, 99));
        File dataFile = SD.open("dataLCP.txt", FILE_WRITE);//open a file named dataLCP.txt.


        if (dataFile) {       // if the file is available, write to it ('datafile' is returned 1 if SD.open was successful.
          dataFile.println(dataString);//print the concatenated data string and finish the line with a carriage return (println adds the CR automatically after printing the string)
          dataFile.close();   //close the file. IT is a good idea to always open/close a file before and after writing to it. That way, if someone removes the card the file is most
          //likely o.k. and can be read with the computer.

          Serial.println(dataString);// print the string also to the serial port, so we can see what is going on.
        }
        // if SD.open is not successful it returns a 0, i.e. the else{} is executed if the file could not be opened/created successfully.
        else {
          Serial.println("error opening dataLCP.txt");//in that case print an error message
        }
        Flag = 1;
      }

      break;

    case btnRESULT:
      break;

    case btnNONE:
      Flag = 0;
      break;

  }//closes switch


  lcd.setCursor(0, 0);
  lcd.print("Contact Points:  ");
  lcd.setCursor(0, 1);

  float value = mapfloat(getCount(), 0, MAX_ENCODER_VALUE, 0, 99);
  if (value <10)
    lcd.print("0");
  lcd.print(value);

  

}//closes loop


void encoder_interrupt() {
  static uint8_t oldEncoderState = 0;
  oldEncoderState <<= 2;
  oldEncoderState |= ((PIND >> 2) & 0x03);
  direction = encoderDirections[(oldEncoderState & 0x0F)];
  counter += direction;

  if (counter < 0) counter = MAX_ENCODER_VALUE;
  else if (counter > MAX_ENCODER_VALUE) counter = 0;
}

float mapfloat(long x, long in_min, long in_max, long out_min, long out_max)
{
  return float((x - in_min) * float((out_max - out_min)) / (in_max - in_min) + out_min);
}


int getCount ()
{
  noInterrupts();
  int copyCounter = counter;
  interrupts();
  return copyCounter;
}

Good work on the mapfloat function.

using a 12v / 1amp power adapter

What is that?

Is is something which plugs into mains power and puts out 12vdc (not ac) or is it some sort of battery pack? Is it plugged in to the barrel jack adaptor?

If everything was working on usb power and then went funny with your power supply, there are three things I can think off. It's not clear to me if things would work to the extent you see with any of these scenarios.

1) The end which plugs into the barrel jack needs to have center positive. Some power supplies can be center ground. You maybe applying reverse polarity.

2) Some "wall wart" power adaptors are unregulated, and can put out significantly higher voltage than the nameplate at low loads. You can actually burn out the voltage regulator with them. the 12v supplies are particularly bad because they can be 18v at low load. These supplies often have a rated load to go with the voltage. You want a regulated or "switching" power supply if using one of the plug in jobs.

3) Its a 12 volt ac adaptor.

There may also be some sort of grounding issue, and you want to make sure all the grounds are closely tied.

Hi Cattledog, Thanks for your reply. Upon reading it I replaced the power supply with a 9v battery adapter which plugs into the barrel jack. Still doing the same behavior. From what I can see, only the output string "Contact Points:" displays correctly. I noticed what appeared to be 00.00 underneath "Contact Points". I commented out the line lcd.print("Contact Points:  "); and uploaded the sketch and sure enough at position 0,0 on the lcd screen 00.00 appeared. The encoder however wasn't functioning. Once I unplugged the 9v battery adapter from the barrel jack and plugged in the usb serial monitor cable the display worked correctly with 00.00 appearing on the lcd screen at 0,1 as it is supposed to and the encoder functioning again. I am somewhat bewildered as to what could be happening and wonder if the barrel jack connection on my arduino mega board 2560 might be faulty? The 9v adapter I am using came with my arduino kit and assume that it is wired correctly with the center pin being positive. Both the power light on the lcd shield and the on led on the mega board are light. Any ideas on what I could try to fix this. I have a arduino uno board which I will try with the lcd shield and encoder hooked up. with have to modify the sketch code to do this. I modified the sketch for the arduino uno board and get the same error now when the usb serial cable is plugged in as well as when I use the 9v adapter on the barrel jack.....confusing...ideas?????? Bill

#include 
#include 
#include 
#define MAX_ENCODER_VALUE 4095
const int chipSelect = 53;
const int8_t encoderDirections[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0};
volatile int16_t counter = 0;
volatile int8_t direction = 0;

byte Flag = 1;//add flag to control multiple records for same button press


const int numRows = 2;
const int numCols = 16;
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
String result;
// define some values used by the panel and buttons
int lcd_key     = 0;
int adc_key_in  = 0;
#define btnRIGHT  0
#define btnUP      1
#define btnRESULT  2
#define btnLEFT    3
#define btnSELECT  4
#define btnNONE    5
int read_LCD_buttons()
{
  adc_key_in = analogRead(0);      // read the value from the sensor


  if (adc_key_in < 50)   return btnRIGHT;
  if (adc_key_in < 195)  return btnUP;
  if (adc_key_in < 380)  return btnRESULT;
  if (adc_key_in < 555)  return btnLEFT;
  if (adc_key_in < 790)  return btnSELECT;
  if (adc_key_in < 1024) return btnNONE;//add 
}

void setup() {
  Serial.begin(9600);
  Serial.println("Initializing SD card...");
  pinMode(chipSelect, OUTPUT);//set chip select PIN as output.
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");

 // pinMode(18, INPUT_PULLUP);
  //pinMode(19, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP); 
  pinMode(3, INPUT_PULLUP);

  // set up the LCD's number of columns and rows:
  lcd.begin(numCols, numRows);

  //attachInterrupt(4, encoder_interrupt, CHANGE);
  //attachInterrupt(5, encoder_interrupt, CHANGE);
  attachInterrupt(0, encoder_interrupt, CHANGE); 
  attachInterrupt(1, encoder_interrupt, CHANGE);
}


void loop() {
  // make a string for assembling the data to log:
  String dataString = "";

  lcd_key = read_LCD_buttons();  // read the buttons
  switch (lcd_key)               // depending on which button was pushed, we perform an action
  {
    case btnRIGHT:
      if (Flag == 0)
      {
        dataString =  String("RCP \t") +  String(mapfloat(getCount(), 0, MAX_ENCODER_VALUE, 0, 99));

        File dataFile = SD.open("dataRCP.txt", FILE_WRITE);//open a file named dataRCP.txt.


        if (dataFile) {       // if the file is available, write to it ('datafile' is returned 1 if SD.open was successful.
          dataFile.println(dataString);//print the concatenated data string and finish the line with a carriage return (println adds the CR automatically after printing the string)
          dataFile.close();   //close the file. IT is a good idea to always open/close a file before and after writing to it. That way, if someone removes the card the file is most
          //likely o.k. and can be read with the computer.

         // Serial.println(dataString);// print the string also to the serial port, so we can see what is going on.
        }

        // if SD.open is not successful it returns a 0, i.e. the else{} is executed if the file could not be opened/created successfully.
        else {
          Serial.println("error opening dataRCP.txt");//in that case print an error message
        }
        Flag = 1;
      }
      break;

    case btnLEFT:
      if (Flag == 0)
      {
        dataString =  String("LCP \t") +  String(mapfloat(getCount(), 0, MAX_ENCODER_VALUE, 0, 99));
        File dataFile = SD.open("dataLCP.txt", FILE_WRITE);//open a file named dataLCP.txt.


        if (dataFile) {       // if the file is available, write to it ('datafile' is returned 1 if SD.open was successful.
          dataFile.println(dataString);//print the concatenated data string and finish the line with a carriage return (println adds the CR automatically after printing the string)
          dataFile.close();   //close the file. IT is a good idea to always open/close a file before and after writing to it. That way, if someone removes the card the file is most
          //likely o.k. and can be read with the computer.

          Serial.println(dataString);// print the string also to the serial port, so we can see what is going on.
        }
        // if SD.open is not successful it returns a 0, i.e. the else{} is executed if the file could not be opened/created successfully.
        else {
          Serial.println("error opening dataLCP.txt");//in that case print an error message
        }
        Flag = 1;
      }

      break;

    case btnRESULT:
      break;

    case btnNONE:
      Flag = 0;
      break;

  }//closes switch


  lcd.setCursor(0, 0);
  lcd.print("Contact Points:  ");
  lcd.setCursor(0, 1);

  float value = mapfloat(getCount(), 0, MAX_ENCODER_VALUE, 0, 99);
  if (value <10)
    lcd.print("0");
  lcd.print(value);

  

}//closes loop


void encoder_interrupt() {
  static uint8_t oldEncoderState = 0;
  oldEncoderState <<= 2;
  oldEncoderState |= ((PIND >> 2) & 0x03);
  direction = encoderDirections[(oldEncoderState & 0x0F)];
  counter += direction;

  if (counter < 0) counter = MAX_ENCODER_VALUE;
  else if (counter > MAX_ENCODER_VALUE) counter = 0;
}

float mapfloat(long x, long in_min, long in_max, long out_min, long out_max)
{
  return float((x - in_min) * float((out_max - out_min)) / (in_max - in_min) + out_min);
}


int getCount ()
{
  noInterrupts();
  int copyCounter = counter;
  interrupts();
  return copyCounter;
}
const int chipSelect = 53;

If you are going to rewrite the sketch for a uno, I believe the CS pin will need to be changed. There may also be interactions with the backlight which is on pin 10, and also pin 4 is in use by the display. However, I doubt this is relevant to the major problems you are having.

I don't understand what might have happened when you hooked up to your first 12v lamp power supply but its possible something was damaged. The difference between the barrel jack and the usb power might indicate a problem with the voltage regulator. I've burned one out with an unregulated 12v supply. Look for that part on a schematic and see if you can see it damaged/burned.

Can you find more detail on what was connected to the barrel jack when things went bad? It could help in determining what happened.

I'm afraid you will have to go back to basic trouble shooting with simple sketches to see what it takes to get the lcd, the encoders, and the sd card working again.

I modified the sketch for the arduino uno board and get the same error now when the usb serial cable is plugged in as well as when I use the 9v adapter on the barrel jack...

I'm with you, and do not understand this. Is the encoder not working or just the lcd on the uno? Unfortunately, all I can say is that you are going to have to take a step backward , but you at least know that you have working code waiting at the end when you figure out what happened with the hardware.

Well Cattledog i'm drawing a huge blank here. I switched out the lcd shield for another one and I am still getting the same behavior. I guess the lcd shield has issues with the barrel connector power supply.....Or even the way it is stacked on the arduino uno or mega board...... Bill

I switched out the lcd shield for another one and I am still getting the same behavior.

That's useful information in that the internal controller chips on the 16x2 lcd which saw the lamp power supply are probably not damaged.

Can you run a simple "hello world" example sketch on either lcd shield with either the uno or the mega?

I uploaded the example sketch 'Hello, World' which includes the counter and it ran fine with the 9v adapter attached to the barrel jack. The lcd displayed "Hello, World" at 0,0 and the counter "0.0" at 0,1 So is it possible it is something in the way the LCD Display is handled in the sketch? Not sure how to proceed. Bill

#include 
#include 
#include 
#define MAX_ENCODER_VALUE 4095
const int chipSelect = 53;
const int8_t encoderDirections[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0};
volatile int16_t counter = 0;
volatile int8_t direction = 0;

byte Flag = 1;//add flag to control multiple records for same button press


const int numRows = 2;
const int numCols = 16;
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
String result;
// define some values used by the panel and buttons
int lcd_key     = 0;
int adc_key_in  = 0;
#define btnRIGHT  0
#define btnUP      1
#define btnRESULT  2
#define btnLEFT    3
#define btnSELECT  4
#define btnNONE    5
int read_LCD_buttons()
{
  adc_key_in = analogRead(0);      // read the value from the sensor


  if (adc_key_in < 50)   return btnRIGHT;
  if (adc_key_in < 195)  return btnUP;
  if (adc_key_in < 380)  return btnRESULT;
  if (adc_key_in < 555)  return btnLEFT;
  if (adc_key_in < 790)  return btnSELECT;
  if (adc_key_in < 1024) return btnNONE;//add 
}

void setup() {
  Serial.begin(9600);
  Serial.println("Initializing SD card...");
  pinMode(chipSelect, OUTPUT);//set chip select PIN as output.
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");

  pinMode(18, INPUT_PULLUP);
  pinMode(19, INPUT_PULLUP);

  // set up the LCD's number of columns and rows:
  lcd.begin(numCols, numRows);

  attachInterrupt(4, encoder_interrupt, CHANGE);
  attachInterrupt(5, encoder_interrupt, CHANGE);

}


void loop() {
  // make a string for assembling the data to log:
  String dataString = "";

  lcd_key = read_LCD_buttons();  // read the buttons
  switch (lcd_key)               // depending on which button was pushed, we perform an action
  {
    case btnRIGHT:
      if (Flag == 0)
      {
        dataString =  String("RCP \t") +  String(mapfloat(getCount(), 0, MAX_ENCODER_VALUE, 0, 99));

        File dataFile = SD.open("dataRCP.txt", FILE_WRITE);//open a file named dataRCP.txt.


        if (dataFile) {       // if the file is available, write to it ('datafile' is returned 1 if SD.open was successful.
          dataFile.println(dataString);//print the concatenated data string and finish the line with a carriage return (println adds the CR automatically after printing the string)
          dataFile.close();   //close the file. IT is a good idea to always open/close a file before and after writing to it. That way, if someone removes the card the file is most
          //likely o.k. and can be read with the computer.

         // Serial.println(dataString);// print the string also to the serial port, so we can see what is going on.
        }

        // if SD.open is not successful it returns a 0, i.e. the else{} is executed if the file could not be opened/created successfully.
        else {
          Serial.println("error opening dataRCP.txt");//in that case print an error message
        }
        Flag = 1;
      }
      break;

    case btnLEFT:
      if (Flag == 0)
      {
        dataString =  String("LCP \t") +  String(mapfloat(getCount(), 0, MAX_ENCODER_VALUE, 0, 99));
        File dataFile = SD.open("dataLCP.txt", FILE_WRITE);//open a file named dataLCP.txt.


        if (dataFile) {       // if the file is available, write to it ('datafile' is returned 1 if SD.open was successful.
          dataFile.println(dataString);//print the concatenated data string and finish the line with a carriage return (println adds the CR automatically after printing the string)
          dataFile.close();   //close the file. IT is a good idea to always open/close a file before and after writing to it. That way, if someone removes the card the file is most
          //likely o.k. and can be read with the computer.

          Serial.println(dataString);// print the string also to the serial port, so we can see what is going on.
        }
        // if SD.open is not successful it returns a 0, i.e. the else{} is executed if the file could not be opened/created successfully.
        else {
          Serial.println("error opening dataLCP.txt");//in that case print an error message
        }
        Flag = 1;
      }

      break;

    case btnRESULT:
      break;

    case btnNONE:
      Flag = 0;
      break;

  }//closes switch


  lcd.setCursor(0, 0);
  lcd.print("Contact Points:  ");
  lcd.setCursor(0, 1);

  float value = mapfloat(getCount(), 0, MAX_ENCODER_VALUE, 0, 99);
  if (value <10)
    lcd.print("0");
  lcd.print(value);

  

}//closes loop


void encoder_interrupt() {
  static uint8_t oldEncoderState = 0;
  oldEncoderState <<= 2;
  oldEncoderState |= ((PIND >> 2) & 0x03);
  direction = encoderDirections[(oldEncoderState & 0x0F)];
  counter += direction;

  if (counter < 0) counter = MAX_ENCODER_VALUE;
  else if (counter > MAX_ENCODER_VALUE) counter = 0;
}

float mapfloat(long x, long in_min, long in_max, long out_min, long out_max)
{
  return float((x - in_min) * float((out_max - out_min)) / (in_max - in_min) + out_min);
}


int getCount ()
{
  noInterrupts();
  int copyCounter = counter;
  interrupts();
  return copyCounter;
}

So is it possible it is something in the way the LCD Display is handled in the sketch?

It was working before the power event, so it's not your lcd display code which looks fine.

lcd.setCursor(0, 0);
  lcd.print("Contact Points:  ");
  lcd.setCursor(0, 1);

  float value = mapfloat(getCount(), 0, MAX_ENCODER_VALUE, 0, 99);
  if (value <10)
    lcd.print("0");
  lcd.print(value);

If the first line of the lcd display "Contact Points: " is unchanging, you can move it to set up. It will be stored in the lcd memory and you do not need to write it over and over in each loop.

Not sure how to proceed.

I'd next work on checking out the encoder and finding out why it is non responsive. As you go forward and describe your trouble shooting, please let me know if you are using the Mega which saw the power event, or the Uno which did not. Whether or not you are using the USB power or the 9V through the barrel jack, and what else is connected when you do the test. You have two arduinos, two means of power, and several pieces of hardware. You will need to move through all the combinations systematically and describe accurately and in detail what you have done.

Hi Cattledog.... I decided to try a a couple of different sketches using the lcd. With hello word...it worked fine on the dc battery adapter which I upgraded to a 16V DC 850ma. I then tried an earlier sketch using the lcd and encoder and this also worked fine on the usb serial cable and using the power adapter on the barrel jack. the code below is what I used. However. When I used the last modified code I now get the error where it will not work on the usb serial cable or with the power adapter on the barrel jack. I have not touched and wiring. I just uploaded the sketch. this makes me wonder if something in the code is causing this. I find it strange that it once worked with the serial cable but does not now, while the earlier sketch works fine . Below this sketch I have posted the last modified sketch that is having the problems...

#include 
#include 
#include 
#define MAX_ENCODER_VALUE 4095
const int chipSelect = 53;
const int8_t encoderDirections[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0};
volatile int16_t counter = 0;
volatile int8_t direction = 0;

byte Flag = 1;//add flag to control multiple records for same button press


const int numRows = 2;
const int numCols = 16;
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
String result;
// define some values used by the panel and buttons
int lcd_key     = 0;
int adc_key_in  = 0;
#define btnRIGHT  0
#define btnUP      1
#define btnRESULT  2
#define btnLEFT    3
#define btnSELECT  4
#define btnNONE    5
int read_LCD_buttons()
{
  adc_key_in = analogRead(0);      // read the value from the sensor


  if (adc_key_in < 50)   return btnRIGHT;
  if (adc_key_in < 195)  return btnUP;
  if (adc_key_in < 380)  return btnRESULT;
  if (adc_key_in < 555)  return btnLEFT;
  if (adc_key_in < 790)  return btnSELECT;
  if (adc_key_in < 1024) return btnNONE;//add 
}

void setup() {
  Serial.begin(9600);
  Serial.println("Initializing SD card...");
  pinMode(chipSelect, OUTPUT);//set chip select PIN as output.
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");

  pinMode(18, INPUT_PULLUP);
  pinMode(19, INPUT_PULLUP);

  // set up the LCD's number of columns and rows:
  lcd.begin(numCols, numRows);

  attachInterrupt(4, encoder_interrupt, CHANGE);
  attachInterrupt(5, encoder_interrupt, CHANGE);

}


void loop() {
  // make a string for assembling the data to log:
  String dataString = "";

  lcd_key = read_LCD_buttons();  // read the buttons
  switch (lcd_key)               // depending on which button was pushed, we perform an action
  {
    case btnRIGHT:
      if (Flag == 0)
      {
        dataString =  String("RCP \t") +  String(mapfloat(getCount(), 0, MAX_ENCODER_VALUE, 0, 99));

        File dataFile = SD.open("dataRCP.txt", FILE_WRITE);//open a file named dataRCP.txt.


        if (dataFile) {       // if the file is available, write to it ('datafile' is returned 1 if SD.open was successful.
          dataFile.println(dataString);//print the concatenated data string and finish the line with a carriage return (println adds the CR automatically after printing the string)
          dataFile.close();   //close the file. IT is a good idea to always open/close a file before and after writing to it. That way, if someone removes the card the file is most
          //likely o.k. and can be read with the computer.

          Serial.println(dataString);// print the string also to the serial port, so we can see what is going on.
        }

        // if SD.open is not successful it returns a 0, i.e. the else{} is executed if the file could not be opened/created successfully.
        else {
          Serial.println("error opening dataRCP.txt");//in that case print an error message
        }
        Flag = 1;
      }
      break;

    case btnLEFT:
      if (Flag == 0)
      {
        dataString =  String("LCP \t") + String(mapfloat(getCount(), 0, MAX_ENCODER_VALUE, 0, 99));
        File dataFile = SD.open("dataLCP.txt", FILE_WRITE);//open a file named datalog.txt.


        if (dataFile) {       // if the file is available, write to it ('datafile' is returned 1 if SD.open was successful.
          dataFile.println(dataString);//print the concatenated data string and finish the line with a carriage return (println adds the CR automatically after printing the string)
          dataFile.close();   //close the file. IT is a good idea to always open/close a file before and after writing to it. That way, if someone removes the card the file is most
          //likely o.k. and can be read with the computer.

          Serial.println(dataString);// print the string also to the serial port, so we can see what is going on.
        }
        // if SD.open is not successful it returns a 0, i.e. the else{} is executed if the file could not be opened/created successfully.
        else {
          Serial.println("error opening dataLCP.txt");//in that case print an error message
        }
        Flag = 1;
      }

      break;

    case btnRESULT:
      break;

    case btnNONE:
      Flag = 0;
      break;

  }//closes switch


  lcd.setCursor(0, 0);
  lcd.print("Contact Points:  ");
  lcd.setCursor(0, 1);

  float value = mapfloat(getCount(), 0, MAX_ENCODER_VALUE, 0, 99);
  if (value <10)
    lcd.print("0");
  lcd.print(value);

  

}//closes loop


void encoder_interrupt() {
  static uint8_t oldEncoderState = 0;
  oldEncoderState <<= 2;
  oldEncoderState |= ((PIND >> 2) & 0x03);
  direction = encoderDirections[(oldEncoderState & 0x0F)];
  counter += direction;

  if (counter < 0) counter = MAX_ENCODER_VALUE;
  else if (counter > MAX_ENCODER_VALUE) counter = 0;
}

float mapfloat(long x, long in_min, long in_max, long out_min, long out_max)
{
  return float((x - in_min) * float((out_max - out_min)) / (in_max - in_min) + out_min);
}


int getCount ()
{
  noInterrupts();
  int copyCounter = counter;
  interrupts();
  return copyCounter;
}

I upgraded to a 16V DC 850ma.

The recommended input voltage is 7-12v. There may be a maximum rating of 20v. You will be creating alot of heat in the voltage regulator. I don't think that a 16v supply is necessary or desireable.

I am very unclear as to what was the last working code, and what is the broken code.

Are you certain that there are no hardware issues from the power supply event? LCD, encoder, sd module and arduino all check out fine?

You are not providing me with the information to help you solve your problem.

Please post a working sketch and the hardware used. Then post the broken sketch and the hardware used. What did you change between the two versions?

Confirm that the working sketch is working and describe how the broken code is not. Explicity address the lcd, the encoder, and the sd card.

Hi Cattledog, Sorry this is the code that is not working correctly. I ran into a problem when I tried to use a power supply hooked up to the barrel jack. I then tried increasing the amount of power to rule out the problem being caused by insufficient power. At first this problem only occurred when I tried using the power supply hooked up to the barrel jack connector with my latest code shown below...when i used the usb serial cable everything worked fine. However the code will not work now when I use either the usb serial cable or the power supply hooked to the barrel jack connector. The code I posted in the previous post does work though. And even works with the 9v battery hooked up to the barrel jack connector. This problem happen whether I am using the lcd shield with the arduino UNO or arduino Mega board. What I find weird is

1:how the code below would work with the usb serial cable before and not now 2:how the code in the above post works okay with either the 9v battery or usb serial cable. 3: For me I can rule out a hardware issue. Note: I have also switched LCD Shields and tried using both the arduino Uno and arduino mega board. I also tried different DC power supplies to rule out insufficient power issues.

 #include 
#include 
#include 
#define MAX_ENCODER_VALUE 4095
const int chipSelect = 53;
const int8_t encoderDirections[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0};
volatile int16_t counter = 0;
volatile int8_t direction = 0;

byte Flag = 1;//add flag to control multiple records for same button press


const int numRows = 2;
const int numCols = 16;
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
String result;
// define some values used by the panel and buttons
int lcd_key     = 0;
int adc_key_in  = 0;
#define btnRIGHT  0
#define btnUP      1
#define btnRESULT  2
#define btnLEFT    3
#define btnSELECT  4
#define btnNONE    5
int read_LCD_buttons()
{
  adc_key_in = analogRead(0);      // read the value from the sensor


  if (adc_key_in < 50)   return btnRIGHT;
  if (adc_key_in < 195)  return btnUP;
  if (adc_key_in < 380)  return btnRESULT;
  if (adc_key_in < 555)  return btnLEFT;
  if (adc_key_in < 790)  return btnSELECT;
  if (adc_key_in < 1024) return btnNONE;//add 
}

void setup() {
  Serial.begin(9600);
  Serial.println("Initializing SD card...");
  pinMode(chipSelect, OUTPUT);//set chip select PIN as output.
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");

  pinMode(18, INPUT_PULLUP);
  pinMode(19, INPUT_PULLUP);

  // set up the LCD's number of columns and rows:
  lcd.begin(numCols, numRows);

  attachInterrupt(4, encoder_interrupt, CHANGE);
  attachInterrupt(5, encoder_interrupt, CHANGE);

}


void loop() {
  // make a string for assembling the data to log:
  String dataString = "";

  lcd_key = read_LCD_buttons();  // read the buttons
  switch (lcd_key)               // depending on which button was pushed, we perform an action
  {
    case btnRIGHT:
      if (Flag == 0)
      {
        dataString =  String("RCP \t") +  String(mapfloat(getCount(), 0, MAX_ENCODER_VALUE, 0, 99));

        File dataFile = SD.open("dataRCP.txt", FILE_WRITE);//open a file named dataRCP.txt.


        if (dataFile) {       // if the file is available, write to it ('datafile' is returned 1 if SD.open was successful.
          dataFile.println(dataString);//print the concatenated data string and finish the line with a carriage return (println adds the CR automatically after printing the string)
          dataFile.close();   //close the file. IT is a good idea to always open/close a file before and after writing to it. That way, if someone removes the card the file is most
          //likely o.k. and can be read with the computer.

         // Serial.println(dataString);// print the string also to the serial port, so we can see what is going on.
        }

        // if SD.open is not successful it returns a 0, i.e. the else{} is executed if the file could not be opened/created successfully.
        else {
          Serial.println("error opening dataRCP.txt");//in that case print an error message
        }
        Flag = 1;
      }
      break;

    case btnLEFT:
      if (Flag == 0)
      {
        dataString =  String("LCP \t") +  String(mapfloat(getCount(), 0, MAX_ENCODER_VALUE, 0, 99));
        File dataFile = SD.open("dataLCP.txt", FILE_WRITE);//open a file named dataLCP.txt.


        if (dataFile) {       // if the file is available, write to it ('datafile' is returned 1 if SD.open was successful.
          dataFile.println(dataString);//print the concatenated data string and finish the line with a carriage return (println adds the CR automatically after printing the string)
          dataFile.close();   //close the file. IT is a good idea to always open/close a file before and after writing to it. That way, if someone removes the card the file is most
          //likely o.k. and can be read with the computer.

          Serial.println(dataString);// print the string also to the serial port, so we can see what is going on.
        }
        // if SD.open is not successful it returns a 0, i.e. the else{} is executed if the file could not be opened/created successfully.
        else {
          Serial.println("error opening dataLCP.txt");//in that case print an error message
        }
        Flag = 1;
      }

      break;

    case btnRESULT:
      break;

    case btnNONE:
      Flag = 0;
      break;

  }//closes switch


  lcd.setCursor(0, 0);
  lcd.print("Contact Points:  ");
  lcd.setCursor(0, 1);

  float value = mapfloat(getCount(), 0, MAX_ENCODER_VALUE, 0, 99);
  if (value <10)
    lcd.print("0");
  lcd.print(value);

  

}//closes loop


void encoder_interrupt() {
  static uint8_t oldEncoderState = 0;
  oldEncoderState <<= 2;
  oldEncoderState |= ((PIND >> 2) & 0x03);
  direction = encoderDirections[(oldEncoderState & 0x0F)];
  counter += direction;

  if (counter < 0) counter = MAX_ENCODER_VALUE;
  else if (counter > MAX_ENCODER_VALUE) counter = 0;
}

float mapfloat(long x, long in_min, long in_max, long out_min, long out_max)
{
  return float((x - in_min) * float((out_max - out_min)) / (in_max - in_min) + out_min);
}


int getCount ()
{
  noInterrupts();
  int copyCounter = counter;
  interrupts();
  return copyCounter;
}

I think my next plan will be to place the "Contact Points" string into setup as you suggested and start commenting out parts of code to see if I can find where the problem originates from Bill

I guess i am missing something. Can you please point out for me the exact differences between the two versions of posted code in post 71(not working) and 69(works OK).

What had you tried to modify when you changed the code. What problem with the working code were you trying to fix?

Please describe "not working".

Post #71

would work with the usb serial cable before and not now At first this problem only occurred when I tried using the power supply hooked up to the barrel jack connector with my latest code shown below...when i used the usb serial cable everything worked fine. However the code(post #71) will not work now when I use either the usb serial cable or the power supply hooked to the barrel jack connector.

Post #69

code in the above post (#69) works okay with either the 9v battery or usb serial cable.

Hi Cattledog, I apologize for not being clear with the problem. In post #64 I referred to a problem I had with the sketch when I tried hooking up a 9v battery to the barrel jack connector. The sketch worked great while the mega board was hooked up to the USB serial cable. But it did not function right using the barrel jack. The LCD displayed the string"Contact Points:" starting at 0,0 correctly. The output from the encoder is supposed to be displayed on LCD at 0,1. This is where the problem is. 1. The output is also displayed at 0,0 2. I commented out the "Contact Points: string and noticed the encoder is not working. I changed the power source to the 16V DC 850ma plugged into the barrel jack connector only to see if the problem was due to the board being under powered. But the problem was still there. When I plugged the board back into the USB serial cable the sketch worked as it should with no errors. I tried replacing the LCD shield but there was no change. I even tried replacing the mega board with a Arduino UNO board but it behaved the same way as the mega board reproducing the LCD display error. I then decided to test the LCD shield by uploading the hello world sketch. The sketch ran fine either booked up to the USB serial cable or using the power adapter hooked to the barrel jack. I then uploaded a previous sketch for the encoder. Post #34 this sketch read the output from the encoder and displayed it on the LCD screen no problem while hooked to the USB serial cable or to the power adapter. What I don't understand is how the sketch in post #69 worked okay with the serial cable and not with the power adapter. And now the program will not work with either the serial cable or the power adapter. I have changed nothing in the hardware. I hope I have explained better what's going on. Bill

I commented out the code

//lcd.setCursor(0, 0);
  //lcd.print("Contact Points:  ");
  //lcd.setCursor(0, 1);

Which comes after the switch statement for the buttons and this is what appeared on the lcd display starting at 0,0

00.0000.0000.00 The encoder is not working as well.

Hi all, I narrowed down the problem to the micro sd card module I am using. I removed all reference of it in the sketch and the sketch works fine using the 9v battery attached to the barrel jack. Not sure why this would be but I will look into it and see what I can come up with.

hi All, Just another update. I solved the problem of the micro sd card malfunction. Turns out that the card didn't work properly when I had it powered off the 3v pin on my arduino mega board. Switched the power over to the 5v pin on the arduino board and everything works like a charm. Whether hooked to the usb serial cable or to the 9v battery plugged into the barrel jack. Thanks for your help and time Cattledog in trying to figure this problem Bill

Been busy with modifying this sketch. When I click on the upBTN, I get the "result" result = leftBtn - rightBtn; I also get the number of the button press

 if(increaseA < 100)
   {
       increaseA++;
   }
   else increaseA = 0;

This represents the number on the safe dial I am testing. An example in my gap.txt would look like this. 0 00.00 1 06.13 2 07.45 and so on up to 99. When I click on the down button I would like to do the same except I want to have the counter increment by 2.5. An example would look like this 0 00.00 2.5 07.12 5 06.34 and so on up to 97.5 At the moment I have it incrementing by 1. My question is how can I make the counter increment by 2.5. One other question is if I have done the following functions okay or is there a better way of writing the code. Bill

#include 
#include 
#include 
#define MAX_ENCODER_VALUE 4095
const int chipSelect = 53;
const int8_t encoderDirections[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0};
volatile int16_t counter = 0;
volatile int8_t direction = 0;
float rightBtn = 0;
float leftBtn = 0;
float result = 0;
int increaseA = 0;//add counter that will increment by 1 on each btnUP press
int increaseB = 0;//add counter that will  increment by 2.5 on each btnDOWN press..not working yet
byte Flag = 1;//add flag to control multiple records for same button press


const int numRows = 2;
const int numCols = 16;
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

// define some values used by the panel and buttons
int lcd_key     = 0;
int adc_key_in  = 0;
#define btnRIGHT  0
#define btnUP      1
#define btnDOWN    2
#define btnLEFT    3
#define btnSELECT  4
#define btnNONE    5
int read_LCD_buttons()
{
  adc_key_in = analogRead(0);      // read the value from the sensor


  if (adc_key_in < 50)   return btnRIGHT;
  if (adc_key_in < 195)  return btnUP;
  if (adc_key_in < 380)  return btnDOWN;
  if (adc_key_in < 555)  return btnLEFT;
  if (adc_key_in < 790)  return btnSELECT;
  if (adc_key_in < 1024) return btnNONE;//add 
}

void setup() {
  //Serial.begin(9600);
  //Serial.println("Initializing SD card...");
  pinMode(chipSelect, OUTPUT);//set chip select PIN as output.
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    //Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  //Serial.println("card initialized.");

  pinMode(18, INPUT_PULLUP);
  pinMode(19, INPUT_PULLUP);

  // set up the LCD's number of columns and rows:
  lcd.begin(numCols, numRows);

  attachInterrupt(4, encoder_interrupt, CHANGE);
  attachInterrupt(5, encoder_interrupt, CHANGE);

}


void loop() {
  // make a string for assembling the data to log:
  String dataString = "";
  

  lcd_key = read_LCD_buttons();  // read the buttons
  switch (lcd_key)               // depending on which button was pushed, we perform an action
  {
    case btnRIGHT:
      if (Flag == 0)
      {
       
        rightBtn = (mapfloat(getCount(), 0, MAX_ENCODER_VALUE, 0, 99));
      
        
        Flag = 1;
      }
      break;

    case btnLEFT:
      if (Flag == 0)
      {
        
        leftBtn = (mapfloat(getCount(), 0, MAX_ENCODER_VALUE, 0, 99));
        
        
        Flag = 1;
      }

      break;

    /**********************************************************************************/

      case btnUP:
      if (Flag == 0)
    {
    
result = leftBtn - rightBtn;
    
      lcd.setCursor(0, 1);
      lcd.print("Gap Dif:");
      lcd.setCursor(9, 1);
      lcd.print(result);
      delay(10000);//Give time to read result before it clears off screen
      lcd.clear();
      if(increaseA < 100)
   {
       increaseA++;
   }
   else increaseA = 0; 
      
    dataString =  String (increaseA) + String("\t") + String("Contact Gap \t") +  String(result);
        File dataFile = SD.open("gap.txt", FILE_WRITE);//open a file named contactGap.txt.


        if (dataFile) {       // if the file is available, write to it ('datafile' is returned 1 if SD.open was successful.
          dataFile.println(dataString);//print the concatenated data string and finish the line with a carriage return (println adds the CR automatically after printing the string)
          dataFile.close();   //close the file. IT is a good idea to always open/close a file before and after writing to it. That way, if someone removes the card the file is most
          //likely o.k. and can be read with the computer.

         
        }
        // if SD.open is not successful it returns a 0, i.e. the else{} is executed if the file could not be opened/created successfully.
        else {
          Serial.println("error opening gap.txt");//in that case print an error message
        }

      
 Flag = 1;
    }
      break;


/*************************************************************************************************************/

       case btnDOWN:
      if (Flag == 0)
    {
       Serial.println(increaseB);
result = leftBtn - rightBtn;
    
      lcd.setCursor(0, 1);
      lcd.print("Gap Dif:");
      lcd.setCursor(9, 1);
      lcd.print(result);
      delay(10000);
      lcd.clear();
      if(increaseB < 100)
   {
       increaseB++;
   }
   else increaseB = 0;
      
    dataString =  String (increaseB) + String("\t") + String("Contact Gap \t") +  String(result);
        File dataFile = SD.open("gap.txt", FILE_WRITE);//open a file named contactGap.txt.


        if (dataFile) {       // if the file is available, write to it ('datafile' is returned 1 if SD.open was successful.
          dataFile.println(dataString);//print the concatenated data string and finish the line with a carriage return (println adds the CR automatically after printing the string)
          dataFile.close();   //close the file. IT is a good idea to always open/close a file before and after writing to it. That way, if someone removes the card the file is most
          //likely o.k. and can be read with the computer.

         
        }
        // if SD.open is not successful it returns a 0, i.e. the else{} is executed if the file could not be opened/created successfully.
        else {
          Serial.println("error opening gap.txt");//in that case print an error message
        }

      
 Flag = 1;
    }
      break;
      


    case btnNONE:
      Flag = 0;
      break;

  }//closes switch


  lcd.setCursor(0, 0);
  lcd.print("Encoder:");
  lcd.setCursor(9, 0);

  float value = mapfloat(getCount(), 0, MAX_ENCODER_VALUE, 0, 99);
  if (value <10)
  
    lcd.print("0");
  lcd.print(value);

  

}//closes loop


void encoder_interrupt() {
  static uint8_t oldEncoderState = 0;
  oldEncoderState <<= 2;
  oldEncoderState |= ((PIND >> 2) & 0x03);
  direction = encoderDirections[(oldEncoderState & 0x0F)];
  counter += direction;

  if (counter < 0) counter = MAX_ENCODER_VALUE;
  else if (counter > MAX_ENCODER_VALUE) counter = 0;
}

float mapfloat(long x, long in_min, long in_max, long out_min, long out_max)
{
  return float((x - in_min) * float((out_max - out_min)) / (in_max - in_min) + out_min);
}


int getCount ()
{
  noInterrupts();
  int copyCounter = counter;
  interrupts();
  return copyCounter;
}