0

I have a project split into several files:

//Loop.cpp
#include "LiquidCrystal.h"

void setup(){
    LiquidCrystal lcd(12, 11, 5, 4, 3, 2);  
    lcd.begin(16, 2);  
}

void loop() {
//Code
}
//Display.cpp
#include "LiquidCrystal.h"

namespace display {
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void displayData(const InputPacket &inputPacket, const DataPacket &dataPacket) {
    lcd.setCursor(0,1);
    lcd.print("Foo");    //This creates display error, unless i call lcd.begin(16, 2) in this function
}

} //namespace display

My initial problem is that - without calling lcd.begin() from within displayData() - the characters aren't displayed correctly. In other words, calling lcd.begin() in setup() doesn't help. (It appears to me that setup() has implicit static property, no?)

As an attempted solution, i could create an lcd object in Display.cpp, and call lcd.begin() from within displayData(). Even though characters are displayed properly, this gives me display flickering. (Presumably as a result of calling these functions repeatedly in the loop during runtime).

So, the only solution to this dilemma that i can come up with is to call lcd.begin() from a function written to run only once inside of displayData(). Or is it?

My question is, are there other more elegant solutions/alternatives?

2

The object called lcd that you create inside the setup function is a local variable. It only lives as long as that function is executing - so not very long. It doesn't exist outside that function.

The lcd object you have in namespace display on the other hand is a global variable. That one lives as long as your program, which means until you reset your Arduino here. (It is completely unrelated to the object inside setup.)

If you want all your LCD code to be grouped inside the display namespace, you could do this in Display.cpp:

namespace display {
  // This is a global variable, full name `display::lcd`
  LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

  void setup() {
    // this `lcd` refers to the global `display::lcd` defined above
    lcd.begin(16, 2);
  }

  // rest of the display functions
}

Create a Display.h file to provide prototypes for the display module:

#ifndef DISPLAY_H
#define DISPLAY_H

// Include the header that defines InputPacket and DataPacket here

namespace display
{
void setup();
void displayData(const InputPacket &inputPacket, const DataPacket &dataPacket);
}

#endif

Then your main file can call the display function. The main file doesn't even have to know that you're using an LCD if you do package all LCD code in the display file.

#include "Display.h" // no need to include LiquidCrystal.h if all
                     // LCD handling is done in Display.cpp

void setup(){
  // Initialize display
  display::setup();
}
1
  • Awesome, thanks. Your reply gave me a lot of interesting things to consider! – Erik Apr 13 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.