#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <RTClib.h>
#define RED_PIN 5
#define GREEN_PIN 18
#define BLUE_PIN 19
#define ENCODER_CLK 33
#define ENCODER_DT 32
#define ENCODER_SW 34
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define OLED_PAGEADDR 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
RTC_DS1307 rtc;
const char* colorNames[] = { "Rood", "Groen", "Blauw", "Geel", "Cyaan", "Magenta", "Wit" };
const uint8_t colors[][3] = {
{255, 0, 0}, {0, 255, 0}, {0, 0, 255},
{255, 255, 0}, {0, 255, 255}, {255, 0, 255}, {255, 255, 255}
};
int currentColorIndex = 0;
bool kleurActief = true;
unsigned long lastColorChange = 0;
int draaiStatus = 0;
bool knopStatus = false;
bool lastClkState = HIGH;
bool lastDtState = HIGH;
unsigned long lastDraaiTijd = 0;
const unsigned long draaiTimeout = 500;
bool lastBtnState = HIGH;
int klikTeller = 0;
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("=== STARTING SYSTEM ===");
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
pinMode(ENCODER_CLK, INPUT);
pinMode(ENCODER_DT, INPUT);
pinMode(ENCODER_SW, INPUT_PULLUP);
Wire.begin(21, 22);
if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_PAGEADDR)) {
Serial.println("OLED niet gevonden!");
while (true);
}
display.clearDisplay();
display.display();
if (!rtc.begin()) {
Serial.println("RTC niet gevonden!");
while (true);
}
if (!rtc.isrunning()) {
Serial.println("RTC was niet actief. Tijd instellen...");
rtc.adjust(DateTime(2025, 5, 10, 14, 0, 0));
}
Serial.println("Opstart voltooid.");
}
void setColor(uint8_t r, uint8_t g, uint8_t b) {
analogWrite(RED_PIN, 255 - r);
analogWrite(GREEN_PIN, 255 - g);
analogWrite(BLUE_PIN, 255 - b);
}
bool readEncoderClick() {
bool currentBtnState = digitalRead(ENCODER_SW);
if (currentBtnState != lastBtnState) {
delay(5);
currentBtnState = digitalRead(ENCODER_SW);
if (currentBtnState != lastBtnState) {
lastBtnState = currentBtnState;
if (currentBtnState == LOW) {
return true;
}
}
}
return false;
}
int readEncoderRotation() {
bool clkState = digitalRead(ENCODER_CLK);
bool dtState = digitalRead(ENCODER_DT);
int result = 0;
if (clkState != lastClkState || dtState != lastDtState) {
if (clkState != dtState) {
if (clkState == HIGH) result = 1; // Rechtsom
else result = -1; // Linksom
lastDraaiTijd = millis();
}
lastClkState = clkState;
lastDtState = dtState;
}
if (millis() - lastDraaiTijd > draaiTimeout && result == 0) {
return 0;
}
return result;
}
void loop() {
unsigned long nowMillis = millis();
// === Kleurcyclus ===
if (kleurActief && nowMillis - lastColorChange > 1000) {
currentColorIndex = (currentColorIndex + 1) % 7;
setColor(colors[currentColorIndex][0], colors[currentColorIndex][1], colors[currentColorIndex][2]);
lastColorChange = nowMillis;
Serial.println("[LED] Kleur: " + String(colorNames[currentColorIndex]));
}
// === Draairichting uitlezen ===
int draaiWaarde = readEncoderRotation();
if (draaiWaarde != 0) {
draaiStatus = draaiWaarde;
} else if (millis() - lastDraaiTijd > draaiTimeout) {
draaiStatus = 0;
}
// === Klik detectie ===
if (readEncoderClick()) {
klikTeller++;
kleurActief = (klikTeller % 2 == 0);
Serial.println("[Encoder] Klik #" + String(klikTeller) + " - Kleur cyclus " + (kleurActief ? "AAN" : "UIT"));
}
// === Knopstatus bijwerken ===
knopStatus = digitalRead(ENCODER_SW) == LOW;
// === Debug info altijd tonen ===
Serial.print("[Encoder Status] Draai: ");
Serial.print(draaiStatus);
Serial.print(" | Knop: ");
Serial.println(knopStatus ? "1" : "0");
// === Tijd ophalen ===
DateTime nu = rtc.now();
char tijdStr[9];
sprintf(tijdStr, "%02d:%02d:%02d", nu.hour(), nu.minute(), nu.second());
// === OLED tekenen ===
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
int16_t x = (SCREEN_WIDTH - strlen(colorNames[currentColorIndex]) * 12) / 2;
display.setCursor(x, 20);
display.print(colorNames[currentColorIndex]);
display.setTextSize(1);
display.setCursor(SCREEN_WIDTH - strlen(tijdStr) * 6, 0);
display.print(tijdStr);
display.setCursor(0, 48);
display.print("Draai: ");
display.print(draaiStatus);
display.setCursor(0, 56);
display.print("Knop: ");
display.print(knopStatus ? "1" : "0");
display.display();
delay(200);
}