i2c light sensor , problem to integrate in code.

I have a gardening project where i have some problem with one of my sensors.
its the BH 1750 i2c sensor.

i managed to rewrite a code where it displayed value on serial window , to display on a tft .
so long its all ok. but when trying to integrate in my other code with other sensors already in work.
i just get problems whereever i put the code ,

either it wont verify, or when i put it in beginning of code it verifies , but the sensor value does not print to tft
in the 5 sec time as all other values ,

instead it just flickers for a fraction of a sec when the other values update every s 5 sec.

this is the code without bh1750 loop commands

#define cs   10
#define dc   9
#define rst  8
#define DHT11PIN 2
#define ALTITUDE 20.0 

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <SPI.h>
#include <dht11.h>
#include <SFE_BMP180.h>
#include <Wire.h>
#include <math.h>

dht11 DHT11;
SFE_BMP180 pressure;
byte buff[2];

int BH1750address = 0x23;


Adafruit_ST7735 tft = Adafruit_ST7735(cs, dc, rst);

void setup() {
  
  Wire.begin();
  
  tft.initR(INITR_REDTAB);
  tft.fillScreen(ST7735_BLACK);
  tft.setRotation(tft.getRotation()+1);
  tft.print("Garden controller");
  
  
  // barometer
  tft.setCursor(0,24);
  
  if (pressure.begin())
    tft.print("Bmp180 init success*");
  else
  {
    // Oops, something went wrong, this is usually a connection problem,
    // see the comments at the top of this sketch for the proper connections.

    tft.print("BMP180 init fail\n\n");
    
  }
  delay(4000);
}
  
  
  


void loop() {
  tft.fillScreen(ST7735_BLACK);
  
   //  lux
   



  // temp/hum

  tft.setCursor(2,2);
  tft.setTextColor(ST7735_WHITE);
  tft.print("Temp");
  tft.setTextColor(ST7735_WHITE);
  tft.setCursor(2, 12);
  tft.print("Humidity");
  int chk = DHT11.read(DHT11PIN);
  tft.setTextColor(ST7735_WHITE);
  tft.setCursor(135,2);
  tft.print(DHT11.temperature);
  tft.setTextColor(ST7735_WHITE);
  tft.setCursor(135, 12);
  tft.print(DHT11.humidity);
  tft.setCursor(150,2);
  tft.print("*");
  tft.setCursor(150,12);
  tft.print("%");
  
  
  // barometer
  
   char status;
  double T,P,p0,a;
  
status = pressure.startTemperature();
  if (status != 0)
  {
    // Wait for the measurement to complete:
    delay(status);  

    status = pressure.getTemperature(T);
    if (status != 0)
    {
      // Print out the measurement:
      tft.setCursor(2,22);
      tft.print("Temp(bmp)            ");
            
      tft.print(T,1);
      tft.println("*");
      
       status = pressure.startPressure(3);
      if (status != 0)
      {
        // Wait for the measurement to complete:
        delay(status);

        status = pressure.getPressure(P,T);
        
        
        {  // Print out the measurement:
          tft.setCursor(2,32);
          tft.print("Pressure           ");
          tft.print(P,1);
          tft.print("mb");
          tft.setCursor(0,42);
          tft.print("soil moisture" );
          tft.setCursor(138,42);
          tft.print(analogRead(0)/10.3);
 
          
          //
 
          
          
      delay(5000); 
      
 
          
     
      } 
       
      }
      
    }
  }
}

and this is when integrated in beginning and it does verify ok , but value just flickers

#define cs   10
#define dc   9
#define rst  8
#define DHT11PIN 2
#define ALTITUDE 20.0 

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <SPI.h>
#include <dht11.h>
#include <SFE_BMP180.h>
#include <Wire.h>
#include <math.h>

dht11 DHT11;
SFE_BMP180 pressure;
byte buff[2];

int BH1750address = 0x23;


Adafruit_ST7735 tft = Adafruit_ST7735(cs, dc, rst);

void setup() {
  
  Wire.begin();
  
  tft.initR(INITR_REDTAB);
  tft.fillScreen(ST7735_BLACK);
  tft.setRotation(tft.getRotation()+1);
  tft.print("Garden controller");
  
  
  // barometer
  tft.setCursor(0,24);
  
  if (pressure.begin())
    tft.print("Bmp180 init success*");
  else
  {
    // Oops, something went wrong, this is usually a connection problem,
    // see the comments at the top of this sketch for the proper connections.

    tft.print("BMP180 init fail\n\n");
    
  }
  delay(4000);
}
  
  
  


void loop() {
  tft.fillScreen(ST7735_BLACK);
  
   //  lux
   
   int i;
 uint16_t val=0;
 BH1750_Init(BH1750address);
 delay(1800);
 if(2==BH1750_Read(BH1750address))
  {
   val=((buff[0]<<8)|buff[1])/1.2;
   
    tft.fillScreen(ST7735_BLACK);
  tft.setCursor(2, 2);
  tft.setTextColor(ST7735_WHITE);
  tft.print("lux                  ");
   tft.print(val);
  
  }
 
}

int BH1750_Read(int address) //
{
  int i=0;
  Wire.beginTransmission(address);
  Wire.requestFrom(address, 2);
  while(Wire.available()) //
  {
    buff[i] = Wire.read();  // receive one byte
    i++;
  }
  Wire.endTransmission();
  return i;
}

void BH1750_Init(int address)
{
  Wire.beginTransmission(address);
  Wire.write(0x10);//1lx reolution 120ms
  Wire.endTransmission();
   



  // temp/hum

  tft.setCursor(2,2);
  tft.setTextColor(ST7735_WHITE);
  tft.print("Temp");
  tft.setTextColor(ST7735_WHITE);
  tft.setCursor(2, 12);
  tft.print("Humidity");
  int chk = DHT11.read(DHT11PIN);
  tft.setTextColor(ST7735_WHITE);
  tft.setCursor(135,2);
  tft.print(DHT11.temperature);
  tft.setTextColor(ST7735_WHITE);
  tft.setCursor(135, 12);
  tft.print(DHT11.humidity);
  tft.setCursor(150,2);
  tft.print("*");
  tft.setCursor(150,12);
  tft.print("%");
  
  
  // barometer
  
   char status;
  double T,P,p0,a;
  
status = pressure.startTemperature();
  if (status != 0)
  {
    // Wait for the measurement to complete:
    delay(status);  

    status = pressure.getTemperature(T);
    if (status != 0)
    {
      // Print out the measurement:
      tft.setCursor(2,22);
      tft.print("Temp(bmp)            ");
            
      tft.print(T,1);
      tft.println("*");
      
       status = pressure.startPressure(3);
      if (status != 0)
      {
        // Wait for the measurement to complete:
        delay(status);

        status = pressure.getPressure(P,T);
        
        
        {  // Print out the measurement:
          tft.setCursor(2,32);
          tft.print("Pressure           ");
          tft.print(P,1);
          tft.print("mb");
          tft.setCursor(0,42);
          tft.print("soil moisture" );
          tft.setCursor(138,42);
          tft.print(analogRead(0)/10.3);
 
          
          //
 
          
          
      delay(5000); 
      
 
          
     
      } 
       
      }
      
    }
  }
}

and where ever else i put the bh1750 loop commands i just get error codes.

this is one example of error code

tft_gardening_proj_7735.ino: In function 'void loop()':
tft_gardening_proj_7735:127: error: 'BH1750_Init' was not declared in this scope
tft_gardening_proj_7735:129: error: 'BH1750_Read' was not declared in this scope
tft_gardening_proj_7735.ino:125: warning: unused variable 'i'
tft_gardening_proj_7735:144: error: a function-definition is not allowed here before '{' token
tft_gardening_proj_7735:179: error: expected `}' at end of input
tft_gardening_proj_7735:179: error: expected `}' at end of input
tft_gardening_proj_7735:179: error: expected `}' at end of input
tft_gardening_proj_7735.ino:72: warning: unused variable 'chk'
tft_gardening_proj_7735.ino:88: warning: unused variable 'p0'
tft_gardening_proj_7735.ino:88: warning: unused variable 'a'
tft_gardening_proj_7735:179: error: expected `}' at end of input
C:\Users\Krister\Documents\Arduino\libraries\DHT11_X/Versalino.h: At global scope:
C:\Users\Krister\Documents\Arduino\libraries\DHT11_X/Versalino.h:76: warning: 'BUSA' defined but not used
C:\Users\Krister\Documents\Arduino\libraries\DHT11_X/Versalino.h:81: warning: 'BUSB' defined but not used

If anyone could see the problem i would be very glad , cause i have tried every possible way i know to get the code working
by moving around and splitting up the bh1750 code , without succes.

Posting errors without posting the code that causes them wastes everybody's time.

It seems unlikely that you need to call BH1750_Init() on every pass through loop.
It seems equally unlikely that delay() belongs anywhere in your code.
It seems unlikely that you need to blank the screen twice before writing to it. It seems obvious, in fact, that blanking the screen on every pass through loop is what causes the text that was displayed to disappear right away.

Yes i should have posted the code that caused the error,

Here is one example that gives error message, its how i actually would like to have the code working ,
with the bh1750 at the end of code.

the error code is the same as in first post above.

#define cs   10
#define dc   9
#define rst  8
#define DHT11PIN 2
#define ALTITUDE 20.0 

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <SPI.h>
#include <dht11.h>
#include <SFE_BMP180.h>
#include <Wire.h>
#include <math.h>

dht11 DHT11;
SFE_BMP180 pressure;
byte buff[2];

int BH1750address = 0x23;


Adafruit_ST7735 tft = Adafruit_ST7735(cs, dc, rst);

void setup() {
  
  Wire.begin();
  
  tft.initR(INITR_REDTAB);
  tft.fillScreen(ST7735_BLACK);
  tft.setRotation(tft.getRotation()+1);
  tft.print("Garden controller");
  
  
  // barometer
  tft.setCursor(0,24);
  
  if (pressure.begin())
    tft.print("Bmp180 init success*");
  else
  {
    // Oops, something went wrong, this is usually a connection problem,
    // see the comments at the top of this sketch for the proper connections.

    tft.print("BMP180 init fail\n\n");
    
  }
  delay(4000);
}
  
  
  


void loop() {
  tft.fillScreen(ST7735_BLACK);

  // temp/hum

  tft.setCursor(2,2);
  tft.setTextColor(ST7735_WHITE);
  tft.print("Temp");
  tft.setTextColor(ST7735_WHITE);
  tft.setCursor(2, 12);
  tft.print("Humidity");
  int chk = DHT11.read(DHT11PIN);
  tft.setTextColor(ST7735_WHITE);
  tft.setCursor(135,2);
  tft.print(DHT11.temperature);
  tft.setTextColor(ST7735_WHITE);
  tft.setCursor(135, 12);
  tft.print(DHT11.humidity);
  tft.setCursor(150,2);
  tft.print("*");
  tft.setCursor(150,12);
  tft.print("%");
  
  
  // barometer
  
   char status;
  double T,P,p0,a;
  
status = pressure.startTemperature();
  if (status != 0)
  {
    // Wait for the measurement to complete:
    delay(status);  

    status = pressure.getTemperature(T);
    if (status != 0)
    {
      // Print out the measurement:
      tft.setCursor(2,22);
      tft.print("Temp(bmp)            ");
            
      tft.print(T,1);
      tft.println("*");
      
       status = pressure.startPressure(3);
      if (status != 0)
      {
        // Wait for the measurement to complete:
        delay(status);

        status = pressure.getPressure(P,T);
        
        
        {  // Print out the measurement:
          tft.setCursor(2,32);
          tft.print("Pressure           ");
          tft.print(P,1);
          tft.print("mb");
          tft.setCursor(0,42);
          tft.print("soil moisture" );
          tft.setCursor(142,42);
          tft.print(analogRead(0)/10.3 ,0);
          
          // lux
          
  int i;
 uint16_t val=0;
 BH1750_Init(BH1750address);
 delay(1800);
 if(2==BH1750_Read(BH1750address))
  {
   val=((buff[0]<<8)|buff[1])/1.2;
   
    
  tft.setCursor(2, 2);
  tft.setTextColor(ST7735_WHITE);
  tft.print("lux                  ");
   tft.print(val);
  
  }
 
}

int BH1750_Read(int address) //
{
  int i=0;
  Wire.beginTransmission(address);
  Wire.requestFrom(address, 2);
  while(Wire.available()) //
  {
    buff[i] = Wire.read();  // receive one byte
    i++;
  }
  Wire.endTransmission();
  return i;
}

void BH1750_Init(int address)
{
  Wire.beginTransmission(address);
  Wire.write(0x10);//1lx reolution 120ms
  Wire.endTransmission();
  
 
          
          //
 
          
          
      delay(5000); 
      
 
          
     
      } 
       
      }
      
    }
  }
}

