Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Date 11/27/13 00:41 EST
- //Name: MENU_TEST2
- //includes
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- #include <MenuBackend.h>
- LiquidCrystal_I2C lcd(0x27,16,2); //addr 0x27,16 char,2 row
- const int buttonPinU = 2; // pin for Up Button
- const int buttonPinD = 3; //pin for down btn
- const int buttonPinL = 4; // pin for left btn
- const int buttonPinR = 5; // pin for right btn
- const int buttonPinEnter = 6; // pin for return btn
- const int buttonPinEsc = 7; // pin for ESC btn
- int led = 13;
- #define DELAY 100; //Default value of this integer will be 100ms
- int lastButtonPushed = 0; // Compares the button state
- int lastButtonEnterState = LOW; // the previous reading from the Enter input pin
- int lastButtonEscState = LOW; // the previous reading from the ESC input pin
- int lastButtonRState = LOW; // the previous reading from the R button
- int lastButtonLState = LOW; // the previous reading from the L button
- int lastButtonDState = LOW; // the previous reading from the D button
- int lastButtonUState = LOW; // the previous reading from the U button
- int resetPin = 12; // Reset pin is 12
- // holding a button down causes it to scroll through items - these int's will count incremently until it "presses" again.
- long lastEnterDebounceTime = 0; // the last time the output pin was toggled
- long lastEscDebounceTime = 0;
- long lastLDebounceTime = 0;
- long lastRDebounceTime = 0;
- long lastDDebounceTime = 0;
- long lastUDebounceTime = 0;
- long debounceDelay = 500; // debounce time = 500 ms or 0.5 Sec.
- long currentmillis = 0;
- /*===============================================================
- || Simplified menu test...
- || /root
- || |_uptime
- || |_settings
- || | |_Blink LED
- || | |_d100 (delay for 100ms)
- || | |_d200 (delay for 200ms)
- || | |_d300 (delay for 300ms)
- || | |_d400 (delay for 400ms)
- || |_reset
- \\=============================================================*/
- MenuBackend menu = MenuBackend(menuUseEvent,menuChanged);
- MenuItem time = MenuItem("Uptime");
- MenuItem set = MenuItem("Settings");
- MenuItem setblink = MenuItem("LED Blink Rate");
- MenuItem d100 = MenuItem("100ms Harder");
- MenuItem d200 = MenuItem("200ms Faster");
- MenuItem d300 = MenuItem("300ms Stronger");
- MenuItem d400 = MenuItem("400ms Better");
- MenuItem abt = MenuItem("About");
- MenuItem rst = MenuItem("Reset");
- void menuSetup(){
- menu.getRoot().add(time);
- time.addBefore(set);
- time.addAfter(rst);
- set.addAfter(time);
- set.addRight(setblink);
- setblink.addLeft(set);
- setblink.addRight(d100);
- d100.addBefore(d200);
- d200.addAfter(d100);
- d300.addAfter(d200);
- d400.addAfter(d300);
- d400.addBefore(d100);
- //Anytime you select left, you will go back into parent menu
- d100.addLeft(setblink);
- d200.addLeft(setblink);
- d300.addLeft(setblink);
- d400.addLeft(setblink);
- abt.addAfter(set);
- abt.addBefore(rst);
- rst.addAfter(abt);
- rst.addBefore(time);
- }
- //Root menu
- void menuChanged(MenuChangeEvent changed){
- lcd.clear();
- lcd.print("Main Menu");
- lcd.setCursor(0, 1);
- lcd.print(changed.to.getName());
- //Uptime block
- long days = 0;
- long hours = 0;
- long mins = 0;
- long secs = 0;
- secs=currentmillis/1000; //divide milliseconds by 1000 - get seconds
- mins=secs/60; //divide secs by 60, get minutes
- hours=mins/60; //divide mins by 60, get hours
- days=hours/24; //divide hours by 24, get days
- secs=secs-(mins*60);
- mins=mins-(hours*60);
- hours=hours-(days*24);
- MenuItem newMenuItem=changed.to;
- if(newMenuItem.getName()==menu.getRoot()){
- lcd.print("Root");
- }else if(newMenuItem.getName()=="Uptime"){
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Uptime");
- lcd.setCursor(0, 1);
- lcd.print(hours);
- lcd.print(":");
- lcd.print(mins);
- lcd.print(":");
- lcd.print(secs);
- }else if(newMenuItem.getName()=="Settings"){
- lcd.print("LED Blink Rate");
- }else if(newMenuItem.getName()=="LED Blink Rate"){
- //I'm not really sure how to configure this. I want to have a list of options, a menu within a menu basically...
- //so... I use lcd.print(changed.to.getName());
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("LED Blink Rates:");
- lcd.setCursor(0, 1);
- lcd.print(changed.to.getName()); //Experimental...
- // For the next 4 values, I want to change x of delay(x); for the blinking LED loop to 100ms, 200ms, 300ms, or 400ms
- }else if(newMenuItem.getName()=="d100"){
- digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
- delay(100); // wait for a second
- digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
- delay(100);
- //OR we do this...
- //(DELAY, '100'); //basically default...
- }else if(newMenuItem.getName()=="d200"){
- digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
- delay(200); // wait for a second
- digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
- delay(200);
- //(DELAY, '200');
- }else if(newMenuItem.getName()=="d300"){
- digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
- delay(300); // wait for a second
- digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
- delay(300);
- //(DELAY, '300');
- }else if(newMenuItem.getName()=="d400"){
- digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
- delay(400); // wait for a second
- digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
- delay(400);
- //(DELAY, '400');
- }else if(newMenuItem.getName()=="rst"){ //This is to Reset the arduino from the buttons. This will be needed because the board is inside the machine casing.
- lcd.print("Resetting...");
- delay(500);
- digitalWrite(resetPin, LOW);
- }
- }
- void menuUseEvent(MenuUseEvent used){
- //I would like to not use this, but for now I'm keeping it for testing purposes.
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("You used:");
- lcd.setCursor(0, 1);
- lcd.print(used.item.getName());
- delay(1000); //Show this screen for a second, then return to the menu
- menu.toRoot();
- }
- void readButtons(){ //read buttons status
- int reading;
- int buttonEnterState=LOW; // the current reading from this button
- int buttonEscState=LOW; // the current reading from this button
- int buttonRState=LOW; // the current reading from this button
- int buttonLState=LOW; //the current reading from this button
- int buttonDState=LOW; // the current reading from this button
- int buttonUState=LOW; // the current reading from this button
- //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;
- //LEFT Button
- reading = digitalRead(buttonPinL);
- if (reading != lastButtonLState) {
- lastLDebounceTime = millis();
- }
- if((millis() - lastLDebounceTime) > debounceDelay) {
- buttonLState=reading;
- lastLDebounceTime=millis();
- }
- lastButtonLState = reading;
- //RIGHT Button
- reading = digitalRead(buttonPinR);
- if (reading != lastButtonRState) {
- lastRDebounceTime = millis();
- }
- if((millis() - lastRDebounceTime) > debounceDelay) {
- buttonRState=reading;
- lastRDebounceTime=millis();
- }
- lastButtonRState = reading;
- //DOWN Button
- reading = digitalRead(buttonPinD);
- if (reading != lastButtonDState) {
- lastDDebounceTime = millis();
- }
- if(millis() - lastDDebounceTime > debounceDelay) {
- buttonDState=reading;
- lastDDebounceTime=millis();
- }
- lastButtonDState = reading;
- //UP Button
- reading = digitalRead(buttonPinU);
- if (reading != lastButtonUState) {
- lastUDebounceTime = millis();
- }
- if((millis() - lastUDebounceTime) > debounceDelay) {
- buttonUState=reading;
- lastUDebounceTime=millis();
- }
- lastButtonUState = reading;
- //records which button has been pressed
- if (buttonEnterState==HIGH){
- lastButtonPushed=buttonPinEnter;
- }else if(buttonEscState==HIGH){
- lastButtonPushed=buttonPinEsc;
- }else if(buttonRState==HIGH){
- lastButtonPushed=buttonPinR;
- }else if(buttonLState==HIGH){
- lastButtonPushed=buttonPinL;
- }else if(buttonUState==HIGH){
- lastButtonPushed=buttonPinU;
- }else if(buttonDState==HIGH){
- lastButtonPushed=buttonPinD;
- }else{
- lastButtonPushed=0;
- }
- }
- void navigateMenus() {
- MenuItem currentMenu=menu.getCurrent();
- switch (lastButtonPushed){
- case buttonPinEnter:
- if(!(currentMenu.moveDown())){
- menu.use();
- }
- break;
- case buttonPinEsc: menu.getRoot(); //back to main
- break;
- case buttonPinR: menu.moveRight();
- break;
- case buttonPinL: menu.moveLeft();
- break;
- case buttonPinU: menu.moveUp();
- break;
- case buttonPinD: menu.moveDown();
- break;
- }
- lastButtonPushed=0; //reset the lastButtonPushed variable
- }
- void setup()
- {
- //start the LCD
- lcd.backlight();
- lcd.init();
- //Set buttons as inputs
- pinMode(buttonPinU, INPUT); // Set pin 2 as Input
- pinMode(buttonPinD, INPUT); // Set pin 3 as Input
- pinMode(buttonPinL, INPUT); // Set pin 4 as Input
- pinMode(buttonPinR, INPUT); // Set pin 5 as Input
- pinMode(buttonPinEnter, INPUT); // Set pin 6 as Input
- pinMode(buttonPinEsc, INPUT); // Set pin 7 as Input
- pinMode(led, OUTPUT);
- digitalWrite(resetPin, HIGH);
- delay(200);
- // start up screen...
- lcd.setCursor(2, 0);
- lcd.print("Arduino2560");
- lcd.setCursor(1, 1);
- lcd.print("Initialized");
- delay(500);
- lcd.print(".");
- delay(500);
- lcd.print(".");
- delay(500);
- lcd.print(".");
- delay(1000);
- lcd.clear();
- }
- void loop(){
- //LED Blink
- digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
- delay(DELAY); // Use DELAY Integer
- digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
- delay(DELAY);
- menuSetup();
- readButtons();
- navigateMenus();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement