Skip to content

Instantly share code, notes, and snippets.

@justinledwards
Last active December 27, 2015 05:38
Show Gist options
  • Save justinledwards/7275245 to your computer and use it in GitHub Desktop.
Save justinledwards/7275245 to your computer and use it in GitHub Desktop.
Working on dial in board
#include <LCD4Bit_mod.h>
LCD4Bit_mod lcd = LCD4Bit_mod(2);
// Keys
// 0 = right
// 1 = up
// 2 = down
// 3 = left
// 4 = select
//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;
// Statuses
// 0 = main screen
// 1 = tens of seconds screen
// 2 = seconds screen
// 3 = tenths of a second screen
// 4 = hundredths of a second screen
int status = 0;
int firstdigit;
int seconddigit;
int thirddigit;
int fourthdigit;
int currentdigit = 0;
boolean digitwritten = true;
char dialin[5] = "";
void setup() {
pinMode(13, OUTPUT); //we'll use the debug LED to output a heartbeat
lcd.init();
lcd.clear();
lcd.printIn("DIAL BOARD");
lcd.cursorTo(2, 0);
lcd.printIn("Select to Start");
}
void loop() {
adc_key_in = analogRead(0); // read the value from the sensor
digitalWrite(13, HIGH);
key = get_key(adc_key_in); // convert into key press
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){
if (status == 0) { //if on main screen
if (key == 4){ //if select is pressed
lcd.clear();
lcd.cursorTo(1,0);
lcd.printIn("Press up or down");
lcd.cursorTo(2,0);
lcd.printIn("Then Select");
delay(2000);
lcd.clear();
status = 1;
lcd.printIn("Tens of Seconds?");
digitwritten = false;
//char keyread[4];
//sprintf(keyread, "%d",key);
// lcd.printIn(keyread);
}
else {
//lcd.clear();
//lcd.cursorTo(2, 0); //line=2, x=0
//lcd.printIn(msgs[key]);
}
} else if (status == 1) { //if on tens of seconds screen
//2 is down 1 is up
lcd.cursorTo(2,0);
char sdigit[1];
sprintf(sdigit, "%d", currentdigit);
lcd.printIn(sdigit);
if (key == 1){
if (currentdigit < 9){ //up button is pressed
currentdigit++ ;
digitwritten = false;
}
} else if (key == 2){ //down button is pressed
if (currentdigit > 0){
currentdigit-- ;
digitwritten = false;
}
} else if (key == 4) { //select button is pressed
firstdigit = currentdigit;
lcd.clear();
status = 2;
lcd.printIn("Seconds?");
currentdigit = 5;
digitwritten = false;
}
} else if (status == 2) {
lcd.cursorTo(2,0);
char sdigit[1];
sprintf(sdigit, "%d", currentdigit);
lcd.printIn(sdigit);
if (key == 1){
if (currentdigit < 9){ //up button is pressed
currentdigit++ ;
digitwritten = false;
}
} else if (key == 2){ //down button is pressed
if (currentdigit > 0){
currentdigit-- ;
digitwritten = false;
}
} else if (key == 4) { //select button is pressed
seconddigit = currentdigit;
lcd.clear();
status = 3 ;
lcd.printIn("Tenths?");
currentdigit = 5;
digitwritten = false;
}
} else if (status == 3) {
lcd.cursorTo(2,0);
char sdigit[1];
sprintf(sdigit, "%d", currentdigit);
lcd.printIn(sdigit);
if (key == 1){
if (currentdigit < 9){ //up button is pressed
currentdigit++ ;
digitwritten = false;
}
} else if (key == 2){ //down button is pressed
if (currentdigit > 0){
currentdigit-- ;
digitwritten = false;
}
} else if (key == 4) { //select button is pressed
thirddigit = currentdigit;
lcd.clear();
status = 4 ;
lcd.printIn("Hundredths?");
currentdigit = 5;
digitwritten = false;
}
} else if (status == 4) {
lcd.cursorTo(2,0);
char sdigit[1];
sprintf(sdigit, "%d", currentdigit);
lcd.printIn(sdigit);
if (key == 1){
if (currentdigit < 9){ //up button is pressed
currentdigit++ ;
digitwritten = false;
}
} else if (key == 2){ //down button is pressed
if (currentdigit > 0){
currentdigit-- ;
digitwritten = false;
}
} else if (key == 4) { //select button is pressed
fourthdigit = currentdigit;
lcd.clear();
status = 0 ;
lcd.clear();
lcd.printIn("DIAL BOARD");
lcd.cursorTo(2, 0);
String pdialin;
char charBuf[5];
pdialin += firstdigit ;
pdialin += seconddigit ;
pdialin += "." ;
pdialin+= thirddigit ;
pdialin += fourthdigit;
pdialin.toCharArray(charBuf, 50);
lcd.printIn(charBuf);
}
}
}
}
}
if (digitwritten) { // if already written
//relax for a millionth of a second
}
else { //lets write the digit
lcd.cursorTo(2,0);
char sdigit[1];
sprintf(sdigit, "%d", currentdigit);
lcd.printIn(sdigit);
digitwritten = true;
}
//delay(1000);
digitalWrite(13, LOW);
}
// 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