|
| 1 | + // this example will just show how to use analog keyboard with the menu |
| 2 | + // depend on https://github.com/neu-rah/ArduinoMenu/issues/17 |
| 3 | + // the used keyboard is: https://www.dfrobot.com/wiki/index.php/Arduino_LCD_KeyPad_Shield_(SKU:_DFR0009) |
| 4 | + // you have to change your analog data according to your keyboadr |
| 5 | + |
| 6 | +#include <menu.h>//menu macros and objects |
| 7 | +#include <genericKeyboard.h> |
| 8 | +#include <chainStream.h> |
| 9 | +#include <LiquidCrystal.h> |
| 10 | +#include <menuLCD.h> |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +#define btnRIGHT menu::enterCode |
| 17 | +#define btnUP menu::downCode |
| 18 | +#define btnDOWN menu::upCode |
| 19 | +#define btnLEFT menu::escCode |
| 20 | +#define btnENTER menu::enterCode |
| 21 | +#define btnNONE -1 |
| 22 | + |
| 23 | +genericKeyboard mykb(read_keyboard); |
| 24 | +//alternative to previous but now we can input from Serial too... |
| 25 | +//Stream* in2[]={&mykb,&Serial}; |
| 26 | +//chainStream<2> allIn(in2); |
| 27 | +Stream* in2[] = { &mykb }; |
| 28 | +chainStream<1> allIn(in2); |
| 29 | + |
| 30 | +//set the pin and the lcd |
| 31 | +LiquidCrystal lcd(8, 9, 4, 5, 6, 7); |
| 32 | +menuLCD menu_lcd(lcd, 16, 2);//menu output device |
| 33 | + |
| 34 | +//this is a simple menu just to test the keyboard check othe eample to extend it. |
| 35 | + |
| 36 | +MENU(mainMenu, "Main menu", |
| 37 | + OP("LED On", menu::nothing), |
| 38 | + OP("LED Off", menu::nothing), |
| 39 | + OP("Empty 1", menu::nothing), |
| 40 | + OP("Empty 2", menu::nothing), |
| 41 | + OP("Empty 3", menu::nothing), |
| 42 | + OP("Empty 4", menu::nothing), |
| 43 | + OP("Empty 5...", menu::nothing) |
| 44 | +); |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +int old_button = 0; |
| 49 | + |
| 50 | +int read_keyboard() { |
| 51 | + int button, button2, pressed_button; |
| 52 | + button = getButton(); |
| 53 | + if (button != old_button) { |
| 54 | + delay(50); // debounce |
| 55 | + button2 = getButton(); |
| 56 | + |
| 57 | + if (button == button2) { |
| 58 | + old_button = button; |
| 59 | + pressed_button = button; |
| 60 | + if (button != 0) { |
| 61 | + //Serial.println(button); |
| 62 | + if (button == 1) return btnLEFT; |
| 63 | + if (button == 2) return btnUP; |
| 64 | + if (button == 3) return btnDOWN; |
| 65 | + if (button == 4) return btnRIGHT; |
| 66 | + if (button == 5) return btnENTER; |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + else { |
| 71 | + return btnNONE; |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +int getButton() { |
| 76 | + int i, z, button; |
| 77 | + int sum = 0; |
| 78 | + |
| 79 | + for (i = 0; i < 4; i++) { |
| 80 | + sum += analogRead(0); |
| 81 | + } |
| 82 | + z = sum / 4; |
| 83 | + // change this numbers according to your keyboard. |
| 84 | + if (z > 1000) button = 0; |
| 85 | + else if (z >= 0 && z < 50) button = 1; //LEFT |
| 86 | + else if (z > 120 && z < 155) button = 2; //UP |
| 87 | + else if (z > 292 && z < 335) button = 3; //DOWN |
| 88 | + else if (z > 460 && z < 515) button = 4; //RIGHT |
| 89 | + else if (z > 650 && z < 745) button = 5; //ENTER |
| 90 | + else button = 0; |
| 91 | + //{ 0, 128, 302, 474, 718 } |
| 92 | + return button; |
| 93 | +} |
| 94 | + |
| 95 | + |
| 96 | +void setup() { |
| 97 | + |
| 98 | +} |
| 99 | + |
| 100 | +void loop() { |
| 101 | + |
| 102 | + mainMenu.poll(menu_lcd, allIn); |
| 103 | + delay(50); |
| 104 | + |
| 105 | +} |
0 commit comments