Rotary encoder output to lcd 1602a

I went over my code and I think I have it right now.....

No. I explicitly pointed out all the places constructing your Strings where you used the unprotected variable "counter" which needed to be replaced with the function getCount(). You have not done that.

Your code appears to work, so you have not adopted the coding practice which says that when you access a volatile variable bigger than a byte from an isr you need to ensure an interrupt can not change it while it is being read.

You can also clean up the lcd print portion of the code now that you have the getCount() function

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

Here's one possibility on how to manage the multiple records on a button press. Your program will need to read a no button pressed before it can service a button again. Create a global variable (a byte will be fine) called Flag. Start off with it =1. You can start the code in each active button with if (Flag ==0 ){//do what follows} At the end of the case, set Flag = 1.

Add btnNONE to the resistance list, and create the case for button NONE which sets the flag to 0.

case btnNONE:
      Flag = 0;
     break;

Here is some code. I could not follow your bracket arrangements, and I changed them. Be aware that the bracket arrangement in this untested code may not be correct.

#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(float(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(float(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));

}//closes loop


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

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

Thanks Cattledog, It works great now. I will study what you did and work on interfacing the sketch and excel using GOBETWINO which looks interesting. I am also going to work on producing some graphics representing the the rotation of the encoder. Thanks again for your help and patience. Really appreciate it. Bill

Hi all, I have been testing my sketch and have a couple of questions... First off when I rotate the encoder clockwise(1 rotation) the lcd displays the count 1-99 fine. If i keep going clockwise the count should repeat 1-99. It does except for on exception. the second digit on the lcd display remains at 9 unti the count reaches 10.....so 1-20 looks like this 19,29,39,49,59,69,79,89,99,10,11,12,13,14,15,16,17,18,19,20. When I rotate the encoder CCW it reaches 10 and then the 0 remains like this count down from 20. 20,19,18,17,16,15,14,13,12,11,10,90,80,70,60,50,40,30,20,10. Anyone have an idea of why it would do this and how I could correct it. The other question involves refining the encoder output. I have included some pics of how am using the encoder which is mounted to a safe dial. The safe dial is numbered 1-99 and my encoder measures 1-99 on 1 rotation. I would like to refine the measurement to includes 10ths. so when I turn the dial from 1 to 2 1 would get this on the display. 1.0,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2.0. It just a matter of changing the output resolution but I am not sure how to do it........ Here is my code.....Special thanks to cattle dog and Jaakko for helping me get to this point....

#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(float(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(float(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));

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

You need to read the documentation on the map function https://www.arduino.cc/en/Reference/Map The map() function uses integer math so will not generate fractions, when the math might indicate that it should do so. Fractional remainders are truncated, and are not rounded or averaged.

Given the integer nature of the map function, you should remove the cast to float of getCount().

 //lcd.print(map(float(getCount()), 0, MAX_ENCODER_VALUE, 0, 99));
lcd.print(map(getCount(), 0, MAX_ENCODER_VALUE, 0, 99));

If you want to translate 0-4095 into 00.0 to 99.9 you are going to have to write your own function.

Regarding the management of the lcd print out, you were pointed at this in earlier posts #32 and #33

Will also work with the blank space to accommodate when the value decreases.

I would handle it this way

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

This should print a leading zero for values less than 10 on the way up and down.

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