#include <Arduino.h>
// func declarations
void printSelected(uint8_t, uint8_t);
boolean btnIsDown(int);
boolean btnIsUp(int);
// setup enum with all the menu pages options
enum pageType {ROOT_MENU, SUB_MENU1, SUB_MENU2, SUB_MENU3};
enum pageType currPage = ROOT_MENU;
// constant holding pot adresses
const int BTN_ACCEPT = 3;
const int BTN_UP = 7;
const int BTN_DOWN = 2;
const int BTN_CANCEL = 5;
//================================================================================
//| page_RootMenu |
//================================================================================
void page_RootMenu(void){
// flag for updating the display
bool updateDisplay = true;
// track when entered top of loop
uint32_t loopStartMs;
// track button states
boolean btn_Up_WasDown = false;
boolean btn_Down_WasDown = false;
boolean btn_Accept_WasDown = false;
// selected item pointer
uint8_t sub_Pos = 1;
// inner loop
while (true){
loopStartMs = millis();
if(updateDisplay){
// clear the update flag
updateDisplay = 0;
// print the display
printSelected(1,sub_Pos);Serial.println(F("First selectable item"));
printSelected(2,sub_Pos);Serial.println(F("Second selectable item"));
printSelected(3,sub_Pos);Serial.println(F("Third selectable item"));
printSelected(4,sub_Pos);Serial.println(F("Forth selectable item"));
}
// capture button down state
if(btnIsDown(BTN_UP)){btn_Up_WasDown = true;}
if(btnIsDown(BTN_DOWN)){btn_Down_WasDown = true;}
if(btnIsDown(BTN_ACCEPT)){btn_Accept_WasDown = true;}
// move the pointer down
if(btn_Down_WasDown && btnIsUp(BTN_DOWN)){
/*
Serial.println("HER");
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
*/
sub_Pos++;
updateDisplay = true;
btn_Down_WasDown = false;
}
if(btn_Up_WasDown && btnIsUp(BTN_UP)){
sub_Pos--;
updateDisplay = true;
btn_Up_WasDown = false;
}
if(btn_Accept_WasDown && btnIsUp(BTN_ACCEPT)){
//sub_Pos--;
//updateDisplay = true;
for(int i = 0;i++;i<10)
{
// sub_Pos++;
//updateDisplay = true;
/*
digitalWrite(12, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(12, LOW); // turn the LED off by making the voltage LOW
delay(1000);
*/ // wait for a second
}
btn_Accept_WasDown = false;
}
// keep a specific pace
while(millis() - loopStartMs < 25) {delay(2);} // hold dette loop kørende med at gøre ingen ting indtil der er gået 25ms
}
}
//================================================================================
//| page_SubMenu1 |
//================================================================================
void page_SubMenu1(void){
}
//================================================================================
//| page_SubMenu2 |
//================================================================================
void page_SubMenu2(void){
}
//================================================================================
//| page_SubMenu3 |
//================================================================================
void page_SubMenu3(void){
}
//================================================================================
//| btnIsDown check if button is pushed |
//================================================================================
boolean btnIsDown(int btn){
if(digitalRead(btn) == LOW && digitalRead(btn) == LOW)
{
// Serial.println("HER i btn Down");
//Serial.println(btn);
//delay(2000);
}
return digitalRead(btn) == LOW && digitalRead(btn) == LOW;
}
//================================================================================
//| btnIsUp check if button is Up |
//================================================================================
boolean btnIsUp(int btn){
if(digitalRead(btn) == HIGH && digitalRead(btn) == HIGH)
{
return digitalRead(btn) == HIGH && digitalRead(btn) == HIGH;
}
else
{
/*
Serial.println("HER i btn UP");
Serial.println(btn);
delay(2000);
*/
}
}
//================================================================================
//| TOOLS DISPLAY printSelected Arrow |
//================================================================================
void printSelected(uint8_t p1, uint8_t p2){
if(p1 == p2){
Serial.print(F("--> "));
}
else{
Serial.print(F(" "));
}
}
//================================================================================
//| ****SETUP***** |
//================================================================================
void setup() {
// init serial port
Serial.begin(9600);
pinMode(12, OUTPUT);
// wait for a second
// setup IO
pinMode(BTN_ACCEPT, INPUT);
pinMode(BTN_UP, INPUT);
pinMode(BTN_DOWN, INPUT);
pinMode(BTN_CANCEL, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
}
//================================================================================
//| Main Loop |
//================================================================================
void loop() {
// put your main code here, to run repeatedly:
for(int i= 0;i<2;i++)
{
digitalWrite(12, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(12, LOW); // turn the LED off by making the voltage LOW
delay(1000);
}
switch (currPage)
{
case ROOT_MENU:
page_RootMenu();
break;
case SUB_MENU1:
page_SubMenu1();
break;
case SUB_MENU2:
page_SubMenu2();
break;
case SUB_MENU3:
page_SubMenu3();
break;
default:
break;
}
}Cancel
Down
Up
Accept