element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Personal Blogs
  • Community Hub
  • More
Personal Blogs
NexGen Flight Simuator Nexgen: Adventures into CANaerospace part 2
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: phoenixcomm
  • Date Created: 14 Jun 2020 4:47 PM Date Created
  • Views 685 views
  • Likes 2 likes
  • Comments 2 comments
  • flight simulator
  • nexgen
  • ifdef
  • diy hardware
  • i2c
  • arduino mega 2560
  • 4x20 lcd module
  • endif
  • canaerospace
  • canbus
Related
Recommended

Nexgen: Adventures into CANaerospace part 2

phoenixcomm
phoenixcomm
14 Jun 2020
image

Oh well, after hooking up my 4x20 LCD (that was held over from the Beagle Bone AI project, that I could never get to work) it still failed on my Arduino Mega. I ordered one, off eBay. The first thing I  did was to run the test program. Vola it worked.

The next piece of business was to get formatted text to the display. Now as we all know Arduino does not support the C library function printf, which will sends formatted text to the console. Using printf is straightforward with these conventions:  int  printf( const *format, ......) here is it used to print the ASCII table

printf("ASCII value = %d, Character =%c\n", ch, ch ); you will note that the return int is optional.

But the Arduino you can't send the message to the LCD display without doing this. My function displayWrite( int row, char[] ) handles writing to either the LCD (during testing) or the LED displays. Furthermore, I can also wrap my library #include statements.

 

sprintf( line, "TANK #%d - %d", tank, lbs );
displayWrite ( 2, 0, line);

~~

void dispalyWrite( int row, int col,  char[] ) {

        lcd.setCursor( col, row );

        lcd.print( line ); }

 

Since I will only be using the LCD for testing and a pair of HDSP-2131 (HDSP-2111) LED Display 5x7 8 characters that are part of the fuel display, and I don't want to rip my code when I change displays. Let me introduce a neat feature of the C preprocessor which are #ifdef, #ifndef, #elseif, #elif, #else, #endif.   I use them so I don't have to rewrite my display function ie. over the years I have gotten into the habit of writing modularized code. The star of the show is #ifdef. This directive works by looking at the term after the directive. If that term has not been #defined it will pass over this code block until an  #else, #endif is encountered.  Here is a nice reference on the #directives.

 

#define displayLCD

#ifdef displayLCD

#include <LiquidCrystal_I2C.h>

#include <Wire.h>

#endif

 

void dispalyWrite( int row, int col, char[] ) {

#ifdef displayLCD

        lcd.setCursor( col, row );

        lcd.print( line );

#else

/// Code to drive the HDSP-2111 w/o shift reg

#endif

} // this closes the function.

  • Sign in to reply

Top Comments

  • DAB
    DAB over 5 years ago +1
    Nice update Chris. DAB
  • phoenixcomm
    phoenixcomm over 5 years ago in reply to DAB

    DAB I was working on the system this was more about the damn LCD than anything else.  The preprocessor directives should have gone into my  'tips" section.

    ~~ Cris

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • DAB
    DAB over 5 years ago

    Nice update Chris.

     

    DAB

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube