Hi Cattledog,
Well I have been working on the keypad/button and I have progressed a bit. In this sketch I was trying to print the value of the encoder when the left or right button was pressed to the serial monitor. I used the serial monitor so I could see if the action worked or not. However its not working. Could you point me in the right direction with my button setup. I also tried your suggestion on the button set up which didn't work. I assume because I am missing something.
#include <LiquidCrystal.h>
#define MAX_ENCODER_VALUE 4095
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;
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 btnLEFT 3
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 < 555) return btnLEFT;
}
void setup() {
Serial.begin(115200);
//pinMode(2, INPUT_PULLUP);arduino uno
//pinMode(3, INPUT_PULLUP);arduino uno
pinMode(18, INPUT_PULLUP);
pinMode(19, INPUT_PULLUP);
// set up the LCD's number of columns and rows:
lcd.begin(numCols, numRows);
//attachInterrupt(0, encoder_interrupt, CHANGE);arduino uno
//attachInterrupt(1, encoder_interrupt, CHANGE);arduino uno
attachInterrupt(4, encoder_interrupt, CHANGE);
attachInterrupt(5, encoder_interrupt, CHANGE);
}
void loop() {
switch (lcd_key) // depending on which button was pushed, we perform an action
{
case btnRIGHT:
{
Serial.println (map(float(counter), 0, MAX_ENCODER_VALUE, 0, 99));
break;
}
case btnLEFT:
{
Serial.println (map(float(counter), 0, MAX_ENCODER_VALUE, 0, 99));
break;
}
}
lcd.setCursor(0, 0);
lcd.print("Contact Points: ");
lcd.setCursor(0, 1);
//lcd.print(map(counter, 0, 4095, 0, 99));
noInterrupts();
int copyCounter = counter;
interrupts();
lcd.print(map(float(counter), 0, MAX_ENCODER_VALUE, 0, 99));
}
void encoder_interrupt() {
static uint8_t oldEncoderState = 0;
oldEncoderState <<= 2;
oldEncoderState |= ((PIND >> 2) & 0x03);
counter += encoderDirections[(oldEncoderState & 0x0F)];
if (counter < 0) counter = MAX_ENCODER_VALUE;
else if (counter > MAX_ENCODER_VALUE) counter = 0;
}