In case the above code works as hoped for and you get bored after running it through all it’s modes and paces, here’s a 1’st cut at some test code for an OLED compatible user interface (UI). It doesn’t store nor retrieve info from EEPROM nor control any timer functions. All it does is allow you to make choices using the select button on pin6 and slidey 2 axis pot on the analog inputs, A0 and A1. Push the select button for 1 sec and it’ll enter UI mode. Navigate among the choices (using the pot up/down,left/right) and change what you can. Upon exit (use select button again) you can “save” the setup (or not) and the new CoF and parameters will print to the serial monitor and display on the LCD.
Hopefully
But I wouldn’t hold my breath
#include <OLEDFourBit.h>
// initialize the library with the numbers of the interface pins
OLEDFourBit lcd(12, 11, 5, 4, 9, 8);
// set pin numbers
const byte SelectPin = 6; // the select button pin for this test
const byte LEDPin = 13; // the number of the LED output pin
const byte Vert = 0; // the analog input for up or down button
const byte Horz = 1; // the analog input for left or right button
// initialize the constants
const unsigned long DB_delay = 300; // set a debounce wait time of 300 msecs
const unsigned long MaxTime = 60001; // the max time the timer can run for = 60 secs
const byte MaxHits = 24; // Maximum number if hits allowed
const int LoThrshld = 255; // Low threshold for L/D button push
const int HiThrshld = 767; // High threshold for R/U button push
char* MenuList[] ={" Select CoF: "," Select # shooters: "," Select Delay: ",
" Select # of hits: "," Select stop time: "," Save options: "}
char* CoFlist[] = {"Standard 1","QuickDraw1"," 21 "," Custom 1 ",
" Custom 2 "," Custom 3 "," A vs B "}
char* Savelist[] = {" Exit ","Exit and Use ","Exit and Save"}
// initialize global variables
int CoF = 0; // initialize the mode to be standard
byte NumShooters = 1; // initialize the number of shooters to 1
byte RanDly = 0; // set random to disabled
byte StopHits = MaxHits; // Number if hits allowed before stopping
unsigned long StopTime = MaxTime; // Time that the timer will stop at
byte UI_mode = 0; // set the user interface to off
byte UI_init = 0; // set the user interface to not initialized
int tmpCoF = CoF; // temp version of timer control
byte tmpRanDly = RanDly; // temp version of timer control
byte tmpNumShooters = NumShooters; // temp version of timer control
byte tmpStopHits = StopHits; // temp version of timer control
unsigned long tmpStopTime = StopTime; // temp version of timer control
unsigned long LastTime = 0; // last time LCD was updated
int 4Way[] = {0, 0, 0, 0};
int indxMenu = 0; // preset to land on CoF menu
int indxSave = 1; // preset to exit and use
int numCoF = 7; // # of CoFs, 7 for now
char Line3[21];
void setup()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" User Interface ");
lcd.setCursor(0, 1);
lcd.print(" Initializing ");
delay(2000);
// initialize output pins
pinMode(LEDPin, OUTPUT);
digitalWrite(LEDPin, LOW);
// initialize the input pins with internal pullups
pinMode(SelectPin, INPUT);
digitalWrite(SelectPin, HIGH);
// opens serial port, sets data rate to 9600 bps
Serial.begin(9600);
lcd.setCursor(0, 1);
lcd.print(" Ready ");
// Send this data to the serial monitor for debug purposes only
Serial.println("Initial values are :");
Serial.print("UI_mode is : ");
Serial.println(UI_mode);
Serial.print("CoF is : ");
Serial.println(CoFlist[indxCoF]);
Serial.print("# Shooters is : ");
Serial.println(NumShooters);
Serial.print("Random/Fixed delay is : ");
Serial.println(RanDly);
Serial.print("Stop hits # is : ");
Serial.println(StopHits);
Serial.print("Stop time is : ");
Serial.println(StopTime/1000);
delay(2000);
}
void loop()
{
// read the Select button and see if is been pushed 1 sec
if (digitalRead(SelectPin) == LOW)
{
delay(1000);
if (digitalRead(SelectPin) == LOW)
{
UI_mode = 1;
Serial.print("UI_mode is : ");
Serial.println(UI_mode);
}
}
// now see if the UI is enabled
if (UI_mode)
{
// UI enabled, read the buttons and set 4Way(LRUD)
GetButtons()
if(UI_init == 0)
{
// first pass since enabled so initialize the indices etc etc
UI_init = 1;
indxMenu = 0; // land on CoF menu
indxSave = 1; // be on exit n use
tmpCoF = CoF; // copy control to UI temp
tmpRanDly = RanDly; // copy control to UI temp
tmpNumShooters = NumShooters; // copy control to UI temp
tmpStopHits = StopHits; // copy control to UI temp
tmpStopTime = StopTime; // copy control to UI temp
}
// set menu to determine which menu item is displayed
if(4Way[1]) // right button pressed
{
indxMenu++;
}
if(4Way[0]) // left button pressed
{
indxMenu--;
}
if(indxMenu<0) // wrap around
{
indxMenu=5; // presently 6 menu items 0-5
}
if(indxMenu>5) // wrap around
{
indxMenu=0;
}
switch (indxMenu) // display menu item set via pointer
{
case 0: // CoF menu
if(4Way[2])
{
tmpCoF++;
}
if(4Way[3])
{
tmpCoF--;
}
if(tmpCoF<0)
{
tmpCoF=numCoF;
}
if(tmpCoF>numCoF)
{
tmpCoF=0;
Line3 = CoFlist[tmpCoF];
break;
case 1: // number of shooters menu
if(4Way[2])
{
tmpNumShooters++;
}
if(4Way[3])
{
tmpNumShooters++;
}
if(tmpNumShooters>2)
{
tmpNumShooters=1;
}
char buf[21];
PString(buf, sizeof(buf), tmpNumShooters); // convert to char
Line3 = buf;
break;
case 2: // countdown delay menu
if(4Way[2])
{
tmpRanDly++;
}
if(4Way[3])
{
tmpRanDly++;
}
if(tmpRanDly>1)
{
tmpRanDly=0;
}
char buf[21];
PString(buf, sizeof(buf), tmpRanDly); // convert to char
Line3 = buf;
break;
case 3: // Stop hits menu
if(4Way[2])
{
tmpStopHits++;
}
if(4Way[3])
{
tmpStopHits--;
}
if(tmpStopHits<1)
{
tmpStopHits=MaxHits;
}
if(tmpStopHits>MaxHits)
{
tmpStopHits=1;
}
char buf[21];
PString(buf, sizeof(buf), tmpStopHits); // convert to char
Line3 = buf;
break;
case 4: // Stop time menu
if(4Way[2])
{
tmpStopTime += 1000; // add 1000 msec to stoptime if up is pushed
}
if(4Way[3])
{
tmpStopTime -= 1000; // subtract 1000 msec to stoptime if down is pushed
}
if(tmpStopTime<1000) // min stop time is 1 sec
{
tmpStopTime=MaxTime; // wrap around to max stop time
}
if(tmpStopTime>MaxTime)
{
tmpStopTime = 1001; // wrap around to min stop time, 1 sec
}
char buf[21];
PString(buf, sizeof(buf), tmpStopTime/1000); // convert to char
Line3 = buf;
break;
case 5: // Save options menu
if(4Way[2])
{
indxSave++;
}
if(4Way[3])
{
indxSave--;
}
if(indxSave<0)
{
indxSave=2;
}
if(indxSave>2)
{
indxSave=0;
Line3 = Savelist[indxSave];
break;
default: // error
Line3 = " Error ";
}
// update LCD if something has possibly changed
if(4Way[]) // if any button pushed
{
lcd.clear();
lcd.setCursor(0,0); // go to line 1
lcd.print(MenuList[indxMenu]); // Show the Menu
lcd.setCursor(0,2); // go to line 3
lcd.print(Line3); // show what Menu item
}
// now check if select has been pushed again to exit UI
if (digitalRead(SelectPin) == LOW)
{
// reset UI controls
UI_mode = 0;
UI_init = 0;
// print to monitor exitting UI
Serial.println(" ");
Serial.print("UI_mode is : ");
Serial.println(UI_mode);
Serial.print("Exit option was : ");
Serial.println(Savelist[indxSave]);
Serial.print("CoF is : ");
Serial.println(CoFlist[indxCoF]);
Serial.print("# Shooters is : ");
Serial.println(NumShooters);
Serial.print("Random/Fixed delay is : ");
Serial.println(RanDly);
Serial.print("Stop hits # is : ");
Serial.println(StopHits);
Serial.print("Stop time is : ");
Serial.println(StopTime/1000);
// copy new settings to timer controls is selected
if(indxSave) // exit and use
{
CoF = tmpCoF;
NumShooters = tmpNumShooters;
RanDly = tmpRanDly;
StopHits = tmpStopHits;
StopTime = tmpStopTime;
}
}
}
else
{
// UI is not enabled
if(millis() - LastTime > 2000)
{
LastTime - millis();
// show results of UI interaction on LCD
lcd.clear();
lcd.setCursor(0,0);
lcd.print(CoFlist[CoF]);
lcd.setCursor(0,1);
lcd.print("Stop hits = ");
lcd.print(StopHits);
lcd.setCursor(0,2);
lcd.print("Stop time is : ");
lcd.print(StopTime/1000);
lcd.setCursor(0,3);
lcd.print("1 or 2 : ");
lcd.print(NumShooters);
lcd.print(" Delay: ");
lcd.print(RanDly);
}
}
}
// end of loop
void GetButtons()
{
// this version uses the analog inputs
4Way[] = {0, 0, 0, 0};
digitalWrite(LEDPin, LOW);
if(analogRead(Horz) < LoThrshld)
{
4Way[0] = 1;
Serial.println("Left button pushed");
}
if(analogRead(Horz) > HiThrshld)
{
4Way[1] = 1;
Serial.println("Right button pushed");
}
if(analogRead(Vert) > HiThrshld)
{
4Way[2] = 1;
Serial.println("Up button pushed");
}
if(analogRead(Vert) < LoThrshld)
{
4Way[3] = 1;
Serial.println("Down button pushed");
}
if(4Way[]) // if button pushed debounce it by waiting
{
delay(DB_delay);
digitalWrite(LEDPin, HIGH); // turn on LED
}
}