Hello, Can someone help me why my BH1750 sensor won't work and only show -1.00 whenever i use for statement in my project? I also use RTC DS3231 in this project.
Can someone please help me found the mistake?
#include <Wire.h>
#include <RTClib.h>
#include <BH1750.h>
#include <FastLED.h>
#define LED_PIN D0
#define NUM_LEDS 5
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
BH1750 lightMeter;
CRGB leds[NUM_LEDS];
void setup () {
Serial.begin(9600);
Wire.begin();
rtc.begin();
rtc.adjust(DateTime(__DATE__, __TIME__));
FastLED.addLeds <WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
lightMeter.begin();
}
void loop () {
DateTime now = rtc.now();
float lux = lightMeter.readLightLevel();
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(" - ");
Serial.print(now.day(), DEC); Serial.print('/'); Serial.print(now.month(), DEC); Serial.print('/'); Serial.print(now.year(), DEC);
Serial.print(" - ");
Serial.print(now.hour(), DEC); Serial.print(':'); Serial.print(now.minute(), DEC); Serial.print(':'); Serial.print(now.second(), DEC);
Serial.println();
Serial.print("Light: ");
Serial.print(lux);
Serial.println(" lx");
for (int i = 0; i <= NUM_LEDS; i++) {
//Jam 6-10
if ((now.hour() >= 6) && (now.hour() < 10)){
leds[i] = CRGB (255 ,255, 0);
if ((lux > 350)){
FastLED.setBrightness(10);
}
else if ((lux <= 350)&&(lux >= 280)){
FastLED.setBrightness(26);
}
else if ((lux <= 279)&&(lux >= 210)){
FastLED.setBrightness(64);
}
else if ((lux <= 209)&&(lux >= 140)){
FastLED.setBrightness(128);
}
else if ((lux <= 139)&&(lux >= 70)){
FastLED.setBrightness(192);
}
else{
FastLED.setBrightness(255);
}
FastLED.show();
}
//Jam 10-14
else if ((now.hour() >= 10) && (now.hour() < 14)){
leds[i] = CRGB (255, 255, 255);
if ((lux > 350)){
FastLED.setBrightness(10);
}
else if ((lux <= 350)&&(lux >= 280)){
FastLED.setBrightness(26);
}
else if ((lux <= 279)&&(lux >= 210)){
FastLED.setBrightness(64);
}
else if ((lux <= 209)&&(lux >= 140)){
FastLED.setBrightness(128);
}
else if ((lux <= 139)&&(lux >= 70)){
FastLED.setBrightness(192);
}
else{
FastLED.setBrightness(255);
}
FastLED.show();
}
//Jam 14-18
else if ((now.hour() >= 14) && (now.hour() < 18)){
leds[i] = CRGB (255 ,255, 0);
if ((lux > 350)){
FastLED.setBrightness(10);
}
else if ((lux <= 350)&&(lux >= 280)){
FastLED.setBrightness(26);
}
else if ((lux <= 279)&&(lux >= 210)){
FastLED.setBrightness(64);
}
else if ((lux <= 209)&&(lux >= 140)){
FastLED.setBrightness(128);
}
else if ((lux <= 139)&&(lux >= 70)){
FastLED.setBrightness(192);
}
else{
FastLED.setBrightness(255);
}
FastLED.show();
}
//Jam 18 - 6
else{
leds[i] = CRGB (255, 255, 255);
if ((lux > 350)){
FastLED.setBrightness(10);
}
else if ((lux <= 350)&&(lux >= 280)){
FastLED.setBrightness(26);
}
else if ((lux <= 279)&&(lux >= 210)){
FastLED.setBrightness(64);
}
else if ((lux <= 209)&&(lux >= 140)){
FastLED.setBrightness(128);
}
else if ((lux <= 139)&&(lux >= 70)){
FastLED.setBrightness(192);
}
else{
FastLED.setBrightness(255);
}
FastLED.show();
}
}
delay(1000);
}

