109 lines
2.7 KiB
C++
109 lines
2.7 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 = 400; // większa liczba = gładsze przejścia
|
|
const int totalMillis = czasSekundy * 1000;
|
|
const int delayPerStep = totalMillis / steps;
|
|
|
|
for (int i = 0; i <= steps; i++) {
|
|
float phase = (float)i / steps;
|
|
float angle = phase * PI; // pół cyklu (od 0 do π)
|
|
|
|
// sinusoidalne przejście
|
|
float brightness1 = (cos(angle) + 1.0) / 2.0; // 1 → 0
|
|
float brightness2 = 1.0 - brightness1; // 0 → 1
|
|
|
|
// Konwersja jasności do długości impulsu (maks. 2000us)
|
|
int pulse1 = (int)(brightness1 * 2000);
|
|
int pulse2 = (int)(brightness2 * 2000);
|
|
|
|
// Wysterowanie IN1
|
|
digitalWrite(IN1, HIGH);
|
|
delayMicroseconds(pulse1);
|
|
digitalWrite(IN1, LOW);
|
|
|
|
// Wysterowanie IN2
|
|
digitalWrite(IN2, HIGH);
|
|
delayMicroseconds(pulse2);
|
|
digitalWrite(IN2, LOW);
|
|
|
|
// Opóźnienie (reszta czasu kroku)
|
|
delayMicroseconds(1000 * delayPerStep - pulse1 - pulse2);
|
|
}
|
|
|
|
// drugi półcykl: kolory zamienione miejscami (pełny cykl = 2x π)
|
|
for (int i = 0; i <= steps; i++) {
|
|
float phase = (float)i / steps;
|
|
float angle = phase * PI;
|
|
|
|
float brightness1 = (cos(angle) + 1.0) / 2.0;
|
|
float brightness2 = 1.0 - brightness1;
|
|
|
|
int pulse1 = (int)(brightness2 * 2000);
|
|
int pulse2 = (int)(brightness1 * 2000);
|
|
|
|
digitalWrite(IN1, HIGH);
|
|
delayMicroseconds(pulse1);
|
|
digitalWrite(IN1, LOW);
|
|
|
|
digitalWrite(IN2, HIGH);
|
|
delayMicroseconds(pulse2);
|
|
digitalWrite(IN2, LOW);
|
|
|
|
delayMicroseconds(1000 * delayPerStep - pulse1 - pulse2);
|
|
}
|
|
}
|
|
|
|
void loop() {
|
|
uint16_t lux = lightMeter.readLightLevel();
|
|
if (lux < 10) {
|
|
digitalWrite(PinLED, LOW);
|
|
pulsujNaprzemiennie(8); // pełny cykl 8 sekund
|
|
} else {
|
|
digitalWrite(PinLED, HIGH);
|
|
display.setCursor(0, 10);
|
|
display.println("OFF");
|
|
delay(5000);
|
|
}
|
|
display.display();
|
|
}
|