LCD menu - Programming questions

EL Wire/Tape/Panels, LEDs, pixels and strips, LCDs and TFTs, etc products from Adafruit

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
matthieu.stb
 
Posts: 17
Joined: Mon Sep 09, 2013 9:00 am

LCD menu - Programming questions

Post by matthieu.stb »

Hi,

I'm trying to build a menu with an Arduino Uno and the (great!) Adafruit RGB backlight LCD 2x16. To do so, I used the corresponding library Adafruit_RGBLCDShield. Unfortunately the detection of pressed buttons is not configured on falling edge and one pressure makes several items scrolling instead of only one ! Could you help me finding an issue ?

Here is my code :

Code: Select all

// include the library code:
#include <Wire.h>
#include <Adafruit_MCP23017.h>
#include <Adafruit_RGBLCDShield.h>

Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();

// These #defines make it easy to set the backlight color
#define RED 0x1
#define GREEN 0x2
#define BLUE 0x4
#define WHITE 0x7 //Other colors : YELLOW 0x3|TEAL 0x6|VIOLET 0x5

typedef struct node {
  char *menuString;            // menu title
  struct node *parent;         // the 'parent' element
  struct node *prevSibling;    // the 'previous sibling' element
  struct node *nextSibling;    // the 'next sibling' element
  struct node *firstChild;     // the 'first child' element
} menuItem;

menuItem *menuPtr; // define the pointer through menu selected

//Build nodes
menuItem configuration = {"CONFIGURATION",0,0,0,0};
menuItem essai = {"ESSAI",0,&configuration,0,0};
menuItem resultats = {"RESULTATS",0,&essai,0,0};
menuItem tension_seuil = {"Tension seuil",&configuration,0,0,0};

void setup() {
  Serial.begin(9600);// Debugging output
  lcd.begin(16, 2); // set up the LCD's number of columns and rows: 
  lcd.setBacklight(WHITE); //Set the backlight to white
  
  // Build links between nodes
  configuration.firstChild = &tension_seuil; //  tell configuration menu how to find its first child
  configuration.nextSibling = &essai;  //tell configuration menu how to find its sibling
  essai.nextSibling = &resultats;  //tell essai menu how to find its sibling
  
  menuPtr = &configuration; //Initialize menu pointer
  lcd.print(menuPtr->menuString); //Print it
}

uint8_t i=0;
void loop() {
  lcd.setCursor(0, 0);  // set the cursor to column 0, line 1 (note: line 1 is the second row, since counting begins with 0)
  uint8_t buttons = lcd.readButtons();

  if (buttons) {
    lcd.clear();
    if (buttons & BUTTON_UP) {
      if (menuPtr->prevSibling!=0){
        menuPtr = menuPtr->prevSibling;
      }
    lcd.print(menuPtr->menuString);
    }
    if (buttons & BUTTON_DOWN) {
       if (menuPtr->nextSibling!=0){
        menuPtr = menuPtr->nextSibling;
      }
      lcd.print(menuPtr->menuString);
    }
    if (buttons & BUTTON_LEFT) {
      if (menuPtr->parent!=0){
        menuPtr = menuPtr->parent;
      }
      lcd.print(menuPtr->menuString);
    }
    if (buttons & BUTTON_RIGHT) {
      if (menuPtr->firstChild!=0){
        menuPtr = menuPtr->firstChild;
      }
      lcd.print(menuPtr->menuString);
    }
    if (buttons & BUTTON_SELECT) {
      lcd.print(menuPtr->menuString);
    }
  }
}
Thanks a lot !

Matthieu

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: LCD menu - Programming questions

Post by adafruit_support_rick »

Well, here's a very simple-minded fix. I added a delay after a button press:

Code: Select all

// include the library code:
#include <Wire.h>
#include <Adafruit_MCP23017.h>
#include <Adafruit_RGBLCDShield.h>

Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();

// These #defines make it easy to set the backlight color
#define RED 0x1
#define GREEN 0x2
#define BLUE 0x4
#define WHITE 0x7 //Other colors : YELLOW 0x3|TEAL 0x6|VIOLET 0x5

typedef struct node {
  char *menuString;            // menu title
  struct node *parent;         // the 'parent' element
  struct node *prevSibling;    // the 'previous sibling' element
  struct node *nextSibling;    // the 'next sibling' element
  struct node *firstChild;     // the 'first child' element
} menuItem;

menuItem *menuPtr; // define the pointer through menu selected

//Build nodes
menuItem configuration = {"CONFIGURATION",0,0,0,0};
menuItem essai = {"ESSAI",0,&configuration,0,0};
menuItem resultats = {"RESULTATS",0,&essai,0,0};
menuItem tension_seuil = {"Tension seuil",&configuration,0,0,0};

void setup() {
  Serial.begin(9600);// Debugging output
  lcd.begin(16, 2); // set up the LCD's number of columns and rows: 
  lcd.setBacklight(WHITE); //Set the backlight to white
  
  // Build links between nodes
  configuration.firstChild = &tension_seuil; //  tell configuration menu how to find its first child
  configuration.nextSibling = &essai;  //tell configuration menu how to find its sibling
  essai.nextSibling = &resultats;  //tell essai menu how to find its sibling
  
  menuPtr = &configuration; //Initialize menu pointer
  lcd.print(menuPtr->menuString); //Print it
}

