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.