[SOLVED]Getting #elif compilation error

I want to make a choice between a 16x2 or a 20x4 LCD Display.
Therefore I wrote this :slight_smile:

#ifndef LCD20
  const uint8_t cols = 16;  // 16 for 1602 display, 20 for 2004 display's
  const uint8_t rows = 2;   // 16 for 1602 display, 20 for 2004 display's
#elif
  const uint8_t cols = 20;  // 16 for 1602 display, 20 for 2004 display's
  const uint8_t rows = 4;
#endif

but I get a compilation error:
Compilation error: #elif with no expression
What expression must #elif have to work??

Compiles OK for me with a Nano classic as the target board

Which Arduino board and IDE version are you using ?

Please post a complete sketch that illustrates the problem and post the full error message produced by that sketch

Given the snippet posted presumably #else is a more reasonable choice.

#elif is short for "else if", so it expects an expression to evaluate for the "if" condition.

Try again with LCD20 defined, if that is not defined the compiler apparently never processes the #elif.

Try lihe this:

#ifndef LCD20
  const uint8_t cols = 16;  // 16 for 1602 display, 20 for 2004 display's
  const uint8_t rows = 2;   // 16 for 1602 display, 20 for 2004 display's
#elif LCDXX
  const uint8_t cols = 20;  // 16 for 1602 display, 20 for 2004 display's
  const uint8_t rows = 4;
#endif

Ref: Preprocessor directives | Microsoft Learn

As has been pointed out, the #elif is wrong and should probably be #else so no surprise that compilation fails under some circumstances

1 Like

So changed the #elif To #else and it compiles correct, full code

#include <Wire.h>
#define LCD20 false        // system uses 20X4 LCD else a 16x2 LCD display
#ifndef LCD20
  const uint8_t cols = 16;  // 16 for 1602 display, 20 for 2004 display's
  const uint8_t rows = 2;   // 16 for 1602 display, 20 for 2004 display's
#else
  const uint8_t cols = 20;  // 16 for 1602 display, 20 for 2004 display's
  const uint8_t rows = 4;
#endif

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  value = millis();
  Serial.println(value);
  Serial.println(rows);
  }
void loop() {}

But now if #define LCD20 false or true in boht cases the value of rows = 4

In both cases of LCD20 being true or false, LCD20 is being defined. Therefore, the #else path is always taken.

Comment out the #define of LCD20, and then the #ifndef path should be taken.

So I have rewritten this section to

#define LCD20 false  // system uses 20X4 LCD else a 16x2 LCD display
#if LCD20
const uint8_t cols = 20;  // 16 for 1602 display, 20 for 2004 display's
const uint8_t rows = 4;
#else
const uint8_t cols = 16;  // 16 for 1602 display, 20 for 2004 display's
const uint8_t rows = 2;   // 16 for 1602 display, 20 for 2004 display's
#endif
LiquidCrystal_I2C lcd(0x27, cols, rows);  // set the LCD address to 0x27 for a 16 chars and 2 line display

Thnx for the input

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.