Go Down

Topic: Rotary encoder output to lcd 1602a (Read 15457 times) previous topic - next topic

williamjcoates

I commented out the code
Code: [Select]
//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.

williamjcoates

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.

williamjcoates

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

williamjcoates

#78
Nov 12, 2015, 03:07 am Last Edit: Nov 14, 2015, 02:54 am by williamjcoates
Been busy with modifying this sketch. When I click on the upBTN, I get the "result"
Code: [Select]
result = leftBtn - rightBtn; I also get the number of the button press
Code: [Select]
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
Code: [Select]

#include <SD.h>
#include <SPI.h>
#include <LiquidCrystal.h>
#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;
}

Go Up