Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- String menuItems[] = {"Display Stats","Set 1", "Set 2", "Send signal"};
- String subMenuItems[] = {"Temperature","Humidity"};
- int readKey;
- int savedDistance = 0;
- int subMenu;
- // Menu control variables
- int menuPage = 0;
- int maxMenuPages = round(((sizeof(menuItems) / sizeof(String)) / 2) + .5);
- int cursorPosition = 0;
- int subMenuPage = 0;
- int maxSubMenuPages1 = round(((sizeof(subMenuItems) / sizeof(String)) / 2) + .5);
- int subCursorPosition = 0;
- byte downArrow[8] = {
- 0b00100, // *
- 0b00100, // *
- 0b00100, // *
- 0b00100, // *
- 0b00100, // *
- 0b10101, // * * *
- 0b01110, // ***
- 0b00100 // *
- };
- byte upArrow[8] = {
- 0b00100, // *
- 0b01110, // ***
- 0b10101, // * * *
- 0b00100, // *
- 0b00100, // *
- 0b00100, // *
- 0b00100, // *
- 0b00100 // *
- };
- byte menuCursor[8] = {
- B01000, // *
- B00100, // *
- B00010, // *
- B00001, // *
- B00010, // *
- B00100, // *
- B01000, // *
- B00000 //
- };
- #include <Wire.h>
- #include <LiquidCrystal.h>
- // Setting the LCD shields pins
- LiquidCrystal lcd(44, 46, 4, 5, 6, 7);
- void setup() {
- // Initializes serial communication
- Serial.begin(9600);
- // Initializes and clears the LCD screen
- lcd.begin(16, 2);
- lcd.clear();
- subMenu=1;
- // Creates the byte for the 3 custom characters
- lcd.createChar(0, menuCursor);
- lcd.createChar(1, upArrow);
- lcd.createChar(2, downArrow);
- }
- void loop() {
- switch(subMenu)
- {
- case 1:
- mainMenuDraw();
- drawCursor();
- operateMainMenu();
- break;
- case 2:
- subMenuDraw();
- drawSubCursor();
- operateSubMenu();
- break;
- }
- }
- void mainMenuDraw() {
- Serial.print(menuPage);
- lcd.clear();
- lcd.setCursor(1, 0);
- lcd.print(menuItems[menuPage]);
- lcd.setCursor(1, 1);
- lcd.print(menuItems[menuPage + 1]);
- if (menuPage == 0) {
- lcd.setCursor(15, 1);
- lcd.write(byte(2));
- } else if (menuPage > 0 and menuPage < maxMenuPages) {
- lcd.setCursor(15, 1);
- lcd.write(byte(2));
- lcd.setCursor(15, 0);
- lcd.write(byte(1));
- } else if (menuPage == maxMenuPages) {
- lcd.setCursor(15, 0);
- lcd.write(byte(1));
- }
- }
- void drawCursor() {
- for (int x = 0; x < 2; x++) { // Erases current cursor
- lcd.setCursor(0, x);
- lcd.print(" ");
- }
- if (menuPage % 2 == 0) {
- if (cursorPosition % 2 == 0) { // If the menu page is even and the cursor position is even that means the cursor should be on line 1
- lcd.setCursor(0, 0);
- lcd.write(byte(0));
- }
- if (cursorPosition % 2 != 0) { // If the menu page is even and the cursor position is odd that means the cursor should be on line 2
- lcd.setCursor(0, 1);
- lcd.write(byte(0));
- }
- }
- if (menuPage % 2 != 0) {
- if (cursorPosition % 2 == 0) { // If the menu page is odd and the cursor position is even that means the cursor should be on line 2
- lcd.setCursor(0, 1);
- lcd.write(byte(0));
- }
- if (cursorPosition % 2 != 0) { // If the menu page is odd and the cursor position is odd that means the cursor should be on line 1
- lcd.setCursor(0, 0);
- lcd.write(byte(0));
- }
- }
- }
- void operateMainMenu() {
- int activeButton = 0;
- while (activeButton == 0) {
- int button;
- readKey = analogRead(0);
- if (readKey < 790) {
- delay(100);
- readKey = analogRead(0);
- }
- button = evaluateButton(readKey);
- switch (button) {
- case 0: // When button returns as 0 there is no action taken
- break;
- case 1: // This case will execute if the "forward" button is pressed
- button = 0;
- switch (cursorPosition) { // The case that is selected here is dependent on which menu page you are on and where the cursor is.
- case 0:
- menuItem1();
- break;
- }
- activeButton = 1;
- mainMenuDraw();
- drawCursor();
- break;
- case 2:
- button = 0;
- if (menuPage == 0) {
- cursorPosition = cursorPosition - 1;
- cursorPosition = constrain(cursorPosition, 0, ((sizeof(menuItems) / sizeof(String)) - 1));
- }
- if (menuPage % 2 == 0 and cursorPosition % 2 == 0) {
- menuPage = menuPage - 1;
- menuPage = constrain(menuPage, 0, maxMenuPages);
- }
- if (menuPage % 2 != 0 and cursorPosition % 2 != 0) {
- menuPage = menuPage - 1;
- menuPage = constrain(menuPage, 0, maxMenuPages);
- }
- cursorPosition = cursorPosition - 1;
- cursorPosition = constrain(cursorPosition, 0, ((sizeof(menuItems) / sizeof(String)) - 1));
- mainMenuDraw();
- drawCursor();
- activeButton = 1;
- break;
- case 3:
- button = 0;
- if (menuPage % 2 == 0 and cursorPosition % 2 != 0) {
- menuPage = menuPage + 1;
- menuPage = constrain(menuPage, 0, maxMenuPages);
- }
- if (menuPage % 2 != 0 and cursorPosition % 2 == 0) {
- menuPage = menuPage + 1;
- menuPage = constrain(menuPage, 0, maxMenuPages);
- }
- cursorPosition = cursorPosition + 1;
- cursorPosition = constrain(cursorPosition, 0, ((sizeof(menuItems) / sizeof(String)) - 1));
- mainMenuDraw();
- drawCursor();
- activeButton = 1;
- break;
- }
- }
- }
- int evaluateButton(int x) {
- int result = 0;
- if (x < 50) {
- result = 1; // right
- } else if (x < 195) {
- result = 2; // up
- } else if (x < 380) {
- result = 3; // down
- } else if (x < 790) {
- result = 4; // left
- }
- return result;
- }
- void drawInstructions() {
- lcd.setCursor(0, 1); // Set cursor to the bottom line
- lcd.print("Use ");
- lcd.write(byte(1)); // Up arrow
- lcd.print("/");
- lcd.write(byte(2)); // Down arrow
- lcd.print(" buttons");
- }
- void menuItem1() { // Function executes when you select the 3rd item from main menu
- int activeButton = 0;
- lcd.clear();
- subMenu=2;
- while (activeButton == 0) {
- int button;
- readKey = analogRead(0);
- if (readKey < 790) {
- delay(100);
- readKey = analogRead(0);
- }
- button = evaluateButton(readKey);
- switch (button) {
- case 4: // This case will execute if the "back" button is pressed
- button = 0;
- activeButton = 1;
- break;
- }
- }
- }
- void subMenuDraw() {
- Serial.print(subMenuPage);
- lcd.clear();
- lcd.setCursor(1, 0);
- lcd.print(subMenuItems[subMenuPage]);
- lcd.setCursor(1, 1);
- lcd.print(subMenuItems[subMenuPage + 1]);
- if (subMenuPage == 0) {
- lcd.setCursor(15, 1);
- lcd.write(byte(2));
- } else if (subMenuPage > 0 and subMenuPage < maxSubMenuPages1) {
- lcd.setCursor(15, 1);
- lcd.write(byte(2));
- lcd.setCursor(15, 0);
- lcd.write(byte(1));
- } else if (subMenuPage == maxSubMenuPages1) {
- lcd.setCursor(15, 0);
- lcd.write(byte(1));
- }
- }
- void drawSubCursor() {
- for (int x = 0; x < 2; x++) { // Erases current cursor
- lcd.setCursor(0, x);
- lcd.print(" ");
- }
- if (subMenuPage % 2 == 0) {
- if (subCursorPosition % 2 == 0) { // If the menu page is even and the cursor position is even that means the cursor should be on line 1
- lcd.setCursor(0, 0);
- lcd.write(byte(0));
- }
- if (subCursorPosition % 2 != 0) { // If the menu page is even and the cursor position is odd that means the cursor should be on line 2
- lcd.setCursor(0, 1);
- lcd.write(byte(0));
- }
- }
- if (subMenuPage % 2 != 0) {
- if (subCursorPosition % 2 == 0) { // If the menu page is odd and the cursor position is even that means the cursor should be on line 2
- lcd.setCursor(0, 1);
- lcd.write(byte(0));
- }
- if (subCursorPosition % 2 != 0) { // If the menu page is odd and the cursor position is odd that means the cursor should be on line 1
- lcd.setCursor(0, 0);
- lcd.write(byte(0));
- }
- }
- }
- void operateSubMenu() {
- int activeButton = 0;
- while (activeButton == 0) {
- int button;
- readKey = analogRead(0);
- if (readKey < 790) {
- delay(100);
- readKey = analogRead(0);
- }
- button = evaluateButton(readKey);
- switch (button) {
- case 0: // When button returns as 0 there is no action taken
- break;
- case 1: // This case will execute if the "forward" button is pressed
- button = 0;
- switch (subCursorPosition) { // The case that is selected here is dependent on which menu page you are on and where the cursor is.
- case 0:
- menuItem1();
- break;
- }
- activeButton = 1;
- subMenuDraw();
- drawSubCursor();
- break;
- case 2:
- button = 0;
- if (subMenuPage == 0) {
- subCursorPosition = subCursorPosition - 1;
- subCursorPosition = constrain(subCursorPosition, 0, ((sizeof(subMenuItems) / sizeof(String)) - 1));
- }
- if (subMenuPage % 2 == 0 and subCursorPosition % 2 == 0) {
- subMenuPage= subMenuPage - 1;
- subMenuPage = constrain(subMenuPage, 0, maxSubMenuPages1);
- }
- if (subMenuPage % 2 != 0 and subCursorPosition % 2 != 0) {
- subMenuPage = subMenuPage - 1;
- subMenuPage = constrain(subMenuPage, 0, maxSubMenuPages1);
- }
- subCursorPosition = subCursorPosition - 1;
- subCursorPosition = constrain(subCursorPosition, 0, ((sizeof(subMenuItems) / sizeof(String)) - 1));
- subMenuDraw();
- drawSubCursor();
- activeButton = 1;
- break;
- case 3:
- button = 0;
- if (subMenuPage % 2 == 0 and subCursorPosition % 2 != 0) {
- subMenuPage = subMenuPage + 1;
- subMenuPage = constrain(subMenuPage, 0, maxSubMenuPages1);
- }
- if (subMenuPage % 2 != 0 and subCursorPosition % 2 == 0) {
- subMenuPage = subMenuPage + 1;
- subMenuPage = constrain(subMenuPage, 0, maxSubMenuPages1);
- }
- subCursorPosition = subCursorPosition + 1;
- subCursorPosition = constrain(subCursorPosition, 0, ((sizeof(subMenuItems) / sizeof(String)) - 1));
- subMenuDraw();
- drawSubCursor();
- activeButton = 1;
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement