#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
#define LED_PIN 23
#define BUZZER_PIN 25
#define SLIDE_POT_PIN 35 // Slide potentiometer → UV-C intensity
// Thresholds for ML prediction
#define GLYPHOSATE_THRESHOLD 5.0
#define UV_THRESHOLD 20
// Simulate sensor readings gradually
float simulate_sensor(float &val, float minVal, float maxVal) {
val += random(-15,16)/100.0;
if(val < minVal) val = minVal;
if(val > maxVal) val = maxVal;
return val;
}
// Show dashboard (initial interface)
void show_dashboard() {
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0,0);
display.println("Glyphosate Detector");
display.setCursor(0,12);
display.println("UV-C LED: 255-280 nm");
display.setCursor(0,24);
display.println("Collecting readings...");
display.display();
}
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(SLIDE_POT_PIN, INPUT);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)){
Serial.println("SSD1306 not found");
while(1) delay(10);
}
randomSeed(analogRead(0));
show_dashboard();
delay(2000);
}
void loop() {
const int numReadings = 10;
float glyReadings[numReadings];
float uvReadings[numReadings];
float simulatedGly = random(0,1001)/100.0; // 0-10 mg/L
// Take readings
for(int i=0;i<numReadings;i++){
float gly = simulate_sensor(simulatedGly,0,10); // Automatic glyphosate
float uv = map(analogRead(SLIDE_POT_PIN),0,4095,0,100); // UV-C manually via slide pot
glyReadings[i] = gly;
uvReadings[i] = uv;
// Show **current reading** on OLED
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0,0);
display.println("Current Reading:");
display.setCursor(0,16);
display.print("Glyphosate: "); display.print(gly,2); display.println(" mg/L");
display.setCursor(0,32);
display.print("UV-C: "); display.print(uv,0); display.println("%");
display.display();
delay(300); // small delay between readings
}
// Calculate average
float avgGly = 0, avgUV = 0;
for(int i=0;i<numReadings;i++){
avgGly += glyReadings[i];
avgUV += uvReadings[i];
}
avgGly /= numReadings;
avgUV /= numReadings;
// ML prediction
String mlPrediction = (avgGly >= GLYPHOSATE_THRESHOLD && avgUV >= UV_THRESHOLD) ? "UNSAFE" : "SAFE";
// Alerts
digitalWrite(LED_PIN, mlPrediction=="UNSAFE"?HIGH:LOW);
if(mlPrediction=="UNSAFE") tone(BUZZER_PIN, 1000);
else noTone(BUZZER_PIN);
// Show ML prediction on OLED
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0,0);
display.print("Avg Glyphosate: "); display.print(avgGly,2); display.println(" mg/L");
display.setCursor(0,16);
display.print("Avg UV-C: "); display.print(avgUV,0); display.println("%");
display.setCursor(0,32);
display.print("ML Prediction: "); display.println(mlPrediction);
display.setCursor(0,48);
display.println("Peak Abs: 205 nm");
display.display();
// Serial output
Serial.print("Avg Glyphosate: "); Serial.print(avgGly,2);
Serial.print(" mg/L | Avg UV-C: "); Serial.print(avgUV,0);
Serial.print("% | ML Prediction: "); Serial.println(mlPrediction);
delay(5000);
show_dashboard();
delay(2000);
}