123 lines
2.6 KiB
C++
123 lines
2.6 KiB
C++
#include <Wire.h>
|
|
#include <BH1750.h>
|
|
#include <Adafruit_GFX.h>
|
|
#include <Adafruit_SSD1306.h>
|
|
#define Version "0.1.0"
|
|
|
|
#define PinLED 2 // on-board LED
|
|
#define IN1 32
|
|
#define IN2 14
|
|
|
|
#define SCREEN_WIDTH 128
|
|
#define SCREEN_HEIGHT 32
|
|
#define OLED_RESET -1
|
|
#define SCREEN_ADDRESS 0x3C
|
|
|
|
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
|
|
|
|
BH1750 lightMeter;
|
|
|
|
void setup() {
|
|
Serial.begin(9600);
|
|
Wire.begin();
|
|
lightMeter.begin();
|
|
|
|
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
|
|
Serial.println(F("SSD1306 allocation failed"));
|
|
}
|
|
|
|
display.setTextSize(1);
|
|
display.setTextColor(SSD1306_WHITE);
|
|
display.clearDisplay();
|
|
display.setCursor(0, 0);
|
|
display.println("Version:");
|
|
display.setCursor(60, 0);
|
|
display.println(Version);
|
|
display.display();
|
|
|
|
|
|
pinMode(IN1, OUTPUT);
|
|
pinMode(IN2, OUTPUT);
|
|
pinMode(PinLED, OUTPUT);
|
|
}
|
|
|
|
void pulsujNaprzemiennie(int czasSekundy) {
|
|
const int steps = 150;
|
|
int delayPerStep = (czasSekundy * 1000) / (2 * steps); // ms
|
|
|
|
for (int i = 0; i <= steps; i++) {
|
|
float phase = (float)i / steps;
|
|
float brightness1 = 1.0 - phase;
|
|
float brightness2 = phase;
|
|
|
|
int t_on1 = (int)(brightness1 * 2000);
|
|
int t_on2 = (int)(brightness2 * 2000);
|
|
|
|
digitalWrite(IN1, HIGH);
|
|
digitalWrite(IN2, LOW);
|
|
delayMicroseconds(t_on1);
|
|
|
|
digitalWrite(IN1, LOW);
|
|
digitalWrite(IN2, HIGH);
|
|
delayMicroseconds(t_on2);
|
|
|
|
digitalWrite(IN1, LOW);
|
|
digitalWrite(IN2, LOW);
|
|
delay(delayPerStep - 4);
|
|
}
|
|
|
|
for (int i = steps; i >= 0; i--) {
|
|
float phase = (float)i / steps;
|
|
float brightness1 = 1.0 - phase;
|
|
float brightness2 = phase;
|
|
|
|
int t_on1 = (int)(brightness1 * 2000);
|
|
int t_on2 = (int)(brightness2 * 2000);
|
|
|
|
digitalWrite(IN1, HIGH);
|
|
digitalWrite(IN2, LOW);
|
|
delayMicroseconds(t_on1);
|
|
|
|
digitalWrite(IN1, LOW);
|
|
digitalWrite(IN2, HIGH);
|
|
delayMicroseconds(t_on2);
|
|
|
|
digitalWrite(IN1, LOW);
|
|
digitalWrite(IN2, LOW);
|
|
delay(delayPerStep - 4);
|
|
}
|
|
}
|
|
|
|
void loop() {
|
|
display.clearDisplay();
|
|
|
|
uint16_t lux = lightMeter.readLightLevel();
|
|
Serial.print("Light: ");
|
|
Serial.print(lux);
|
|
Serial.println(" lx");
|
|
|
|
|
|
display.setCursor(0, 0);
|
|
display.println("Luxy:");
|
|
display.setCursor(40, 0);
|
|
display.println(lux);
|
|
|
|
if (lux < 10 && lux >= 1) {
|
|
digitalWrite(PinLED, LOW);
|
|
pulsujNaprzemiennie(5); // cykl trwa teraz 5 sekund
|
|
display.setCursor(0, 10);
|
|
display.println("Pulsowanie");
|
|
} else {
|
|
digitalWrite(PinLED, HIGH);
|
|
display.setCursor(0, 10);
|
|
display.println("OFF");
|
|
if (lux < 1) {
|
|
display.setCursor(0, 20);
|
|
display.println("Zbyt ciemno");
|
|
}
|
|
delay(500);
|
|
}
|
|
display.display();
|
|
|
|
}
|