#include <Wire.h>
#include <BH1750FVI.h>
#include <LiquidCrystal_I2C.h> //https://github.com/enjoyneering/LiquidCrystal_I2C
#define LCD_COLUMNS 20 //LCD columns
#define LCD_ROWS 4 //LCD rows
#define LCD_SPACE_SYMBOL 0x20 //space symbol located in LCD ROM, see p.9 of GDM2004D datasheet
#define LIGHT_LEVEL_OFFICE 200 //recommended office light level 200..500 lux
#define LIGHT_LEVEL_PLANT_GROW 12000 //recommended indoor light level for plants growing 12000..20000 lux
const uint8_t iconInPowerTwo[8] PROGMEM = {0x1C, 0x04, 0x08, 0x10, 0x1C, 0x00, 0x00, 0x00}; //PROGMEM saves variable to flash & keeps dynamic memory free
BH1750FVI myBH1750(BH1750_DEFAULT_I2CADDR, BH1750_CONTINUOUS_HIGH_RES_MODE, BH1750_SENSITIVITY_DEFAULT, BH1750_ACCURACY_DEFAULT);
LiquidCrystal_I2C lcd(PCF8574_ADDR_A21_A11_A01, 4, 5, 6, 16, 11, 12, 13, 14, POSITIVE);
/**************************************************************************/
/*
setup()
Main setup
*/
/**************************************************************************/
void setup()
{
/* Serial initialization */
Serial.begin(115200);
Serial.println();
/* LCD initialization */
while (lcd.begin(LCD_COLUMNS, LCD_ROWS, LCD_5x8DOTS) != true) //colums, rows, characters size
{
Serial.println(F("PCF8574 is not connected or lcd pins declaration is wrong. Only pins numbers: 4,5,6,16,11,12,13,14 are legal."));
delay(5000);
}
/* BH1750 initialization */
while (myBH1750.begin() != true)
{
lcd.setCursor(0, 0);
lcd.print(F("BH1750 error"));
delay(5000);
}
lcd.clear();
lcd.print(F("BH1750 OK"));
delay(2000);
/* load custom symbol to LCD CGRAM */
lcd.createChar(0, iconInPowerTwo);
/* prints static text to LCD */
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(F("Light:"));
lcd.setCursor(0, 1);
lcd.print(F("Power:"));
}
/**************************************************************************/
/*
loop()
Main loop
*/
/**************************************************************************/
void loop()
{
float lightLevel = myBH1750.readLightLevel(); //start measurment -> wait for result -> read result -> retrun result or 4294967295 if communication error is occurred
lcd.setCursor(6, 0); //set 7-th colum & 1-st row
if (lightLevel != BH1750_ERROR) //BH1750_ERROR=4294967295
{
lcd.print(lightLevel, 2);
lcd.print(F("lx"));
lcd.write(LCD_SPACE_SYMBOL); //"lcd.write(0x20)" is faster than "lcd.print(" ")"
lcd.write(LCD_SPACE_SYMBOL);
}
else
{
lcd.print(F("error "));
}
lcd.setCursor(6, 1); //set 7-th colum & 2-nd row
if (lightLevel != BH1750_ERROR) //BH1750_ERROR=4294967295
{
lcd.print(lightLevel / 683); //power for 555nm wave
lcd.print(F("Watt/m"));
lcd.write(0); //print custom char 0="iconInPowerTwo"
lcd.write(LCD_SPACE_SYMBOL);
lcd.write(LCD_SPACE_SYMBOL);
}
else
{
lcd.print(F("error "));
}
if (((LCD_ROWS) == 4) && (lightLevel != BH1750_ERROR))
{
lcd.printHorizontalGraph('W', 2, lightLevel, LIGHT_LEVEL_OFFICE); //graph name, row position, current value, maximum value
lcd.printHorizontalGraph('G', 3, lightLevel, LIGHT_LEVEL_PLANT_GROW); //graph name, row position, current value, maximum value
}
delay(1000);
}Loading
stm32-bluepill
stm32-bluepill