#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 display(128, 64, &Wire, -1);
#define LEFT_LDR A3
#define BOTTOM_LDR A1
#define RIGHT_LDR A2
#define LED 12
#define MIN_VALUE 700
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
pinMode(LED, OUTPUT);
}
void loop() {
bool left_triggered = getLux(analogRead(LEFT_LDR)) > MIN_VALUE;
bool right_triggered = getLux(analogRead(RIGHT_LDR)) > MIN_VALUE;
bool bottom_triggered = getLux(analogRead(BOTTOM_LDR)) > MIN_VALUE;
int triggered_count = left_triggered + right_triggered + bottom_triggered;
blink(triggered_count);
if (triggered_count == 3) {
printText("Vehicle surfaced");
}
else if (triggered_count == 2) {
if (left_triggered && right_triggered) {
printText("Light - 1 2");
}
else if (left_triggered && bottom_triggered) {
printText("Light - 1 3");
}
else if (right_triggered && bottom_triggered) {
printText("Light - 2 3");
}
}
else if (triggered_count == 1) {
if (left_triggered) {
printText("Light - 1");
}
else if (right_triggered) {
printText("Light - 2");
}
else if (bottom_triggered) {
printText("Light - 3");
}
} else {
printText("");
}
delay(5);
}
void blink(int count) {
for (int i = 0; i < count; i++) {
digitalWrite(LED, HIGH);
delay(100);
digitalWrite(LED, LOW);
delay(100);
}
}
void printText(const char* text) {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(15, 28);
display.println(text);
display.display();
}
float getLux(int adc) {
constexpr float R1 = 127410.0f;
constexpr float GAMMA = 0.8582f;
constexpr float R_FIXED = 10000.0f;
float rLdr = R_FIXED * (1023.0f / adc - 1.0f);
float lux = pow(R1 / rLdr, 1.0f / GAMMA);
return lux;
}