#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_NeoPixel.h>
#define NUMSLEDs 2
#define PINSLEDs 10
#define OLED_RESET -1
Adafruit_SSD1306 OLED (OLED_RESET);
int BH1750address = 0x23;
byte buff[2];
int t=0;
int temp1 = 0;
int temp2 = 0;
int changeTime;
Adafruit_NeoPixel SLEDs = Adafruit_NeoPixel(NUMSLEDs,PINSLEDs, NEO_RGB + NEO_KHZ800);
void setup()
{
OLED.begin(SSD1306_SWITCHCAPVCC, 0x3c);
SLEDs.begin(); // This initializes the NeoPixel library.
// Initialize all SLEDs to 'off'
Serial.begin(115200);
SLEDs.setPixelColor(0, SLEDs.Color(0, 0, 0));
SLEDs.setPixelColor(1, SLEDs.Color(0, 0, 0));
SLEDs.show();
}
void loop()
{
changeTime = millis();
uint16_t val = 0;
BH1750_Init(BH1750address);
delay(200);
if (2 == BH1750_Read(BH1750address))
{
val = ((buff[0] << 8) | buff[1]) / 1.2;
OLED.clearDisplay();
OLED.setTextColor(WHITE);
OLED.setCursor(0, 0);
OLED.setTextSize(1);
OLED.println("BH1750");
OLED.setCursor(0, 15);
OLED.println(String(val) + " lux");
OLED.display();
Serial.println(val);
}
if(buff[1]/1.2 < 80)
{
// t++;
t=t+1;
// delay(500);
while(buff[1]/1.2 < 80)
{
uint16_t val = 0;
BH1750_Init(BH1750address); // Initial BH1750
delay(200);
BH1750_Read(BH1750address); // Read and check data from BH1750
val = ((buff[0] << 8) | buff[1])/1.2;
}
if(t==1)
{
SLEDs.setPixelColor(0, SLEDs.Color(255, 0, 0));
SLEDs.setPixelColor(1, SLEDs.Color(0, 0, 0));
SLEDs.show();
//delay(50);
}
if(t==3)
{
SLEDs.setPixelColor(0, SLEDs.Color(0, 255, 0));
SLEDs.setPixelColor(1, SLEDs.Color(0, 0, 0));
SLEDs.show();
// delay(50);
}
if(t==6)
{
SLEDs.setPixelColor(0, SLEDs.Color(0, 0, 255));
SLEDs.setPixelColor(1, SLEDs.Color(0, 0, 0));
SLEDs.show();
//delay(50);
}
//showtime = millis();
if(millis()-changeTime > 4000)
{
//Serial.println();
SLEDs.setPixelColor(0, SLEDs.Color(0, 0, 0));
SLEDs.setPixelColor(1, SLEDs.Color(0, 0, 0));
SLEDs.show();
//delay(50);
//delay(4000);
t=0;
}
// delay(150);
Serial.println(t);
}
}
int BH1750_Read(int address)
{
int i = 0;
Wire.beginTransmission(address);
Wire.requestFrom(address, 2);
while (Wire.available())
{
buff[i] = Wire.read();
i++;
}
Wire.endTransmission();
return i;
}
void BH1750_Init(int address)
{
Wire.beginTransmission(address);
Wire.write(0x10);
Wire.endTransmission();
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น