Skip to content

Instantly share code, notes, and snippets.

@dadecoza
Last active July 8, 2016 09:09
Show Gist options
  • Save dadecoza/1977a99688fb5a7f129e4544675b56e0 to your computer and use it in GitHub Desktop.
Save dadecoza/1977a99688fb5a7f129e4544675b56e0 to your computer and use it in GitHub Desktop.
This sketch will calibrate and generate an example sketch for reading the Arduino LCD Keypad Shiled.
#define SELECT 0
#define LEFT 1
#define UP 2
#define DOWN 3
#define RIGHT 4
int buttonVals[5];
int orderedVals[5];
String label[] = {"SELECT", "LEFT", "UP", "DOWN", "RIGHT"};
void setup() {
Serial.begin(9600);
delay(100);
// put your setup code here, to run once:
}
int getButtonIndex(int v) {
for (int i = 0; i < 5; i++) {
if (buttonVals[i] == v) {
return i;
}
}
return -1;
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println("Calibration sketch for LCD Keypad!");
Serial.println("Press select button ...");
buttonVals[SELECT] = readButton();
Serial.println("Press left button ...");
buttonVals[LEFT] = readButton();
Serial.println("Press up button ...");
buttonVals[UP] = readButton();
Serial.println("Press down button ...");
buttonVals[DOWN] = readButton();
Serial.println("Press right button ...");
buttonVals[RIGHT] = readButton();
int c, d, swap;
for (c = 0 ; c < 5; c++) orderedVals[c] = buttonVals[c];
for (c = 0 ; c < ( 5 - 1 ); c++) {
for (d = 0 ; d < 5 - c - 1; d++) {
if (orderedVals[d] > orderedVals[d + 1]) {
swap = orderedVals[d];
orderedVals[d] = orderedVals[d + 1];
orderedVals[d + 1] = swap;
}
}
}
int t1 = (orderedVals[0] + orderedVals[1]) / 2;
int t2 = (orderedVals[1] + orderedVals[2]) / 2;
int t3 = (orderedVals[2] + orderedVals[3]) / 2;
int t4 = (orderedVals[3] + orderedVals[4]) / 2;
int avg = ((orderedVals[4] - orderedVals[3]) + (orderedVals[3] - orderedVals[2]) + (orderedVals[2] - orderedVals[1]) + (orderedVals[1] - orderedVals[0])) / 4;
Serial.println("//Beginning of example sketch");
Serial.println();
for (c = 0; c < 5; c++) {
Serial.print("#define "); Serial.print(label[c] + " "); Serial.println(c + 1);
}
Serial.print("#define NONE "); Serial.println(c + 1);
Serial.println();
Serial.println("int getKey() {");
Serial.println(" int b = analogRead(A0);");
Serial.println(" if (b > 1000) return NONE;");
Serial.println(" delay(8);");
Serial.print(" if (b < "); Serial.print(t1); Serial.print(") return "); Serial.println(label[getButtonIndex(orderedVals[0])] + ";");
Serial.print(" if (b < "); Serial.print(t2); Serial.print(") return "); Serial.println(label[getButtonIndex(orderedVals[1])] + ";");
Serial.print(" if (b < "); Serial.print(t3); Serial.print(") return "); Serial.println(label[getButtonIndex(orderedVals[2])] + ";");
Serial.print(" if (b < "); Serial.print(t4); Serial.print(") return "); Serial.println(label[getButtonIndex(orderedVals[3])] + ";");
Serial.print(" if (b < "); Serial.print(orderedVals[4] + avg); Serial.print(") return "); Serial.println(label[getButtonIndex(orderedVals[4])] + ";");
Serial.println(" return NONE;");
Serial.println("}");
Serial.println();
Serial.println("void setup() {");
Serial.println(" Serial.begin(9600);");
Serial.println(" delay(100);");
Serial.println("}");
Serial.println();
Serial.println("void loop() {");
Serial.println(" int k = getKey();");
Serial.println(" switch (k) {");
Serial.println(" case NONE : Serial.println(\"None\"); break;");
Serial.println(" case SELECT : Serial.println(\"Select\"); break;");
Serial.println(" case LEFT : Serial.println(\"Left\"); break;");
Serial.println(" case UP : Serial.println(\"Up\"); break;");
Serial.println(" case DOWN : Serial.println(\"Down\"); break;");
Serial.println(" case RIGHT : Serial.println(\"Right\"); break;");
Serial.println(" }");
Serial.println(" delay(100);");
Serial.println("}");
Serial.println();
Serial.println("//End of example sketch");
}
int readButton() {
int b;
do {
b = analogRead(A0);
} while (b > 1000);
delay(300); //debounce
return b;
}
Calibration sketch for LCD Keypad!
Press select button ...
Press left button ...
Press up button ...
Press down button ...
Press right button ...
//Beginning of example sketch
#define SELECT 1
#define LEFT 2
#define UP 3
#define DOWN 4
#define RIGHT 5
#define NONE 6
int getKey() {
int b = analogRead(A0);
if (b > 1000) return NONE;
delay(8);
if (b < 49) return RIGHT;
if (b < 177) return UP;
if (b < 331) return DOWN;
if (b < 529) return LEFT;
if (b < 815) return SELECT;
return NONE;
}
void setup() {
Serial.begin(9600);
delay(100);
}
void loop() {
int k = getKey();
switch (k) {
case NONE : Serial.println("None"); break;
case SELECT : Serial.println("Select"); break;
case LEFT : Serial.println("Left"); break;
case UP : Serial.println("Up"); break;
case DOWN : Serial.println("Down"); break;
case RIGHT : Serial.println("Right"); break;
}
delay(100);
}
//End of example sketch
Calibration sketch for LCD Keypad!
Press select button ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment