0
votes

I'm making a project where you have a led matrix, LCD and 7 different sensors. Each sensor will display a different message to the matrix and has it's own sub menu on the LCD.

I've created a menu for switching between the sensors. A rotary encoder is used to navigate in the menu. (A pin has an interrupt on change for executing only the void loop when the pin has changed) Also a select button is used to run the program of a menu item.( a sensor program like prg_Pong(), prg_Flame());

I want to display the menu items on the LCD (no I2C). Now my problem is my main menu items are displayed correctly (with no flicker because of the interrupt on the rotary encoder pin) but when I press the button to run for ex prg_Pong() the lcd prints message running pong but the message is flickering and the message on the led matrix is good (static).

I've found a solution by adding a delay(300); in the loop after drawMenu but when I do that and enter prg_Pong(); the message on the led matrix starts to flicker at the delay time and the menu item "running pong" doesn't flicker anymore.

The functions displayData(),mtx_clear() are used for the led matrix and they are working. Because when I remove the functionality of the LCD the message is displayed correctly.

void loop() {
  drawMenu(RotaryEncoder());
  delay(300);
}


void drawMenu(byte rotaryPosition) {
  stateBtnSelect = digitalRead(BTN_SELECT);
  if (stateBtnSelect == HIGH && previousBtnState == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;
    time = millis();
  }
  previousBtnState = stateBtnSelect;

  //Switch the value of the rotary encoder
  switch (rotaryPosition) {
    case 0:
      displayMenuItem(0);
      if (state == HIGH) {
        prg_Pong();
      }
      break;
    case 1:
      displayMenuItem(1);
      if (state == HIGH) {
        prg_Ascroll();
      }
      break;
    case 2:
      displayMenuItem(2);
      if (state == HIGH) {
        prg_Clock();
      }
      break;
    case 3:
      displayMenuItem(3);
      if (state == HIGH) {
        prg_Flame();
      }
      break;
    case 4:
      displayMenuItem(4);
      if (state == HIGH) {
        prg_Heartbeat();
      }
      break;
    default:
      break;
  }
}

//Display a menu item
void displayMenuItem(byte item)
{
  //menu strings (mai n categories)
    String menuItems[MAX_MENU_ITEMS] = {"Pong game", "A. Scroller", "Clock", "Flame/temp", "Heartbeat"};
    lcd.clear();
    lcd.setCursor(6, 0);
    lcd.print("Menu");
    lcd.setCursor(0, 1);
    lcd.write(byte(0));
    lcd.print(menuItems[item]);
}


void prg_Pong()
{
  lcd.clear();
  delay(100);
  lcd.print("run pong");
  mtx_clear();

  //print cirlce once
  MessageEncoder(ReturnLetter(24), BitStream);
  for (int j = 0; j < 8; j++)
  {
    displayData(~BitStream[j] & Kolommen[j]);
  }
}
1
1. Don't clear the display if you don't have to, i.e. when nothing changes, don't clear, 2. consider using a kind of frame buffer, i.e. an array or the like in which you maintain and modify the full display contents, then transfer the whole buffer to the display (without clearing) overwriting the previous contents.JimmyB

1 Answers

0
votes

you are clearing the lcd display frequently, so it will look like flickering.

i used a two more variables to find, Is there any need to update the display? change the display contents only if required, I didn't compile code but hope this will solve your problem, i use similar technique in my projects.

prev_menu=-1;
update_display=true;

void loop() {
  drawMenu(RotaryEncoder());
  delay(300);
}


void drawMenu(byte rotaryPosition) {
  stateBtnSelect = digitalRead(BTN_SELECT);
  if (stateBtnSelect == HIGH && previousBtnState == LOW && millis() - time > debounce) {
    if (state == HIGH)
        state = LOW;
    else
      state = HIGH;
    time = millis();
    update_display=true;
  }
  previousBtnState = stateBtnSelect;

    if(prev_menu != rotaryPosition)
            update_display=true;
    else
        update_display=false;

  //Switch the value of the rotary encoder
  switch (rotaryPosition) {
    case 0:
      displayMenuItem(0);
      if (state == HIGH) {
        prg_Pong();
      }
      break;
    case 1:
      displayMenuItem(1);
      if (state == HIGH) {
        prg_Ascroll();
      }
      break;
    case 2:
      displayMenuItem(2);
      if (state == HIGH) {
        prg_Clock();
      }
      break;
    case 3:
      displayMenuItem(3);
      if (state == HIGH) {
        prg_Flame();
      }
      break;
    case 4:
      displayMenuItem(4);
      if (state == HIGH) {
        prg_Heartbeat();
      }
      break;
    default:
      break;
  }
}

//Display a menu item
void displayMenuItem(byte item)
{
  //menu strings (mai n categories)
    if(  update_display){

    String menuItems[MAX_MENU_ITEMS] = {"Pong game", "A. Scroller", "Clock", "Flame/temp", "Heartbeat"};
    lcd.clear();
    lcd.setCursor(6, 0);
    lcd.print("Menu");
    lcd.setCursor(0, 1);
    lcd.write(byte(0));
    lcd.print(menuItems[item]);
    prev_menu=item;

    }
}


void prg_Pong()
{
if(update_display){
  lcd.clear();
  delay(100);
  lcd.print("run pong");
    update_display=false;

  }
  mtx_clear();

  //print cirlce once
  MessageEncoder(ReturnLetter(24), BitStream);
  for (int j = 0; j < 8; j++)
  {
    displayData(~BitStream[j] & Kolommen[j]);
  }
}