#include <Adafruit_NeoPixel.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
// --- CONFIGURATIE ---
#define LED_PIN 5
#define LED_COUNT 16
#define i2c_Address 0x3c
#define TRIG_PIN 6
#define ECHO_PIN 7
#define RPM_PIN A2
Adafruit_NeoPixel ring(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
Adafruit_SH1107 display = Adafruit_SH1107(64, 128, &Wire);
// Pins voor knoppen
const int knopScherm = 2;
const int knopMute = 3;
const int buzzerPin = 4;
// Variabelen
int schermNummer = 0;
bool buzzerMuted = false;
unsigned long vorigeMillis = 0;
bool knipperStatus = false;
// --- HULPFUNCTIES ---
long leesVloeistofNiveau() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH, 20000);
if (duration <= 0) return 999;
return duration * 0.034 / 2;
}
void updateRingBalk(int waarde, int minW, int maxW) {
int aantalLedsAan = map(waarde, minW, maxW, 0, LED_COUNT);
for (int i = 0; i < LED_COUNT; i++) {
if (i < aantalLedsAan) {
if (i < (LED_COUNT * 0.7)) ring.setPixelColor(i, ring.Color(0, 255, 0));
else if (i < (LED_COUNT * 0.9)) ring.setPixelColor(i, ring.Color(255, 100, 0));
else ring.setPixelColor(i, ring.Color(255, 0, 0));
} else {
ring.setPixelColor(i, ring.Color(0, 0, 0));
}
}
ring.show();
}
void setup() {
display.begin(i2c_Address, true);
display.setRotation(3);
display.clearDisplay();
display.display();
ring.begin();
ring.setBrightness(150);
ring.show();
pinMode(knopScherm, INPUT_PULLUP);
pinMode(knopMute, INPUT_PULLUP);
pinMode(buzzerPin, OUTPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}
void loop() {
// 1. Sensoren uitlezen
int temp = map(analogRead(A0), 0, 1023, 0, 120);
float druk = analogRead(A1) * (6.0 / 1023.0);
int rpm = map(analogRead(RPM_PIN), 0, 1023, 0, 7000);
long niveau = leesVloeistofNiveau();
// 2. Alarm condities
bool coolantAlarm = (niveau > 20);
bool tempAlarm = (temp > 100);
bool drukLaag = (druk < 1.0);
bool drukHoog = (druk > 5.5);
bool rpmAlarm = (rpm > 6200);
bool algemeenAlarm = (coolantAlarm || tempAlarm || drukLaag || drukHoog || rpmAlarm);
// 3. Knoppen logica
if (digitalRead(knopScherm) == LOW) {
schermNummer = (schermNummer + 1) % 3;
delay(250);
}
if (algemeenAlarm && digitalRead(knopMute) == LOW) {
buzzerMuted = true;
delay(250);
}
if (!algemeenAlarm) {
buzzerMuted = false;
}
// 4. ALARM UITVOERING
if (algemeenAlarm) {
if (millis() - vorigeMillis > 200) {
vorigeMillis = millis();
knipperStatus = !knipperStatus;
if (knipperStatus) {
if (!buzzerMuted) tone(buzzerPin, 1200);
for(int i=0; i<LED_COUNT; i++) ring.setPixelColor(i, ring.Color(255, 0, 0));
} else {
noTone(buzzerPin);
ring.clear();
}
ring.show();
}
} else {
noTone(buzzerPin);
if (schermNummer == 0) updateRingBalk(temp, 0, 120);
else if (schermNummer == 1) updateRingBalk(int(druk * 10), 0, 60);
else updateRingBalk(rpm, 0, 7000);
}
// 5. OLED SCHERM TEKENEN
display.clearDisplay();
display.setTextColor(SH110X_WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
if (schermNummer == 0) display.print("MOTOR TEMP");
else if (schermNummer == 1) display.print("OLIEDRUK");
else display.print("TOERENTAL");
if (buzzerMuted && algemeenAlarm) {
display.setCursor(90, 0);
display.print("[M]");
}
display.setTextSize(2);
display.setCursor(15, 22);
if (schermNummer == 0) { display.print(temp); display.print(" C"); }
else if (schermNummer == 1) { display.print(druk, 1); display.print(" BAR"); }
else { display.print(rpm); display.print(" RPM"); }
// ALARM TEKSTEN ONDERIN (Aangepast voor breedte)
display.setTextSize(1);
display.setCursor(0, 50);
if (coolantAlarm) display.print("!! LOW COOLANT !!");
else if (tempAlarm) display.print("!! ENGINE HOT !!");
else if (drukLaag) display.print("!! LOW OIL !!");
else if (drukHoog) display.print("!! HIGH OIL !!");
else if (rpmAlarm) display.print("!! OVER REV !!");
else {
display.drawRect(0, 56, 128, 6, SH110X_WHITE);
int b = 0;
if (schermNummer == 0) b = map(temp, 0, 120, 0, 128);
else if (schermNummer == 1) b = map(int(druk*10), 0, 60, 0, 128);
else b = map(rpm, 0, 7000, 0, 128);
display.fillRect(0, 56, b, 6, SH110X_WHITE);
}
display.display();
}Loading
grove-oled-sh1107
grove-oled-sh1107