Hello -
I fadged together some other peoples sketches to print the Lux value from a BH1750 light sensor to an OLED, and also to switch a relay that turns on one LED, or another one, depending on how much light there is.
And I think I understand it. Except that when I was commenting it I called something an Object, something else an instance of an Object, and something else a function. I did this based on what I remember from when I was trying to program in Javascript. As I understand it, an Arduino sketch is written in C.
Is my labeling of an Object, etc. correct ?
/*
BH1750 & OLED 1306 library usage.
This sketch reads the BH1750 and either sends a 1 or a 0 to pin 11, depending on
if the LUX value is above or below the threshold value. Pin 11 is connected to a
5v relay. If a 1, one LED is turned on. If a 0, the other LED is turned on.
This will be used to control the direction of a DC motor, instead of two LEDs.
Connection:
VCC-3.3v (Powers both the BH1750 & the 1306 OLED) BH1750 2.4v to 3.6v 1306 OLED 1.65v to 3.3v
GND-GND
SCL-SCL(analog pin 5) Clock Line
SDA-SDA(analog pin 4) Data Line
ADD-NC Not Connected
*/
#include <Wire.h>
#include <BH1750.h>
#include <SPI.h> // If using the Wire.h library, SPI.h must be included
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET); //this reset is required to intitiate the OLED
BH1750 lightMeter; // starts an instance of the BH1750 object
void setup(){
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // starts the display located at address 0x3C
pinMode(13, OUTPUT);
digitalWrite(13,LOW); // turn off the UNO on-board LED
pinMode(11, OUTPUT); // initialize digital pin 11 as an output.
lightMeter.begin(); //begins function lightMeter
}
void loop() {
unsigned int lux = lightMeter.readLightLevel(); //Reads the light level
delay(500);
display.clearDisplay();
display.setTextColor(WHITE);
display.setCursor(18,20); //starts at Row 18 & Col 20
display.setTextSize(2);
String stringOne = "";
String stringTwo = stringOne + lux;
String stringThree = stringTwo + " LUX";
display.println(stringThree);
display.println(""); // adds a linebreak
display.display(); // this command must be used after EVERY display.println
display.setTextSize(1);
if (lux > 80)
{
display.println(" Hello frogging World");
digitalWrite(11,HIGH);
}
else
{
display.println(" Lights Out");
digitalWrite(11,LOW);
}
display.display();
delay(1500); // wait for 1.5 second
display.clearDisplay();
display.display();
delay(1500);
}