Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to modify on the menu code (source can be found in this link Arduino/libraries/U8glib/examples/Menu/Menu.ino · 891506b4b1ac0364027c6f3443dd19b17f6cff48 · machinery / MarlinKimbra · GitLab[^]) to design a main menu so I can scroll between them by a keypad
What happened after I uploaded the code was the following:
1- I can scroll only between 2 lines.
2- At pressing D, I can scroll to another 2 lines.

What is the problem in this modification?

What I have tried:

C++
#include <string.h>
#include<accelstepper.h>
#include <keypad.h>
#include "U8glib.h" 

U8GLIB_ST7920_128X64_1X u8g(12, 11, 10, 13);

//Keypad setup
const byte ROWS = 4;
const byte COLS = 4;
char hexaKeys [ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins [ROWS] = {5, 4, 3, 2}; 
byte colPins [COLS] = {9, 8, 7, 6};
Keypad customKeypad = Keypad (makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

#define KEY_NONE 0
#define KEY_PREV 1
#define KEY_NEXT 2
#define KEY_SELECT 3 
#define KEY_BACK 4 

uint8_t keycode1 = KEY_NONE;
uint8_t keycode2 = KEY_NONE;
uint8_t keycode = KEY_NONE;

void keymode(void){
  keycode2 = keycode1;
  char keypressed = customKeypad.getKey();
if (keypressed != NO_KEY){
  switch (keypressed){     
 case 'A':
  keycode1 = KEY_PREV;
  Serial.println("A");
  break;
case'B':
  keycode1 = KEY_NEXT;
    Serial.println("B");
  break;
case 'C':
  keycode1 = KEY_SELECT;
    Serial.println("C");
  break;
case 'D':
  keycode1 = KEY_BACK;
    Serial.println("D");
  break;
  }
  if (keypressed != 'A' && keypressed != 'B' && keypressed !='C' && keypressed != 'D') 
  keycode1 = KEY_NONE;
}
 if (keycode2 == keycode1) 
 keycode = keycode1;
 else 
 keycode = KEY_NONE;
}

 #define MENU_ITEMS 4
 const char *menu_strings[MENU_ITEMS] = {"Stepper Speed", "DC Speed", "Distance", "Done"};

 uint8_t menu_current = 0;
 uint8_t menu_redraw = 0;
 uint8_t Last_key_code = KEY_NONE;

 void drawmenu(void){
  uint8_t i, h;
  u8g_uint_t w, d;
  u8g.setFont(u8g_font_6x13);
  u8g.setFontRefHeightText();
  u8g.setFontPosTop();
  
  h = u8g.getFontAscent()-u8g.getFontDescent();
  w = u8g.getWidth();
  for( i = 0; i < MENU_ITEMS; i++ ) {
    d = (w-u8g.getStrWidth(menu_strings[i]))/2;
    u8g.setDefaultForegroundColor();
    if ( i == menu_current ) {
      u8g.drawBox(0, i*h+1, w, h);
      u8g.setDefaultBackgroundColor();
    }
    u8g.drawStr(d, i*h, menu_strings[i]);
  }
 }

 void updatemenu(void){ 
  if (keycode != KEY_NONE && Last_key_code == keycode) { 
    return;
  }
  Last_key_code = keycode;

  switch(keycode){
    case KEY_NEXT:
    menu_current ++;
    if (menu_current >= MENU_ITEMS) 
    menu_current =0;
    menu_redraw = 1;
    
    break;

    case KEY_PREV: 
    if (menu_current ==0) 
    menu_current = MENU_ITEMS;
    menu_current --;
    menu_redraw = 1;
    
    break;
  }
 }

 void setup (){
  Serial.begin(9600);
  menu_redraw = 1;
 }

 void loop (){
  keymode();
  if (menu_redraw !=0) {
    u8g.firstPage();
    do{
      drawmenu();
    }while(u8g.nextPage());
    menu_redraw = 0;
  }
   updatemenu();
 }
Posted
Updated 17-Jun-19 13:54pm
v2
Comments
Mike Hankey 2-Aug-19 12:32pm    
avrfreaks.net is the place for help with AVR devices.

1 solution

Quote:
I am trying to modify on the menu code

Why don't you ask a forum of Arduino users ?
They are certainly more familiar with the code than us.
-----
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900