I found the extra fill screen that you pointed out, and i erased that,
but still this part of code just flickers when i put it in beginning of code

 tft.setCursor(2, 42);
  tft.setTextColor(ST7735_WHITE);
  tft.print("lux                  ");
 tft.print(val);

i did not find the delay();

and the BH1750_Init() you mention , would that be placed in the setup , do you mean?

edit: did you mean that there should not be any delays at all in the code?

It would certainly help if you used Tools + Auto Format to properly format your code. It would help, too, if you consistently put the { on a new line. It would not hurt to remove the random blank lines from your program.

When I did all that, I found this:

        int i;
        uint16_t val=0;
        BH1750_Init(BH1750address);
        delay(1800);
        if(2==BH1750_Read(BH1750address))
        {
          val=((buff[0]<<8)|buff[1])/1.2;

          tft.setCursor(2, 2);
          tft.setTextColor(ST7735_WHITE);
          tft.print("lux                  ");
          tft.print(val);
        }

        int BH1750_Read(int address) //
        {
          int i=0;
          Wire.beginTransmission(address);
          Wire.requestFrom(address, 2);
          while(Wire.available()) //
          {
            buff[i] = Wire.read();  // receive one byte
            i++;
          }
          Wire.endTransmission();
          return i;
        }

        void BH1750_Init(int address)
        {
          Wire.beginTransmission(address);
          Wire.write(0x10);//1lx reolution 120ms
          Wire.endTransmission();
          delay(5000); 
        }

Surely you know that you can't, in C or C++, define a function inside another function (loop()).

As I pointed out earlier, there is no reason to call BH1750_Init() in loop. It should be called once in setup().

when i move the "BH1750_Init(BH1750address);" to setup.

the tft displays something like the opposit of the original problem.

it flickers the" lux + value" every 1800ms .

and all the other sensor values dissapear from screen.

if i remove the 1800ms delay , it flickers even faster at a rate i dont know what is determin.

#define cs   10
#define dc   9
#define rst  8
#define DHT11PIN 2
#define ALTITUDE 20.0 

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <SPI.h>
#include <dht11.h>
#include <SFE_BMP180.h>
#include <Wire.h>
#include <math.h>

dht11 DHT11;
SFE_BMP180 pressure;
byte buff[2];

int BH1750address = 0x23;
int i;


Adafruit_ST7735 tft = Adafruit_ST7735(cs, dc, rst);

void setup() {

  Wire.begin();

  tft.initR(INITR_REDTAB);
  tft.fillScreen(ST7735_BLACK);
  tft.setRotation(tft.getRotation()+1);
  tft.print("Garden control v1"); 

  // barometer 

  tft.setCursor(0,24);

  if (pressure.begin())
    tft.print("Bmp180 init success*");
  else
  {

    tft.print("BMP180 init fail\n\n");
    BH1750_Init(BH1750address);

  }
  delay(4000);
}  


void loop() {
  tft.fillScreen(ST7735_BLACK);

  //  lux


  uint16_t val=0;

  delay(1800);
  if(2==BH1750_Read(BH1750address))
  {
    val=((buff[0]<<8)|buff[1])/1.2;

    tft.setCursor(2, 52);
    tft.setTextColor(ST7735_WHITE);
    tft.print("lux                  ");
    tft.print(val);
  }

}

int BH1750_Read(int address) //
{
  int i=0;
  Wire.beginTransmission(address);
  Wire.requestFrom(address, 2);
  while(Wire.available()) //
  {
    buff[i] = Wire.read();  // receive one byte
    i++;
  }
  Wire.endTransmission();
  return i;
}

void BH1750_Init(int address)
{
  Wire.beginTransmission(address);
  Wire.write(0x10);//1lx reolution 120ms
  Wire.endTransmission();

  // temp/hum

  tft.setCursor(2,2);
  tft.setTextColor(ST7735_WHITE);
  tft.print("Temp");
  tft.setTextColor(ST7735_WHITE);
  tft.setCursor(2, 12);
  tft.print("Humidity");
  int chk = DHT11.read(DHT11PIN);
  tft.setTextColor(ST7735_WHITE);
  tft.setCursor(135,2);
  tft.print(DHT11.temperature);
  tft.setTextColor(ST7735_WHITE);
  tft.setCursor(135, 12);
  tft.print(DHT11.humidity);
  tft.setCursor(150,2);
  tft.print("*");
  tft.setCursor(150,12);
  tft.print("%");


  // barometer

  char status;
  double T,P,p0,a;

  status = pressure.startTemperature();
  if (status != 0)
  {
    // Wait for the measurement to complete:
    delay(status);  

    status = pressure.getTemperature(T);
    if (status != 0)
    {
      // Print out the measurement:
      tft.setCursor(2,22);
      tft.print("Temp(bmp)            ");

      tft.print(T,1);
      tft.println("*");

      status = pressure.startPressure(3);
      if (status != 0)
      {
        // Wait for the measurement to complete:
        delay(status);

        status = pressure.getPressure(P,T);


        {  // Print out the measurement:
          tft.setCursor(2,32);
          tft.print("Pressure           ");
          tft.print(P,1);
          tft.print("mb");
          tft.setCursor(2,42);
          tft.print("soil moisture" );
          tft.setCursor(146,42);
          tft.print(analogRead(0)/10.3,0);        

          delay(5000);          

        }       
      }      
    }
  }
}

however , there must be something in this part of code that disturbs the chronologic order

.

 uint16_t val=0;

  delay(1800);
  if(2==BH1750_Read(BH1750address))
  {
    val=((buff[0]<<8)|buff[1])/1.2;

    tft.setCursor(2, 52);
    tft.setTextColor(ST7735_WHITE);
    tft.print("lux                  ");
    tft.print(val);
  }

}

