Created
March 8, 2013 18:19
-
-
Save akbsteam/5118599 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//example use of LCD4Bit_mod library | |
#include <LCD4Bit_mod.h> | |
//create object to control an LCD. | |
//number of lines in display=1 | |
LCD4Bit_mod lcd = LCD4Bit_mod(2); | |
//these pins can not be changed 2/3 are special pins | |
int encoderPin1 = 2; | |
int encoderPin2 = 3; | |
int encoderSwitchPin = 13; //push button switch | |
int encoderUpdated = 0; | |
//Key message | |
char msgs[5][15] = {"Right Key OK ", | |
"Up Key OK ", | |
"Down Key OK ", | |
"Left Key OK ", | |
"Select Key OK" }; | |
int adc_key_val[5] ={30, 150, 360, 535, 760 }; | |
int NUM_KEYS = 5; | |
int adc_key_in; | |
int key=-1; | |
int oldkey=-1; | |
volatile int lastEncoded = 0; | |
volatile long encoderValue = 0; | |
long lastencoderValue = 0; | |
int lastMSB = 0; | |
int lastLSB = 0; | |
void setup() { | |
Serial.begin(19200); | |
setupEncoder(); | |
lcd.init(); | |
//optionally, now set up our application-specific display settings, overriding whatever the lcd did in lcd.init() | |
//lcd.commandWrite(0x0F);//cursor on, display on, blink on. (nasty!) | |
lcd.clear(); | |
lcd.printIn("KEYPAD testing... pressing"); | |
Serial.println("KEYPAD testing... pressing"); | |
} | |
void setupEncoder() { | |
pinMode(encoderPin1, INPUT); | |
pinMode(encoderPin2, INPUT); | |
pinMode(encoderSwitchPin, INPUT); | |
digitalWrite(encoderPin1, HIGH); //turn pullup resistor on | |
digitalWrite(encoderPin2, HIGH); //turn pullup resistor on | |
digitalWrite(encoderSwitchPin, HIGH); //turn pullup resistor on | |
//call updateEncoder() when any high/low changed seen | |
//on interrupt 0 (pin 2), or interrupt 1 (pin 3) | |
attachInterrupt(0, updateEncoder, CHANGE); | |
attachInterrupt(1, updateEncoder, CHANGE); | |
} | |
void loop() { | |
adc_key_in = analogRead(0); // read the value from the sensor | |
key = get_key(adc_key_in); // convert into key press | |
if (encoderUpdated > 0) | |
{ | |
encoderUpdated = 0; | |
char temp[16]; | |
dtostrf(encoderValue,8,8,temp); | |
String tempAsString = String(temp); | |
String s = "encoder: " + tempAsString + " "; | |
lcd.cursorTo(2, 0); //line=2, x=0 | |
lcd.printIn(temp); | |
Serial.println(s); | |
} else if (key != oldkey) // if keypress is detected | |
{ | |
delay(50); // wait for debounce time | |
adc_key_in = analogRead(0); // read the value from the sensor | |
key = get_key(adc_key_in); // convert into key press | |
if (key != oldkey) | |
{ | |
oldkey = key; | |
if (key >=0){ | |
lcd.cursorTo(2, 0); //line=2, x=0 | |
lcd.printIn(msgs[key]); | |
Serial.println(msgs[key]); | |
} | |
} | |
} else if(!digitalRead(encoderSwitchPin)){ | |
//button is being pushed | |
Serial.println("button pushed"); | |
lcd.cursorTo(2, 0); //line=2, x=0 | |
lcd.printIn("button pushed"); | |
} | |
delay(100); //just here to slow down the output, and show it will work even during a delay | |
} | |
void updateEncoder(){ | |
int MSB = digitalRead(encoderPin1); //MSB = most significant bit | |
int LSB = digitalRead(encoderPin2); //LSB = least significant bit | |
int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number | |
int sum = (lastEncoded << 2) | encoded; //adding it to the previous encoded value | |
if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderValue ++; | |
if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue --; | |
lastEncoded = encoded; //store this value for next time | |
encoderUpdated = 1; | |
} | |
// Convert ADC value to key number | |
int get_key(unsigned int input) | |
{ | |
int k; | |
for (k = 0; k < NUM_KEYS; k++) | |
{ | |
if (input < adc_key_val[k]) | |
{ | |
return k; | |
} | |
} | |
if (k >= NUM_KEYS) | |
k = -1; // No valid key pressed | |
return k; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment