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