#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// ---- Wokwi mock mode (no WiFi libs needed) ----
#define USE_WIFI_NINA 0
#define USE_WIFIESPAT 0
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int TRIG_PIN = 9;
const int ECHO_PIN = 10;
const int DIST_THRESHOLD_CM = 10;
const unsigned long DEBOUNCE_MS = 1200;
unsigned long lastTriggerMs = 0;
bool armed = true;
long readDistanceCm() {
digitalWrite(TRIG_PIN, LOW); delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH, 30000UL); // 30ms timeout
if (duration == 0) return 9999;
return (duration * 34L) / 2000L; // (duration*0.034)/2
}
// ---- Your API config (just for printing in mock mode) ----
const char* BIN_ID = "bin-123";
const char* DEVICE_ID = "arduino-uno-01";
long totalCount = 0;
void showCount() {
lcd.clear();
lcd.setCursor(0,0); lcd.print("Recycled:");
lcd.setCursor(0,1); lcd.print(totalCount);
}
void sendEventJSON(long delta) {
String json = String("{\"bin_id\":\"") + BIN_ID +
"\",\"device_id\":\"" + DEVICE_ID +
"\",\"delta\":" + delta + "}";
Serial.print("[MOCK POST] "); Serial.println(json);
}
void setup() {
Serial.begin(115200);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
lcd.init(); lcd.backlight();
// If LCD is blank, try 0x3F instead of 0x27:
// LiquidCrystal_I2C lcd(0x3F, 16, 2);
showCount();
}
void loop() {
long d = readDistanceCm();
unsigned long now = millis();
if (d < DIST_THRESHOLD_CM && armed && (now - lastTriggerMs > DEBOUNCE_MS)) {
totalCount += 1;
showCount();
sendEventJSON(1);
lastTriggerMs = now;
armed = false; // wait for object to move away
}
if (d >= DIST_THRESHOLD_CM) {
armed = true;
}
delay(50);
}