Skip to content

Instantly share code, notes, and snippets.

@Pedro147
Created March 11, 2014 07:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pedro147/9481325 to your computer and use it in GitHub Desktop.
Save Pedro147/9481325 to your computer and use it in GitHub Desktop.
Menu_SR_fourPatterns_exp_4.ino / Menu_SR_fourPatterns_exp_11_modMsg.ino
// http://www.coagula.org/content/pages/tutorial-manage-menu-and-lcd-display-arduino
#include <MenuBackend.h> // MenuBackend library - copyright by Alexander Brevig
#include <LiquidCrystal.h> // this library is included in the Arduino IDE
const int buttonPinLeft = 8; // pin for the Up button
const int buttonPinRight = 9; // pin for the Down button
const int buttonPinEsc = 10; // pin for the Esc button
const int buttonPinEnter = 11; // pin for the Enter button
byte lastButtonPushed = 0;
byte dataPin = A0; // connect to pin 14 on the shift register controlling the LCD display
byte clockPin = A1; // connect to pin 11 on the shift register controlling the LCD display
byte latchPin = A2; // connect to pin 12 on the shift register controlling the LCD display
byte dataPin1 = A3; // connect to pin 14 on the shift register controlling the 7Seg display
byte clockPin1 = A4; // connect to pin 11 on the shift register controlling the 7Seg display
byte latchPin1 = A5; // connect to pin 12 on the shift register controlling the 7Seg display
byte digit[] = {B01100000,B11011010,B11110010,B01100110,B10110110,B10111110,B11100000,B11111110};
byte i = 0;
byte j = 0;
byte k = 0;
byte l = 0;
byte m = 0;
byte n = 0;
byte o = 0;
byte p = 0;
byte blank = B00000000;
byte seq1[15] = {1,2,4,8,16,32,64,128,64,32,16,8,4,2,1};
byte digit1[7] = {B00011000,B00100100,B01000010,B10000001,B01000010,B00100100,B00011000};
byte digit2[7] = {B11000000,B00110000,B00001100,B00000011,B00001100,B00110000,B11000000};
byte digit3[7] = {B10000001,B01000010,B00100100,B00011000,B00100100,B01000010,B10000001};
byte digit4[7] = {B10101010,B01010101,B10101010,B01010101,B10101010,B01010101,B10101010};
byte digit5[10] = {B11000000,B00000011,B00110000,B00001100,B00011000,B00110000,B00001100,B11000000,B00000011,B11000000};
byte digit6[8] = {B00011000,B00111100,B01111110,B11111111,B01111110,B00111100,B00011000,B00000000};
byte digit7[9] = {B11111111,B01111110,B00111100,B00011000,B00000000,B00011000,B00111100,B01111110,B11111111};
byte lastButtonEnterState = LOW; // the previous reading from the Enter input pin
byte lastButtonEscState = LOW; // the previous reading from the Esc input pin
byte lastButtonLeftState = LOW; // the previous reading from the Left input pin
byte lastButtonRightState = LOW; // the previous reading from the Right input pin
long lastEnterDebounceTime = 0; // the last time the output pin was toggled
long lastEscDebounceTime = 0; // the last time the output pin was toggled
long lastLeftDebounceTime = 0; // the last time the output pin was toggled
long lastRightDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 500; // the debounce time
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
//Menu variables
MenuBackend menu = MenuBackend(menuUsed,menuChanged);
//initialize menuitems
MenuItem menu1Item1 = MenuItem("Item1");
MenuItem menu1Item2 = MenuItem("Item2");
MenuItem menu1Item3 = MenuItem("Item3");
MenuItem menu1Item4 = MenuItem("Item4");
MenuItem menu1Item5 = MenuItem("Item5");
MenuItem menu1Item6 = MenuItem("Item6");
MenuItem menu1Item7 = MenuItem("Item7");
MenuItem menu1Item8 = MenuItem("Item8");
void setup()
{
pinMode(buttonPinLeft, INPUT);
pinMode(buttonPinRight, INPUT);
pinMode(buttonPinEnter, INPUT);
pinMode(buttonPinEsc, INPUT);
pinMode(dataPin, OUTPUT); // Configure dataPin as OUTPUT for controlling the LED display
pinMode(latchPin, OUTPUT); // Configure latchPin as OUTPUT for controlling the LED display
pinMode(clockPin, OUTPUT); // Configure clockPin as OUTPUT for controlling the LED display
pinMode(dataPin1, OUTPUT); // Configure dataPin as OUTPUT for 7Seg SR
pinMode(latchPin1, OUTPUT); // Configure latchPin as OUTPUT for 7Seg SR
pinMode(clockPin1, OUTPUT); // Configure clockPin as OUTPUT for 7Seg SR
lcd.begin(16, 2);
//configure menu
menu.getRoot().add(menu1Item1);
menu1Item1.addRight(menu1Item2).addRight(menu1Item3).addRight(menu1Item4).addRight(menu1Item5).addRight(menu1Item6).addRight(menu1Item7).addRight(menu1Item8);
menu.toRoot();
lcd.setCursor(0,0);
lcd.print("Pedro's Digital"); // - THIS DISPLAYS THE TEXT ON THE FIRST LINE OF THE OPENING SCREEN - ///////////////
} // setup()...
void loop()
{
readButtons(); // I split button reading and navigation in two procedures because
navigateMenus(); // in some situations I want to use the button for other purpose (eg. to change some settings)
} //loop()...
// - CODE TO DISPLAY THE TEXT ON THE SECOND LINE OF THE OPENING SCREEN - ///////////////////////////////////////////////
////////// - THE NAME OF EACH PATTERN PRIOR TO IT'S EXECUTION - ////////////////////////////////////////////////////////
////////// - THE PATTERN NUMBER DISPLAYED ON THE 7SEG DISPLAY - ////////////////////////////////////////////////////////
void menuChanged(MenuChangeEvent changed){
MenuItem newMenuItem=changed.to; //get the destination menu
lcd.setCursor(0,1); //set the start position for lcd printing to the second row
if(newMenuItem.getName()==menu.getRoot()){
lcd.print(" LED Patterns ");
}
else if(newMenuItem.getName()=="Item1"){
lcd.print(" KnightRider ");
digitalWrite(latchPin1, LOW);
shiftOut(dataPin1, clockPin1, LSBFIRST,digit[0]);
digitalWrite(latchPin1, HIGH);
delay(100);
}
else if(newMenuItem.getName()=="Item2"){
lcd.print(" Out_In In_Out ");
digitalWrite(latchPin1, LOW);
shiftOut(dataPin1, clockPin1, LSBFIRST,digit[1]);
digitalWrite(latchPin1, HIGH);
delay(100);
}
else if(newMenuItem.getName()=="Item3"){
lcd.print(" In_Out Out_In ");
digitalWrite(latchPin1, LOW);
shiftOut(dataPin1, clockPin1, LSBFIRST,digit[2]);
digitalWrite(latchPin1, HIGH);
delay(100);
}
else if(newMenuItem.getName()=="Item4"){
lcd.print("2ShiftR 2ShiftL");
digitalWrite(latchPin1, LOW);
shiftOut(dataPin1, clockPin1, LSBFIRST,digit[3]);
digitalWrite(latchPin1, HIGH);
delay(100);
}
else if(newMenuItem.getName()=="Item5"){
lcd.print("2 LED's All Over");
digitalWrite(latchPin1, LOW);
shiftOut(dataPin1, clockPin1, LSBFIRST,digit[4]);
digitalWrite(latchPin1, HIGH);
delay(100);
}
else if(newMenuItem.getName()=="Item6"){
lcd.print("MiddleOut OutIn");
digitalWrite(latchPin1, LOW);
shiftOut(dataPin1, clockPin1, LSBFIRST,digit[5]);
digitalWrite(latchPin1, HIGH);
delay(100);
}
else if(newMenuItem.getName()=="Item7"){
lcd.print("AllOnIn AllOnOut");
digitalWrite(latchPin1, LOW);
shiftOut(dataPin1, clockPin1, LSBFIRST,digit[6]);
digitalWrite(latchPin1, HIGH);
delay(100);
}
else if(newMenuItem.getName()=="Item8"){
lcd.print("2_Across 2_Back ");
digitalWrite(latchPin1, LOW);
shiftOut(dataPin1, clockPin1, LSBFIRST,digit[7]);
digitalWrite(latchPin1, HIGH);
delay(100);
}
}
//////////////////////// - CODE TO DISPLAY EIGHT DIFFERENT PATTERNS ON EIGHT LED's VIA SHIFT REGISTER - ////////////
void menuUsed(MenuUseEvent used)
{
if (used.item == "Item1")
{
for(i = 0; i < 15; i++)
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST,seq1[i]);
digitalWrite(latchPin, HIGH);
delay(100);
}
}
else if (used.item == "Item2")
{
for (l = 0; l < 7; l++)
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST,digit3[l]);
digitalWrite(latchPin, HIGH);
delay(100);
}
}
else if (used.item == "Item3")
{
for (j = 0; j < 7; j++)
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST,digit1[j]);
digitalWrite(latchPin, HIGH);
delay(150);
}
}
else if (used.item == "Item4")
{
for (m = 0; m < 7; m++)
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST,digit4[m]);
digitalWrite(latchPin, HIGH);
delay(150);
}
}
else if (used.item == "Item5")
{
for (n = 0; n < 10; n++)
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST,digit5[n]);
digitalWrite(latchPin, HIGH);
delay (150);
}
}
else if (used.item == "Item6")
{
for (o = 0; o < 8; o++)
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST,digit6[o]);
digitalWrite(latchPin, HIGH);
delay(500);
}
}
else if (used.item == "Item7")
{
for (p = 0; p < 9; p++)
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST,digit7[p]);
digitalWrite(latchPin, HIGH);
delay(500);
}
}
else
{
for (k = 0; k < 7; k++)
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST,digit2[k]);
digitalWrite(latchPin, HIGH);
delay(500);
}
}
/////////////////////// - CODE TO CLEAR LED's AND 7SEG DISPLAY AFTER PATTERNS COMPLETED - ////////////////////////////
digitalWrite(latchPin, LOW); // Pull latch LOW to start sending data
shiftOut(dataPin, clockPin, LSBFIRST,blank); // Send the data
digitalWrite(latchPin, HIGH); // Pull latch HIGH to stop sending data
digitalWrite(latchPin1, LOW); // Pull latch LOW to start sending data
shiftOut(dataPin1, clockPin1, LSBFIRST,blank); // Send the data
digitalWrite(latchPin1, HIGH); // Pull latch HIGH to stop sending data
/////////////////////// - CODE TO DISPLAY PATTERN COMPLETED AND BACK TO OPENING SCREEN - ///////////////////
if (used.item == "Item1")
{
lcd.setCursor(0,0);
lcd.print(" Knight Rider ");
lcd.setCursor(0,1);
lcd.print(" Completed ");
delay(2000);
lcd.setCursor(0,0);
lcd.print(" Back to ");
lcd.setCursor(0,1);
lcd.print(" Opening Screen ");
delay(1000);
}
else if (used.item == "Item2")
{
lcd.setCursor(0,0);
lcd.print(" Out_In In_Out ");
lcd.setCursor(0,1);
lcd.print(" Completed ");
delay(2000);
lcd.setCursor(0,0);
lcd.print(" Back to ");
lcd.setCursor(0,1);
lcd.print(" Opening Screen ");
delay(1000);
}
else if (used.item == "Item3")
{
lcd.setCursor(0,0);
lcd.print(" In_Out Out_In ");
lcd.setCursor(0,1);
lcd.print(" Completed ");
delay(2000);
lcd.setCursor(0,0);
lcd.print(" Back to ");
lcd.setCursor(0,1);
lcd.print(" Opening Screen ");
delay(1000);
}
else if (used.item == "Item4")
{
lcd.setCursor(0,0);
lcd.print("2ShiftR 2ShiftL");
lcd.setCursor(0,1);
lcd.print(" Completed ");
delay(2000);
lcd.setCursor(0,0);
lcd.print(" Back to ");
lcd.setCursor(0,1);
lcd.print(" Opening Screen ");
delay(1000);
}
else if (used.item == "Item5")
{
lcd.setCursor(0,0);
lcd.print("2 LED's All Over");
lcd.setCursor(0,1);
lcd.print(" Completed ");
delay(2000);
lcd.setCursor(0,0);
lcd.print(" Back to ");
lcd.setCursor(0,1);
lcd.print(" Opening Screen ");
delay(1000);
}
else if (used.item == "Item6")
{
lcd.setCursor(0,0);
lcd.print("MiddleOut OutIn");
lcd.setCursor(0,1);
lcd.print(" Completed ");
delay(2000);
lcd.setCursor(0,0);
lcd.print(" Back to ");
lcd.setCursor(0,1);
lcd.print(" Opening Screen ");
delay(1000);
}
else if (used.item == "Item7")
{
lcd.setCursor(0,0);
lcd.print("AllOnIn AllOnOut");
lcd.setCursor(0,1);
lcd.print(" Completed ");
delay(2000);
lcd.setCursor(0,0);
lcd.print(" Back to ");
lcd.setCursor(0,1);
lcd.print(" Opening Screen ");
delay(1000);
}
else if (used.item == "Item8")
{
lcd.setCursor(0,0);
lcd.print("2_Across 2_Back ");
lcd.setCursor(0,1);
lcd.print(" Completed ");
delay(2000);
lcd.setCursor(0,0);
lcd.print(" Back to ");
lcd.setCursor(0,1);
lcd.print(" Opening Screen ");
delay(1000);
}
lcd.setCursor(0,0);
lcd.print("Pedro's Digital ");
menu.toRoot();
}
///////////////////////// - READ BUTTONS CODE - ///////////////////////////////////////////////
void readButtons(){ //read buttons status
int reading;
int buttonEnterState=LOW; // the current reading from the Enter input pin
int buttonEscState=LOW; // the current reading from the input pin
int buttonLeftState=LOW; // the current reading from the input pin
int buttonRightState=LOW; // the current reading from the input pin
//Enter button
// read the state of the switch into a local variable:
reading = digitalRead(buttonPinEnter);
// check to see if you just pressed the enter button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonEnterState) {
// reset the debouncing timer
lastEnterDebounceTime = millis();
}
if ((millis() - lastEnterDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
buttonEnterState=reading;
lastEnterDebounceTime=millis();
}
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonEnterState = reading;
//Esc button
// read the state of the switch into a local variable:
reading = digitalRead(buttonPinEsc);
// check to see if you just pressed the Down button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonEscState) {
// reset the debouncing timer
lastEscDebounceTime = millis();
}
if ((millis() - lastEscDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
buttonEscState = reading;
lastEscDebounceTime=millis();
}
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonEscState = reading;
//Down button
// read the state of the switch into a local variable:
reading = digitalRead(buttonPinRight);
// check to see if you just pressed the Down button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonRightState) {
// reset the debouncing timer
lastRightDebounceTime = millis();
}
if ((millis() - lastRightDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
buttonRightState = reading;
lastRightDebounceTime =millis();
}
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonRightState = reading;
//Up button
// read the state of the switch into a local variable:
reading = digitalRead(buttonPinLeft);
// check to see if you just pressed the Down button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonLeftState) {
// reset the debouncing timer
lastLeftDebounceTime = millis();
}
if ((millis() - lastLeftDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
buttonLeftState = reading;
lastLeftDebounceTime=millis();
;
}
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonLeftState = reading;
//records which button has been pressed
if (buttonEnterState==HIGH){
lastButtonPushed=buttonPinEnter;
}
else if(buttonEscState==HIGH){
lastButtonPushed=buttonPinEsc;
}
else if(buttonRightState==HIGH){
lastButtonPushed=buttonPinRight;
}
else if(buttonLeftState==HIGH){
lastButtonPushed=buttonPinLeft;
}
else{
lastButtonPushed=0;
}
}
/////////////////////////// - NAVIGATE MENU's CODE - ////////////////////////////////////////////////////
void navigateMenus() {
MenuItem currentMenu=menu.getCurrent();
switch (lastButtonPushed){
case buttonPinEnter:
if(!(currentMenu.moveDown())){ // if the current menu has a child and has been
// pressed enter then menu navigate to item below
menu.use();
}
else{ //otherwise, if menu has no child and has been pressed enter the current menu is used
menu.moveDown();
}
break;
case buttonPinEsc:
menu.toRoot(); //back to main
break;
case buttonPinRight:
menu.moveRight();
break;
case buttonPinLeft:
menu.moveLeft();
break;
}
lastButtonPushed=0; //reset the lastButtonPushed variable
}
// http://www.coagula.org/content/pages/tutorial-manage-menu-and-lcd-display-arduino
#include <MenuBackend.h> //MenuBackend library - copyright by Alexander Brevig
#include <LiquidCrystal.h> //this library is included in the Arduino IDE
const int buttonPinLeft = 8; // pin for the Up button
const int buttonPinRight = 9; // pin for the Down button
const int buttonPinEsc = 10; // pin for the Esc button
const int buttonPinEnter = 11; // pin for the Enter button
byte lastButtonPushed = 0;
byte latchPin = A2; // connect to pin 12 on the shift register
byte dataPin = A0; // connect to pin 14 on the shift register
byte clockPin = A1; // connect to pin 11 on the shift register
byte i = 0;
byte digit = 0;
byte blank = B00000000;
#define pattern1 B00011000
#define pattern2 B00111100
#define pattern3 B01111110
#define pattern4 B11111111
byte lastButtonEnterState = LOW; // the previous reading from the Enter input pin
byte lastButtonEscState = LOW; // the previous reading from the Esc input pin
byte lastButtonLeftState = LOW; // the previous reading from the Left input pin
byte lastButtonRightState = LOW; // the previous reading from the Right input pin
long lastEnterDebounceTime = 0; // the last time the output pin was toggled
long lastEscDebounceTime = 0; // the last time the output pin was toggled
long lastLeftDebounceTime = 0; // the last time the output pin was toggled
long lastRightDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 500; // the debounce time
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
//Menu variables
MenuBackend menu = MenuBackend(menuUsed,menuChanged);
//initialize menuitems
MenuItem menu1Item1 = MenuItem("Item1");
MenuItem menu1Item2 = MenuItem("Item2");
MenuItem menu1Item3 = MenuItem("Item3");
MenuItem menu1Item4 = MenuItem("Item4");
void setup()
{
pinMode(buttonPinLeft, INPUT);
pinMode(buttonPinRight, INPUT);
pinMode(buttonPinEnter, INPUT);
pinMode(buttonPinEsc, INPUT);
pinMode(dataPin, OUTPUT); // Configure dataPin as OUTPUT
pinMode(latchPin, OUTPUT); // Configure latchPin as OUTPUT
pinMode(clockPin, OUTPUT); // Configure clockPin as OUTPUT
lcd.begin(16, 2);
//configure menu
menu.getRoot().add(menu1Item1);
menu1Item1.addRight(menu1Item2).addRight(menu1Item3).addRight(menu1Item4);
menu.toRoot();
lcd.setCursor(0,0);
lcd.print("Pedro's Digital");
} // setup()...
void loop()
{
readButtons(); //I split button reading and navigation in two procedures because
navigateMenus(); //in some situations I want to use the button for other purpose (eg. to change some settings)
} //loop()...
void menuChanged(MenuChangeEvent changed){
MenuItem newMenuItem=changed.to; //get the destination menu
lcd.setCursor(0,1); //set the start position for lcd printing to the second row
if(newMenuItem.getName()==menu.getRoot()){
lcd.print(" LED Patterns ");
}
else if(newMenuItem.getName()=="Item1"){
lcd.print(" Pattern 1 ");
}
else if(newMenuItem.getName()=="Item2"){
lcd.print(" Pattern 2 ");
}
else if(newMenuItem.getName()=="Item3"){
lcd.print(" Pattern 3 ");
}
else if(newMenuItem.getName()=="Item4"){
lcd.print(" Pattern 4 ");
}
}
void menuUsed(MenuUseEvent used)
{
if (used.item == "Item1")
digit=pattern1;
else if (used.item == "Item2")
digit=pattern2;
else if (used.item == "Item3")
digit=pattern3;
else
digit=pattern4;
{
digitalWrite(latchPin, LOW); // Pull latch LOW to start sending data
shiftOut(dataPin, clockPin, LSBFIRST,digit); // Send the data
digitalWrite(latchPin, HIGH); // Pull latch HIGH to stop sending data
delay(1000);
digitalWrite(latchPin, LOW); // Pull latch LOW to start sending data
shiftOut(dataPin, clockPin, LSBFIRST,blank); // Send the data
digitalWrite(latchPin, HIGH); // Pull latch HIGH to stop sending data
}
lcd.setCursor(0,0);
lcd.print("PatternDisplayed");
lcd.setCursor(0,1);
lcd.print(" Back to Menu");
delay(2000); //delay to allow message reading
lcd.setCursor(0,0);
lcd.print("Pedro's Digital ");
menu.toRoot();
}
void readButtons(){ //read buttons status
int reading;
int buttonEnterState=LOW; // the current reading from the Enter input pin
int buttonEscState=LOW; // the current reading from the input pin
int buttonLeftState=LOW; // the current reading from the input pin
int buttonRightState=LOW; // the current reading from the input pin
//Enter button
// read the state of the switch into a local variable:
reading = digitalRead(buttonPinEnter);
// check to see if you just pressed the enter button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonEnterState) {
// reset the debouncing timer
lastEnterDebounceTime = millis();
}
if ((millis() - lastEnterDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
buttonEnterState=reading;
lastEnterDebounceTime=millis();
}
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonEnterState = reading;
//Esc button
// read the state of the switch into a local variable:
reading = digitalRead(buttonPinEsc);
// check to see if you just pressed the Down button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonEscState) {
// reset the debouncing timer
lastEscDebounceTime = millis();
}
if ((millis() - lastEscDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
buttonEscState = reading;
lastEscDebounceTime=millis();
}
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonEscState = reading;
//Down button
// read the state of the switch into a local variable:
reading = digitalRead(buttonPinRight);
// check to see if you just pressed the Down button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonRightState) {
// reset the debouncing timer
lastRightDebounceTime = millis();
}
if ((millis() - lastRightDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
buttonRightState = reading;
lastRightDebounceTime =millis();
}
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonRightState = reading;
//Up button
// read the state of the switch into a local variable:
reading = digitalRead(buttonPinLeft);
// check to see if you just pressed the Down button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonLeftState) {
// reset the debouncing timer
lastLeftDebounceTime = millis();
}
if ((millis() - lastLeftDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
buttonLeftState = reading;
lastLeftDebounceTime=millis();
;
}
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonLeftState = reading;
//records which button has been pressed
if (buttonEnterState==HIGH){
lastButtonPushed=buttonPinEnter;
}
else if(buttonEscState==HIGH){
lastButtonPushed=buttonPinEsc;
}
else if(buttonRightState==HIGH){
lastButtonPushed=buttonPinRight;
}
else if(buttonLeftState==HIGH){
lastButtonPushed=buttonPinLeft;
}
else{
lastButtonPushed=0;
}
}
void navigateMenus() {
MenuItem currentMenu=menu.getCurrent();
switch (lastButtonPushed){
case buttonPinEnter:
if(!(currentMenu.moveDown())){ //if the current menu has a child and has been pressed enter then menu navigate to item below
menu.use();
}
else{ //otherwise, if menu has no child and has been pressed enter the current menu is used
menu.moveDown();
}
break;
case buttonPinEsc:
menu.toRoot(); //back to main
break;
case buttonPinRight:
menu.moveRight();
break;
case buttonPinLeft:
menu.moveLeft();
break;
}
lastButtonPushed=0; //reset the lastButtonPushed variable
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment