Well, I worked hard all week and finally got my sketch to do what I want.
Many thanks to Korman. I'm sure it could be done in a more elegant manner and would appreciate if any of you could take the time to look at it and critique it for me.
/*This sketch reads a numbers from a 4x4 matrix keypad using either decimal
or fractional entry and returns a value in thousandths of an inch. */
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
{'7','8','9','A'},
{'4','5','6','B'},
{'1','2','3','C'},
{'0','.','e','/'},
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6,}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
long value = 0; //value in thousandths of an inch
int dec_switch = 0; //0=integer entry, 1=decimal entry, 2=fractional entry
int dec_place = 0; //tracks decimal place
int frac_switch = 0; //switches to fraction mode
int numerator; //numerator value
int denominator; //denominator value
float frac_value; //calculated decimal value of fraction
void setup(){
Serial.begin(9600);
}
void loop(){
char key = keypad.getKey(); //read keypad
switch (key){
case NO_KEY:
// No key read. Do nothing
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
//Serial.print (key);
if (frac_switch == 1){ //prints and evaluates numerator
numerator = numerator * 10 + key - '0'; //prints and evaluates denominator
Serial.print (key);
}
if (frac_switch == 2){ //prints and evaluates denominator
denominator = denominator * 10 + key - '0';
Serial.print (key);
}
if ( dec_switch == 0 ){ //prints and evaluates integer
value = value * 10 + key - '0';
Serial.print (key);
}
else if ( dec_switch == 1 && dec_place < 3){ //prints and evaluates decimal
value = value * 10 + key - '0';
dec_place = dec_place + 1;
Serial.print (key);
}
else if (dec_switch == 1 && dec_place == 3) { //prevents entering extra decimal places
Serial.println ();
Serial.print ("Entry complete. Press Enter");
}
break;
case 'e':
// Enter key
if (frac_switch == 1){ //multiplier for fractional value
value = value * 1000;
}
if (frac_switch == 0 && dec_place == 0){
Serial.print (".000"); //prints decimal placeholder
value = value * 1000; //multiplier for integer only decimal value
}
else if (frac_switch == 0 && dec_place == 1){
Serial.print ("00"); //prints decimal placeholder
value = value * 100; //multiplier for decimal place
}
else if (frac_switch == 0 && dec_place == 2){
Serial.print ("0"); //prints decimal placeholder
value = value * 10; //multiplier for decimal place
}
if (frac_switch == 2) {
frac_value = (float)numerator/denominator; //calculates value of fraction
value = (value + frac_value) * 1000; //multiplier for decimal place
}
Serial.println ();
Serial.print ("The current value is: ");
Serial.print (value);
Serial.print (" thousandths.");
Serial.println ();
//Serial.println (numerator);
//Serial.println (denominator);
//Serial.print (frac_value);
//Serial.println ();
// Reset values. The last number is done
value = 0;
dec_switch = 0;
dec_place = 0;
frac_switch = 0;
numerator = 0;
denominator = 0;
frac_value = 0;
break;
case '.':
dec_switch = 1; //switches to decimal mode
Serial.print ("."); //prints decimal point
break;
case '/':
if (frac_switch == 0){
Serial.print ("-");
frac_switch = 1;
dec_switch = 2;
}
else if (frac_switch == 1){
Serial.print ("/");
frac_switch = 2;
}
break;
default:
// All other keys
Serial.println ();
Serial.print ("Criminy! Don't press key '");
Serial.print (key);
Serial.println ("'! Another ");
Serial.print (value);
Serial.println (" puppies died in China.");
// Reset value. The last number is done
value = 0;
}
}
While I'm waiting for my LCD display to arrive I will work on some routines to prevent entry of invalid data but I would like to make sure I've got the basic sketch in its best form first.
Thanks again.
Greg