Advertisement
skizziks_53

Menu w/splash screen ver 1.01

Jan 14th, 2018
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.12 KB | None | 0 0
  1. /*
  2.    version 1.01
  3.  
  4.    This is a menu demo sketch.
  5.    It requires a 20x4 LCD connected by an I2C adapter board.
  6.    It requires a 4x3 keypad using the Arduino keybad library.
  7.    The 'A' and 'B' buttons on the keypad scroll forwards and backwards through 5 different screens.
  8.    On screen 1 and 2, the digits input a number and the * clears the number value.
  9. */
  10. #include <Wire.h>
  11. #include <LiquidCrystal_I2C.h>
  12. // I don't know what library you used exactly?
  13. // It appeared that you had commands from at least two different LCD_I2C libraries, and that don't werk.
  14. // I used this one: https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library
  15.  
  16. #include <Keypad.h>
  17. // I assumed this keypad library: https://playground.arduino.cc/Code/Keypad (authors = Mark Stanley, Alexander Brevig)
  18. const byte ROWS = 4; // Four rows
  19. const byte COLS = 4; // Three columns
  20.  
  21. // Define the Keymap
  22. char keys[][COLS] = {
  23.   {'1', '2', '3', 'A'},
  24.   {'4', '5', '6', 'B'},
  25.   {'7', '8', '9', 'C'},
  26.   {'*', '0', '#', 'D'}
  27. };
  28. // row value removed,,,,, also back-slashes removed ?
  29. // compiler gives an error if you try to declare the rows
  30. // (or more accurately--if you try to declare the first element count in an intialized array or matrix)
  31. // see here: https://forum.arduino.cc/index.php?topic=220385.0
  32.  
  33. // Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
  34. byte rowPins[] = { 0, 1, 2, 3 }; // If you are initializing an array, you don't declare the count of array members.
  35. // Connect keypad COL0, COL1 and COL2 to these Arduino pins.
  36. byte colPins[] = { 4, 5, 6, 7 }; // If you are initializing an array, you don't declare the count of array members.
  37. //  Create the Keypad
  38. Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
  39.  
  40. //#define pins:
  41.  
  42. #define I2C_ADDR    0x27 // LCD address (confirmed by scanner)
  43.  
  44. // ??? you don't define separate pins if you are using an I2C device, other than defining the I2C channel if the board has more than one I2C channel.
  45. /*
  46.   #define Rs_pin  0        // Assign pins between I2C and LCD
  47.   #define Rw_pin  1
  48.   #define En_pin  2
  49.   #define BACKLIGHT_PIN 3
  50.   #define D4_pin  4
  51.   #define D5_pin  5
  52.   #define D6_pin  6
  53.   #define D7_pin  7
  54. */
  55.  
  56. // The constructor statement below is correct for the fdebrabander liquidcrystal_i2c library.
  57. LiquidCrystal_I2C lcd(I2C_ADDR, 20, 4);
  58.  
  59. int buttonCheck_interval = 100; // This is the milliseconds time that the keypad is checked for button presses.
  60. unsigned long buttonCheck_previous_time = 0;
  61. unsigned long buttonCheck_current_time = 0;
  62.  
  63. int button_debounce_time = 500; // This is the milliseconds time for button debouncing (the time delay after a button has been pressed).
  64. unsigned long button_previous_time = 0;
  65. unsigned long button_current_time = 0;
  66. boolean buttons_enabled = false; // This is a "switch" for enabling or disabling checks for button presses.
  67.  
  68. int menu_page = 0; // This is a variable to indicate what menu page is being displayed.
  69. // The splash screen is #zero. Four more screens are included already, numbered 1 through 4.
  70.  
  71. int splash_screen_timer = 5000; // This is the time in milliseconds that the splash screen will show. After that it should change to screen #1.
  72. // You can scroll backward to it again, but it will automatically switch to page 1 only once on startup.
  73. // This timer starts from zero and only runs once, so you just need to compare the current_time with the timer value above
  74. int splash_screen_current_time = 0;
  75. int splashScreen_runMode = 0; // This is a flag to indicate if the splash screen has automatically changed once or not.
  76.  
  77. char keyPadValue = 0; // This is the variable to capture keypad inputs.
  78.  
  79. long input_number = 0;
  80. long screen_input_numbers[5]; // This is for storing a screen number (an input value) for each different screen.
  81. // ----------------------------- screens 1 and 2 are set to allow entering a number.
  82.  
  83. // function declarations------------
  84. void screen_0_buttonPress(char);
  85. void screen_1_buttonPress(char);
  86. void screen_2_buttonPress(char);
  87. void screen_3_buttonPress(char);
  88. void screen_4_buttonPress(char);
  89. void draw_screen(int);
  90. void draw_screen1();
  91. void draw_screen2();
  92. void draw_screen3();
  93. void draw_screen4();
  94. void draw_splash_screen();
  95. void start_buttonDebounce();
  96. void clear_lcd_display();
  97. void clear_lcd_line1();
  98. void clear_lcd_line2();
  99. void clear_lcd_line3();
  100. void clear_lcd_line4();
  101. void GetNumber(char);
  102.  
  103.  
  104. void setup() {
  105.   pinMode (11, OUTPUT);
  106.   pinMode (12, OUTPUT);
  107.   pinMode (13, OUTPUT);
  108.  
  109.   for (int x = 0; x < 5; x++) {
  110.     screen_input_numbers[x] = 0;
  111.   }
  112.  
  113.   // Before you send any commands to the LCD, you must initialize it.
  114.   lcd.begin(); // The rows and columns are not included here. They were set in line 52 in the constructor call.
  115.  
  116.   // lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE); // this command is not in the Stanley-Brevig library at all
  117.   lcd.backlight(); // This is how to turn the backlight on
  118.   // lcd.noBacklight(); -------> and this is how you turn it off
  119.   // you can find out all of the available commands by looking in a library's .cpp file.
  120.  
  121.   clear_lcd_display(); // This clears out all four lines of the LCD.
  122. }
  123.  
  124. void loop() {
  125.   if (splashScreen_runMode == 0) {
  126.     menu_page = 0;
  127.     draw_screen(menu_page);
  128.     buttons_enabled = false;
  129.     splashScreen_runMode = 1;
  130.   }
  131.   else if (splashScreen_runMode == 1) {
  132.     splash_screen_current_time = millis();
  133.     if (splash_screen_current_time >= splash_screen_timer) {
  134.       menu_page = 1;
  135.       draw_screen(menu_page);
  136.       splashScreen_runMode = 2;
  137.       buttons_enabled = true;
  138.       buttonCheck_previous_time = millis();
  139.     }
  140.   }
  141.   else {
  142.     if (buttons_enabled == true) {
  143.       //buttonCheck_current_time = millis();
  144.       //if (buttonCheck_current_time >= (buttonCheck_previous_time + buttonCheck_interval)) {
  145.       keyPadValue = kpd.getKey();
  146.       if (keyPadValue) {
  147.         switch (menu_page) {
  148.           case 0:
  149.             screen_0_buttonPress(keyPadValue);
  150.             break;
  151.           case 1:
  152.             screen_1_buttonPress(keyPadValue);
  153.             break;
  154.           case 2:
  155.             screen_2_buttonPress(keyPadValue);
  156.             break;
  157.           case 3:
  158.             screen_3_buttonPress(keyPadValue);
  159.             break;
  160.           case 4:
  161.             screen_4_buttonPress(keyPadValue);
  162.             break;
  163.           default:
  164.             // nothing here
  165.             break;
  166.         }
  167.       }
  168.       //keyPadValue = NO_KEY; // wipe out this keypad value before moving onward
  169.       //}
  170.       //else {
  171.       //  buttonCheck_previous_time = millis(); // rollover condition
  172.       //}
  173.     }
  174.     else { // if buttons_enabled = false
  175.       // If buttons_enabled is false then this checks the time to see if they should be re-enabled.
  176.       button_current_time = millis();
  177.       if (button_current_time > button_previous_time) {
  178.         if (button_current_time >= (button_previous_time + button_debounce_time)) {
  179.           button_previous_time = millis();
  180.           buttons_enabled = true;
  181.         }
  182.       }
  183.       else {
  184.         button_previous_time = millis(); // rollover condition
  185.       }
  186.     }
  187.   }
  188. }
  189.  
  190. void screen_0_buttonPress(char buttonChar) {
  191.   // All the buttons that do anything during the splash screen are defined in here.
  192.   // The only button that works on the splash screen is "B", so you can go back to screen 1
  193.   if (buttonChar == 'B') {
  194.     menu_page = 1;
  195.     draw_screen(menu_page);
  196.   }
  197. }
  198.  
  199. void screen_1_buttonPress(char buttonChar) {
  200.   // All the buttons that do anything during screen #1 are defined in here.
  201.   if (buttonChar == 'A') {
  202.     menu_page = 0;
  203.     draw_screen(menu_page);
  204.   }
  205.   else if (buttonChar == 'B') {
  206.     menu_page = 2;
  207.     draw_screen(menu_page);
  208.   }
  209.   else {
  210.     GetNumber(buttonChar);
  211.   }
  212. }
  213.  
  214. void screen_2_buttonPress(char buttonChar) {
  215.   // All the buttons that do anything during screen #2 are defined in here.
  216.   if (buttonChar == 'A') {
  217.     menu_page = 1;
  218.     draw_screen(menu_page);
  219.   }
  220.   if (buttonChar == 'B') {
  221.     menu_page = 3;
  222.     draw_screen(menu_page);
  223.   }
  224.   else {
  225.     GetNumber(buttonChar);
  226.   }
  227. }
  228.  
  229. void screen_3_buttonPress(char buttonChar) {
  230.   // All the buttons that do anything during screen #3 are defined in here.
  231.   if (buttonChar == 'A') {
  232.     menu_page = 2;
  233.     draw_screen(menu_page);
  234.   }
  235.   if (buttonChar == 'B') {
  236.     menu_page = 4;
  237.     draw_screen(menu_page);
  238.   }
  239. }
  240.  
  241. void screen_4_buttonPress(char buttonChar) {
  242.   // All the buttons that do anything during screen #4 are defined in here.
  243.   // Button "A" is assigned a function to go back to screen-3
  244.   if (buttonChar == 'A') {
  245.     menu_page = 3;
  246.     draw_screen(menu_page);
  247.   }
  248.   // Button "B" has no function because this is the last screen.
  249. }
  250.  
  251. void draw_screen(int screenPage) {
  252.   switch (screenPage) {
  253.     case 0:
  254.       clear_lcd_display();
  255.       draw_splash_screen();
  256.       break;
  257.     case 1:
  258.       clear_lcd_display();
  259.       draw_screen1();
  260.       break;
  261.     case 2:
  262.       clear_lcd_display();
  263.       draw_screen2();
  264.       break;
  265.     case 3:
  266.       clear_lcd_display();
  267.       draw_screen3();
  268.       break;
  269.     case 4:
  270.       clear_lcd_display();
  271.       draw_screen4();
  272.       break;
  273.     default:
  274.       // statements
  275.       break;
  276.   }
  277.   input_number = 0;
  278. }
  279.  
  280. void draw_splash_screen() {
  281.   lcd.setCursor(2, 0); //col,row
  282.   lcd.print("splash screen #0");
  283.   lcd.setCursor(1, 1);
  284.   lcd.print("splishy splashy");
  285.   lcd.setCursor(0, 3);
  286.   lcd.print("rub a dub dub");
  287. }
  288.  
  289. void draw_screen1() {
  290.   lcd.setCursor(1, 0); //col,row
  291.   lcd.print("screen 1");
  292.   lcd.setCursor(1, 1);
  293.   lcd.print("screen 1 stuff");
  294.  
  295.   lcd.setCursor(1, 2);
  296.   lcd.print("input = ?");
  297.   lcd.setCursor(1, 3);
  298.   lcd.print("total = 0");
  299. }
  300.  
  301. void draw_screen2() {
  302.   lcd.setCursor(1, 0); //col,row
  303.   lcd.print("screen 2");
  304.   lcd.setCursor(1, 1);
  305.   lcd.print("screen 2 stuff");
  306.  
  307.   lcd.setCursor(1, 2);
  308.   lcd.print("input = ?");
  309.   lcd.setCursor(1, 3);
  310.   lcd.print("total = 0");
  311. }
  312.  
  313. void draw_screen3() {
  314.   lcd.setCursor(1, 0); //col,row
  315.   lcd.print("screen 3");
  316.   lcd.setCursor(1, 1);
  317.   lcd.print("screen 3 stuff");
  318. }
  319.  
  320. void draw_screen4() {
  321.   lcd.setCursor(1, 0); //col,row
  322.   lcd.print("screen 4");
  323.   lcd.setCursor(1, 1);
  324.   lcd.print("screen 4 stuff");
  325. }
  326.  
  327. void start_buttonDebounce() {
  328.   button_previous_time = millis();
  329.   buttons_enabled = false;
  330. }
  331.  
  332. void clear_lcd_display() {
  333.   // Note: there is an lcd.clear() command in the Stanley-Brevig library that clears all four lines at once,
  334.   // but you may not want to clear all four lines
  335.   clear_lcd_line1();
  336.   clear_lcd_line2();
  337.   clear_lcd_line3();
  338.   clear_lcd_line4();
  339. }
  340.  
  341. void clear_lcd_line1() {
  342.   lcd.setCursor(0, 0); //col,row
  343.   lcd.print("                    "); // print 20 spaces
  344. }
  345.  
  346. void clear_lcd_line2() {
  347.   lcd.setCursor(0, 1); //col,row
  348.   lcd.print("                    "); // print 20 spaces
  349. }
  350.  
  351. void clear_lcd_line3() {
  352.   lcd.setCursor(0, 2); //col,row
  353.   lcd.print("                    "); // print 20 spaces
  354. }
  355.  
  356. void clear_lcd_line4() {
  357.   lcd.setCursor(0, 3); //col,row
  358.   lcd.print("                    "); // print 20 spaces
  359. }
  360.  
  361. void GetNumber(char key) {
  362.   // This function is set to only recognize number keys and the "*" key, which resets the value to zero.
  363.   //long num = 0; ---------------------------- not using this
  364.   // char key = kpd.getKey(); ---------------- not using this either, since we are passing the char value in
  365.   // while (key != '#') { -------------------- not using this either, we can just ignore it here
  366.   switch (key) {
  367.     case NO_KEY:
  368.       break;
  369.  
  370.     case '0': case '1': case '2': case '3': case '4':
  371.     case '5': case '6': case '7': case '8': case '9':
  372.       lcd.setCursor(1, 2);
  373.       lcd.print("input = ");
  374.       lcd.print(key);
  375.       screen_input_numbers[menu_page] = screen_input_numbers[menu_page] * 10 + (key - '0');
  376.       lcd.setCursor(1, 3);
  377.       lcd.print("total = ");
  378.       lcd.print(screen_input_numbers[menu_page]);
  379.       break;
  380.  
  381.     case '*':
  382.       screen_input_numbers[menu_page] = 0;
  383.       lcd.setCursor(1, 3);
  384.       lcd.print("total = ");
  385.       lcd.print(screen_input_numbers[menu_page]);
  386.       break;
  387.   }
  388.   // key = kpd.getKey(); ------ not using this
  389.   //}
  390.   //return num; -- no return value used
  391. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement