I am attempting to build Arduino examples for LCD GitHub - Freenove/Freenove_LCD_Module: Apply to FNK0079
I am using VS code and Platform IO and I get this error.
src\led-demo.cpp:15:24: error: ‘i2CAddrTest’ was not declared in this scope
I suspect this is in a library not normally in PlatformIO.
do you have any suggestions for which library to use?
Thank you
No, the error is due to the fact that *.ino files are not valid C++ and simply renaming the file from *.ino to *.cpp is not sufficient.
For a correct conversion to C++ please see Convert Arduino file to C++ manually — PlatformIO latest documentation
This file is not included in the mentioned repository. I therefore assume that this file is meant:
Sketch_1.1_Display_the_string_on_LCD1602.ino
:
#include <LiquidCrystal_I2C.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
if (!i2CAddrTest(0x27)) {
lcd = LiquidCrystal_I2C(0x3F, 16, 2);
}
lcd.init(); // initialize the lcd
lcd.backlight(); // Turn on backlight
lcd.print("hello, world!");// Print a message to the LCD
}
void loop() {
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);// set the cursor to column 0, line 1
// print the number of seconds since reset:
lcd.print("Counter:");
lcd.print(millis() / 1000);
}
bool i2CAddrTest(uint8_t addr) {
Wire.begin();
Wire.beginTransmission(addr);
if (Wire.endTransmission() == 0) {
return true;
}
return false;
}
The function i2CAddrTest
is defined after the setup
function and is therefore ‘not visible’ for the setup
function.
A valid conversion to C++ would look like this:
Sketch_1.1_Display_the_string_on_LCD1602.cpp
:
#include <Arduino.h>
#include <LiquidCrystal_I2C.h>
// forward declaration of i2CAddrTest function
bool i2CAddrTest(uint8_t addr);
// initialize the library with the numbers of the interface pins
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
if (!i2CAddrTest(0x27)) {
lcd = LiquidCrystal_I2C(0x3F, 16, 2);
}
lcd.init(); // initialize the lcd
lcd.backlight(); // Turn on backlight
lcd.print("hello, world!");// Print a message to the LCD
}
void loop() {
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);// set the cursor to column 0, line 1
// print the number of seconds since reset:
lcd.print("Counter:");
lcd.print(millis() / 1000);
}
bool i2CAddrTest(uint8_t addr) {
Wire.begin();
Wire.beginTransmission(addr);
if (Wire.endTransmission() == 0) {
return true;
}
return false;
}
Thank you for the thorough explanation. I should have noticed that. Lesson learned.