uint8_t i=0;
void loop() {
  lcd.setCursor(0, 0);  // set the cursor to column 0, line 1 (note: line 1 is the second row, since counting begins with 0)
  uint8_t buttons = lcd.readButtons();

  if (buttons) {
    lcd.clear();
    if (buttons & BUTTON_UP) {
      if (menuPtr->prevSibling!=0){
        menuPtr = menuPtr->prevSibling;
      }
    lcd.print(menuPtr->menuString);
    }
    if (buttons & BUTTON_DOWN) {
       if (menuPtr->nextSibling!=0){
        menuPtr = menuPtr->nextSibling;
      }
      lcd.print(menuPtr->menuString);
    }
    if (buttons & BUTTON_LEFT) {
      if (menuPtr->parent!=0){
        menuPtr = menuPtr->parent;
      }
      lcd.print(menuPtr->menuString);
    }
    if (buttons & BUTTON_RIGHT) {
      if (menuPtr->firstChild!=0){
        menuPtr = menuPtr->firstChild;
      }
      lcd.print(menuPtr->menuString);
    }
    if (buttons & BUTTON_SELECT) {
      lcd.print(menuPtr->menuString);
    }
    delay(150);
  }
}
Here is a better fix. It compares the current button state with the previous button state. It only processes buttons if the current state and previous state are different:

Code: Select all

// include the library code:
#include <Wire.h>
#include <Adafruit_MCP23017.h>
#include <Adafruit_RGBLCDShield.h>

Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();

// These #defines make it easy to set the backlight color
#define RED 0x1
#define GREEN 0x2
#define BLUE 0x4
#define WHITE 0x7 //Other colors : YELLOW 0x3|TEAL 0x6|VIOLET 0x5

typedef struct node {
  char *menuString;            // menu title
  struct node *parent;         // the 'parent' element
  struct node *prevSibling;    // the 'previous sibling' element
  struct node *nextSibling;    // the 'next sibling' element
  struct node *firstChild;     // the 'first child' element
} menuItem;

menuItem *menuPtr; // define the pointer through menu selected

//Build nodes
menuItem configuration = {"CONFIGURATION",0,0,0,0};
menuItem essai = {"ESSAI",0,&configuration,0,0};
menuItem resultats = {"RESULTATS",0,&essai,0,0};
menuItem tension_seuil = {"Tension seuil",&configuration,0,0,0};

void setup() {
  Serial.begin(9600);// Debugging output
  lcd.begin(16, 2); // set up the LCD's number of columns and rows: 
  lcd.setBacklight(WHITE); //Set the backlight to white
  
  // Build links between nodes
  configuration.firstChild = &tension_seuil; //  tell configuration menu how to find its first child
  configuration.nextSibling = &essai;  //tell configuration menu how to find its sibling
  essai.nextSibling = &resultats;  //tell essai menu how to find its sibling
  
  menuPtr = &configuration; //Initialize menu pointer
  lcd.print(menuPtr->menuString); //Print it
}

uint8_t i=0;
uint8_t buttons = 0;
uint8_t lastButtons = 0;
void loop() {
  lcd.setCursor(0, 0);  // set the cursor to column 0, line 1 (note: line 1 is the second row, since counting begins with 0)
  while (buttons == lastButtons)
    buttons = lcd.readButtons();

  if (buttons) {
    lcd.clear();
    if (buttons & BUTTON_UP) {
      if (menuPtr->prevSibling!=0){
        menuPtr = menuPtr->prevSibling;
      }
    lcd.print(menuPtr->menuString);
    }
    if (buttons & BUTTON_DOWN) {
       if (menuPtr->nextSibling!=0){
        menuPtr = menuPtr->nextSibling;
      }
      lcd.print(menuPtr->menuString);
    }
    if (buttons & BUTTON_LEFT) {
      if (menuPtr->parent!=0){
        menuPtr = menuPtr->parent;
      }
      lcd.print(menuPtr->menuString);
    }
    if (buttons & BUTTON_RIGHT) {
      if (menuPtr->firstChild!=0){
        menuPtr = menuPtr->firstChild;
      }
      lcd.print(menuPtr->menuString);
    }
    if (buttons & BUTTON_SELECT) {
      lcd.print(menuPtr->menuString);
    }
  }
  lastButtons = buttons;
}

User avatar
matthieu.stb
 
Posts: 17
Joined: Mon Sep 09, 2013 9:00 am

Re: LCD menu - Programming questions

Post by matthieu.stb »

Hi Rick,

Sorry for my late reply... Thnak you very much for your help ! I have implemented your idea and it works like a charm !

Final question: Where is the best place in the forum where I can ask for help concerning Arduino programming ?

Thx

Matt

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: LCD menu - Programming questions

Post by adafruit_support_rick »

Matthieu.stb wrote:Sorry for my late reply... Thnak you very much for your help ! I have implemented your idea and it works like a charm !
Great - Glad to hear it! :D
Matthieu.stb wrote:Final question: Where is the best place in the forum where I can ask for help concerning Arduino programming ?
Best place would be our Arduino forum:
http://forums.adafruit.com/viewforum.php?f=25

Locked
Please be positive and constructive with your questions and comments.

Return to “Glowy things (LCD, LED, TFT, EL) purchased at Adafruit”