#define LED_NORMAL 25
#define LED_THEFT 26
#define ZONE1_LED 27
#define ZONE2_LED 14
#define ZONE3_LED 12
// BUTTON PINS
#define BTN_Z1 32
#define BTN_Z2 33
#define BTN_Z3 35
void setup() {
Serial.begin(115200);
// Set LEDs as Outputs
pinMode(LED_NORMAL, OUTPUT);
pinMode(LED_THEFT, OUTPUT);
pinMode(ZONE1_LED, OUTPUT);
pinMode(ZONE2_LED, OUTPUT);
pinMode(ZONE3_LED, OUTPUT);
// Set Buttons as Inputs
// INPUT_PULLUP makes the button stay "HIGH" until you press it
pinMode(BTN_Z1, INPUT_PULLUP);
pinMode(BTN_Z2, INPUT_PULLUP);
pinMode(BTN_Z3, INPUT_PULLUP);
}
void loop() {
// Base consumption for 3 zones
float z1 = 1.0, z2 = 1.5, z3 = 0.8;
float billed_total = z1 + z2 + z3;
// Check if buttons are pressed (LOW means pressed)
bool theft1 = (digitalRead(BTN_Z1) == LOW);
bool theft2 = (digitalRead(BTN_Z2) == LOW);
bool theft3 = (digitalRead(BTN_Z3) == LOW);
// If a button is pressed, add "stolen" current to the master
float master_supply = billed_total;
if(theft1) master_supply += 2.0;
if(theft2) master_supply += 2.0;
if(theft3) master_supply += 2.0;
float leakage = master_supply - billed_total;
// Visual Alerts
if (leakage > 0.5) {
digitalWrite(LED_THEFT, HIGH);
digitalWrite(LED_NORMAL, LOW);
// Light up the specific zone where button is pressed
digitalWrite(ZONE1_LED, theft1);
digitalWrite(ZONE2_LED, theft2);
digitalWrite(ZONE3_LED, theft3);
Serial.println("🚨 ALERT: MANUAL THEFT INJECTED!");
} else {
digitalWrite(LED_THEFT, LOW);
digitalWrite(LED_NORMAL, HIGH);
digitalWrite(ZONE1_LED, LOW);
digitalWrite(ZONE2_LED, LOW);
digitalWrite(ZONE3_LED, LOW);
Serial.println("✅ Grid Stable");
}
delay(200); // Fast check for responsive buttons
}