int BH1750_Read(int address) //
{
  int i=0;
  Wire.beginTransmission(address);
  Wire.requestFrom(address, 2);
  while(Wire.available()) //
  {
    buff[i] = Wire.read();  // receive one byte
    i++;
  }
  Wire.endTransmission();
  return i;
}

void BH1750_Init(int address)
{
  Wire.beginTransmission(address);
  Wire.write(0x10);//1lx reolution 120ms
  Wire.endTransmission();

i've tried to remove all commands part by part to see if anything does any difference , but mostly whatever i do , i only get error codes.

as a newbie , when i dont understand what each command actually does , it makes it harder to understand the construction.

If i try another solution, dont know if thats what you suggested , to remove all delays
i end eventually end up with blocks with pixels when all different digits just fill up the block.

see pic.

i end eventually end up with blocks with pixels when all different digits just fill up the block.

You appear to not be erasing the screen anymore. The new text overwrites the old text, but does not erase it.

PaulS:

i end eventually end up with blocks with pixels when all different digits just fill up the block.

You appear to not be erasing the screen anymore. The new text overwrites the old text, but does not erase it.

Yes, I did understand that was the cause.

But would like to know if there was any command that keeps the values to automaticly erase it's previous digits
when they are refreshed in each loop ?

As it works in the "utft" library ,
when i have a similar setup, and use the " myGLCD.printNumI "
the values updates and in some magic way , and erase "old" pixels automaticly without having the screen flicker at every loop.

Is that due to differences in library programming?

However , i have solved the org. problem by moving the " fillscreen" part from first line in code , to last line!

so now the " lux part of the code is in sync with other values, dont know how that did a made difference.

if fillscreen had been placed in the middle , i would have guessed that it could have made a difference, but first or last in code ,
is more difficult to understand