Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- version 1.01
- This is a menu demo sketch.
- It requires a 20x4 LCD connected by an I2C adapter board.
- It requires a 4x3 keypad using the Arduino keybad library.
- The 'A' and 'B' buttons on the keypad scroll forwards and backwards through 5 different screens.
- On screen 1 and 2, the digits input a number and the * clears the number value.
- */
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- // I don't know what library you used exactly?
- // It appeared that you had commands from at least two different LCD_I2C libraries, and that don't werk.
- // I used this one: https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library
- #include <Keypad.h>
- // I assumed this keypad library: https://playground.arduino.cc/Code/Keypad (authors = Mark Stanley, Alexander Brevig)
- const byte ROWS = 4; // Four rows
- const byte COLS = 4; // Three columns
- // Define the Keymap
- char keys[][COLS] = {
- {'1', '2', '3', 'A'},
- {'4', '5', '6', 'B'},
- {'7', '8', '9', 'C'},
- {'*', '0', '#', 'D'}
- };
- // row value removed,,,,, also back-slashes removed ?
- // compiler gives an error if you try to declare the rows
- // (or more accurately--if you try to declare the first element count in an intialized array or matrix)
- // see here: https://forum.arduino.cc/index.php?topic=220385.0
- // Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
- byte rowPins[] = { 0, 1, 2, 3 }; // If you are initializing an array, you don't declare the count of array members.
- // Connect keypad COL0, COL1 and COL2 to these Arduino pins.
- byte colPins[] = { 4, 5, 6, 7 }; // If you are initializing an array, you don't declare the count of array members.
- // Create the Keypad
- Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
- //#define pins:
- #define I2C_ADDR 0x27 // LCD address (confirmed by scanner)
- // ??? 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.
- /*
- #define Rs_pin 0 // Assign pins between I2C and LCD
- #define Rw_pin 1
- #define En_pin 2
- #define BACKLIGHT_PIN 3
- #define D4_pin 4
- #define D5_pin 5
- #define D6_pin 6
- #define D7_pin 7
- */
- // The constructor statement below is correct for the fdebrabander liquidcrystal_i2c library.
- LiquidCrystal_I2C lcd(I2C_ADDR, 20, 4);
- int buttonCheck_interval = 100; // This is the milliseconds time that the keypad is checked for button presses.
- unsigned long buttonCheck_previous_time = 0;
- unsigned long buttonCheck_current_time = 0;
- int button_debounce_time = 500; // This is the milliseconds time for button debouncing (the time delay after a button has been pressed).
- unsigned long button_previous_time = 0;
- unsigned long button_current_time = 0;
- boolean buttons_enabled = false; // This is a "switch" for enabling or disabling checks for button presses.
- int menu_page = 0; // This is a variable to indicate what menu page is being displayed.
- // The splash screen is #zero. Four more screens are included already, numbered 1 through 4.
- 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.
- // You can scroll backward to it again, but it will automatically switch to page 1 only once on startup.
- // This timer starts from zero and only runs once, so you just need to compare the current_time with the timer value above
- int splash_screen_current_time = 0;
- int splashScreen_runMode = 0; // This is a flag to indicate if the splash screen has automatically changed once or not.
- char keyPadValue = 0; // This is the variable to capture keypad inputs.
- long input_number = 0;
- long screen_input_numbers[5]; // This is for storing a screen number (an input value) for each different screen.
- // ----------------------------- screens 1 and 2 are set to allow entering a number.
- // function declarations------------
- void screen_0_buttonPress(char);
- void screen_1_buttonPress(char);
- void screen_2_buttonPress(char);
- void screen_3_buttonPress(char);
- void screen_4_buttonPress(char);
- void draw_screen(int);
- void draw_screen1();
- void draw_screen2();
- void draw_screen3();
- void draw_screen4();
- void draw_splash_screen();
- void start_buttonDebounce();
- void clear_lcd_display();
- void clear_lcd_line1();
- void clear_lcd_line2();
- void clear_lcd_line3();
- void clear_lcd_line4();
- void GetNumber(char);
- void setup() {
- pinMode (11, OUTPUT);
- pinMode (12, OUTPUT);
- pinMode (13, OUTPUT);
- for (int x = 0; x < 5; x++) {
- screen_input_numbers[x] = 0;
- }
- // Before you send any commands to the LCD, you must initialize it.
- lcd.begin(); // The rows and columns are not included here. They were set in line 52 in the constructor call.
- // lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE); // this command is not in the Stanley-Brevig library at all
- lcd.backlight(); // This is how to turn the backlight on
- // lcd.noBacklight(); -------> and this is how you turn it off
- // you can find out all of the available commands by looking in a library's .cpp file.
- clear_lcd_display(); // This clears out all four lines of the LCD.
- }
- void loop() {
- if (splashScreen_runMode == 0) {
- menu_page = 0;
- draw_screen(menu_page);
- buttons_enabled = false;
- splashScreen_runMode = 1;
- }
- else if (splashScreen_runMode == 1) {
- splash_screen_current_time = millis();
- if (splash_screen_current_time >= splash_screen_timer) {
- menu_page = 1;
- draw_screen(menu_page);
- splashScreen_runMode = 2;
- buttons_enabled = true;
- buttonCheck_previous_time = millis();
- }
- }
- else {
- if (buttons_enabled == true) {
- //buttonCheck_current_time = millis();
- //if (buttonCheck_current_time >= (buttonCheck_previous_time + buttonCheck_interval)) {
- keyPadValue = kpd.getKey();
- if (keyPadValue) {
- switch (menu_page) {
- case 0:
- screen_0_buttonPress(keyPadValue);
- break;
- case 1:
- screen_1_buttonPress(keyPadValue);
- break;
- case 2:
- screen_2_buttonPress(keyPadValue);
- break;
- case 3:
- screen_3_buttonPress(keyPadValue);
- break;
- case 4:
- screen_4_buttonPress(keyPadValue);
- break;
- default:
- // nothing here
- break;
- }
- }
- //keyPadValue = NO_KEY; // wipe out this keypad value before moving onward
- //}
- //else {
- // buttonCheck_previous_time = millis(); // rollover condition
- //}
- }
- else { // if buttons_enabled = false
- // If buttons_enabled is false then this checks the time to see if they should be re-enabled.
- button_current_time = millis();
- if (button_current_time > button_previous_time) {
- if (button_current_time >= (button_previous_time + button_debounce_time)) {
- button_previous_time = millis();
- buttons_enabled = true;
- }
- }
- else {
- button_previous_time = millis(); // rollover condition
- }
- }
- }
- }
- void screen_0_buttonPress(char buttonChar) {
- // All the buttons that do anything during the splash screen are defined in here.
- // The only button that works on the splash screen is "B", so you can go back to screen 1
- if (buttonChar == 'B') {
- menu_page = 1;
- draw_screen(menu_page);
- }
- }
- void screen_1_buttonPress(char buttonChar) {
- // All the buttons that do anything during screen #1 are defined in here.
- if (buttonChar == 'A') {
- menu_page = 0;
- draw_screen(menu_page);
- }
- else if (buttonChar == 'B') {
- menu_page = 2;
- draw_screen(menu_page);
- }
- else {
- GetNumber(buttonChar);
- }
- }
- void screen_2_buttonPress(char buttonChar) {
- // All the buttons that do anything during screen #2 are defined in here.
- if (buttonChar == 'A') {
- menu_page = 1;
- draw_screen(menu_page);
- }
- if (buttonChar == 'B') {
- menu_page = 3;
- draw_screen(menu_page);
- }
- else {
- GetNumber(buttonChar);
- }
- }
- void screen_3_buttonPress(char buttonChar) {
- // All the buttons that do anything during screen #3 are defined in here.
- if (buttonChar == 'A') {
- menu_page = 2;
- draw_screen(menu_page);
- }
- if (buttonChar == 'B') {
- menu_page = 4;
- draw_screen(menu_page);
- }
- }
- void screen_4_buttonPress(char buttonChar) {
- // All the buttons that do anything during screen #4 are defined in here.
- // Button "A" is assigned a function to go back to screen-3
- if (buttonChar == 'A') {
- menu_page = 3;
- draw_screen(menu_page);
- }
- // Button "B" has no function because this is the last screen.
- }
- void draw_screen(int screenPage) {
- switch (screenPage) {
- case 0:
- clear_lcd_display();
- draw_splash_screen();
- break;
- case 1:
- clear_lcd_display();
- draw_screen1();
- break;
- case 2:
- clear_lcd_display();
- draw_screen2();
- break;
- case 3:
- clear_lcd_display();
- draw_screen3();
- break;
- case 4:
- clear_lcd_display();
- draw_screen4();
- break;
- default:
- // statements
- break;
- }
- input_number = 0;
- }
- void draw_splash_screen() {
- lcd.setCursor(2, 0); //col,row
- lcd.print("splash screen #0");
- lcd.setCursor(1, 1);
- lcd.print("splishy splashy");
- lcd.setCursor(0, 3);
- lcd.print("rub a dub dub");
- }
- void draw_screen1() {
- lcd.setCursor(1, 0); //col,row
- lcd.print("screen 1");
- lcd.setCursor(1, 1);
- lcd.print("screen 1 stuff");
- lcd.setCursor(1, 2);
- lcd.print("input = ?");
- lcd.setCursor(1, 3);
- lcd.print("total = 0");
- }
- void draw_screen2() {
- lcd.setCursor(1, 0); //col,row
- lcd.print("screen 2");
- lcd.setCursor(1, 1);
- lcd.print("screen 2 stuff");
- lcd.setCursor(1, 2);
- lcd.print("input = ?");
- lcd.setCursor(1, 3);
- lcd.print("total = 0");
- }
- void draw_screen3() {
- lcd.setCursor(1, 0); //col,row
- lcd.print("screen 3");
- lcd.setCursor(1, 1);
- lcd.print("screen 3 stuff");
- }
- void draw_screen4() {
- lcd.setCursor(1, 0); //col,row
- lcd.print("screen 4");
- lcd.setCursor(1, 1);
- lcd.print("screen 4 stuff");
- }
- void start_buttonDebounce() {
- button_previous_time = millis();
- buttons_enabled = false;
- }
- void clear_lcd_display() {
- // Note: there is an lcd.clear() command in the Stanley-Brevig library that clears all four lines at once,
- // but you may not want to clear all four lines
- clear_lcd_line1();
- clear_lcd_line2();
- clear_lcd_line3();
- clear_lcd_line4();
- }
- void clear_lcd_line1() {
- lcd.setCursor(0, 0); //col,row
- lcd.print(" "); // print 20 spaces
- }
- void clear_lcd_line2() {
- lcd.setCursor(0, 1); //col,row
- lcd.print(" "); // print 20 spaces
- }
- void clear_lcd_line3() {
- lcd.setCursor(0, 2); //col,row
- lcd.print(" "); // print 20 spaces
- }
- void clear_lcd_line4() {
- lcd.setCursor(0, 3); //col,row
- lcd.print(" "); // print 20 spaces
- }
- void GetNumber(char key) {
- // This function is set to only recognize number keys and the "*" key, which resets the value to zero.
- //long num = 0; ---------------------------- not using this
- // char key = kpd.getKey(); ---------------- not using this either, since we are passing the char value in
- // while (key != '#') { -------------------- not using this either, we can just ignore it here
- switch (key) {
- case NO_KEY:
- break;
- case '0': case '1': case '2': case '3': case '4':
- case '5': case '6': case '7': case '8': case '9':
- lcd.setCursor(1, 2);
- lcd.print("input = ");
- lcd.print(key);
- screen_input_numbers[menu_page] = screen_input_numbers[menu_page] * 10 + (key - '0');
- lcd.setCursor(1, 3);
- lcd.print("total = ");
- lcd.print(screen_input_numbers[menu_page]);
- break;
- case '*':
- screen_input_numbers[menu_page] = 0;
- lcd.setCursor(1, 3);
- lcd.print("total = ");
- lcd.print(screen_input_numbers[menu_page]);
- break;
- }
- // key = kpd.getKey(); ------ not using this
- //}
- //return num; -- no return value used
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement