diff --git a/ESP32/LampkiBalkon/LampkiBalkon.ino b/ESP32/LampkiBalkon/LampkiBalkon.ino index e922939..5f09b5c 100644 --- a/ESP32/LampkiBalkon/LampkiBalkon.ino +++ b/ESP32/LampkiBalkon/LampkiBalkon.ino @@ -14,7 +14,6 @@ #define SCREEN_ADDRESS 0x3C Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); - BH1750 lightMeter; void setup() { @@ -22,7 +21,7 @@ void setup() { Wire.begin(); lightMeter.begin(); - if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { + if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1306 allocation failed")); } @@ -35,88 +34,75 @@ void setup() { 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 + 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 brightness1 = 1.0 - phase; - float brightness2 = phase; + 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 - int t_on1 = (int)(brightness1 * 2000); - int t_on2 = (int)(brightness2 * 2000); + // 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); - digitalWrite(IN2, LOW); - delayMicroseconds(t_on1); - + delayMicroseconds(pulse1); digitalWrite(IN1, LOW); + + // Wysterowanie IN2 digitalWrite(IN2, HIGH); - delayMicroseconds(t_on2); - - digitalWrite(IN1, LOW); + delayMicroseconds(pulse2); digitalWrite(IN2, LOW); - delay(delayPerStep - 4); + + // Opóźnienie (reszta czasu kroku) + delayMicroseconds(1000 * delayPerStep - pulse1 - pulse2); } - for (int i = steps; i >= 0; i--) { + // drugi półcykl: kolory zamienione miejscami (pełny cykl = 2x π) + for (int i = 0; i <= steps; i++) { float phase = (float)i / steps; - float brightness1 = 1.0 - phase; - float brightness2 = phase; + float angle = phase * PI; - int t_on1 = (int)(brightness1 * 2000); - int t_on2 = (int)(brightness2 * 2000); + 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); - digitalWrite(IN2, LOW); - delayMicroseconds(t_on1); - + delayMicroseconds(pulse1); digitalWrite(IN1, LOW); + digitalWrite(IN2, HIGH); - delayMicroseconds(t_on2); - - digitalWrite(IN1, LOW); + delayMicroseconds(pulse2); digitalWrite(IN2, LOW); - delay(delayPerStep - 4); + + delayMicroseconds(1000 * delayPerStep - pulse1 - pulse2); } } 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) { + if (lux < 10) { digitalWrite(PinLED, LOW); - pulsujNaprzemiennie(5); // cykl trwa teraz 5 sekund - display.setCursor(0, 10); - display.println("Pulsowanie"); - } else { + pulsujNaprzemiennie(8); // pełny cykl 8 sekund + } else { digitalWrite(PinLED, HIGH); display.setCursor(0, 10); display.println("OFF"); - if (lux < 1) { - display.setCursor(0, 20); - display.println("Zbyt ciemno"); - } - delay(500); + delay(5000); } display.display(); - }