/* Session 1 — Simple irrigation pump simulation (Arduino Uno — works in Tinkercad Circuits) Parts : green LED (pump running) on D9 (PWM) + 220Ω, red LED (tank empty) on D12 + 220Ω, push button on D2 (INPUT_PULLUP, other leg to GND), potentiometer on A0 (pump speed) Concepts: GPIO (digitalRead / digitalWrite), PWM (analogWrite 0-255), Serial Communication */ const int PUMP_LED = 9; // PWM pin (~) const int ALARM_LED = 12; const int BUTTON = 2; const int SPEED_POT = A0; bool pumpOn = false; bool lastButton = HIGH; unsigned long startedAt = 0; const unsigned long MAX_RUN_MS = 10000; // safety: stop the pump after 10 s in the simulation void setup() { pinMode(PUMP_LED, OUTPUT); pinMode(ALARM_LED, OUTPUT); pinMode(BUTTON, INPUT_PULLUP); // pressed = LOW Serial.begin(9600); Serial.println(F("KFS farm pump simulator ready. Press the button to start/stop irrigation.")); } void loop() { bool now = digitalRead(BUTTON); if (lastButton == HIGH && now == LOW) { // falling edge = one press pumpOn = !pumpOn; startedAt = millis(); Serial.println(pumpOn ? F("Pump ON") : F("Pump OFF")); delay(30); // debounce } lastButton = now; if (pumpOn && millis() - startedAt > MAX_RUN_MS) { pumpOn = false; digitalWrite(ALARM_LED, HIGH); // timeout -> alarm LED (e.g. tank empty / pipe blocked) Serial.println(F("Safety timeout! Pump stopped.")); } int pot = analogRead(SPEED_POT); // 0..1023 int duty = map(pot, 0, 1023, 0, 255); // 0..255 (PWM duty cycle) analogWrite(PUMP_LED, pumpOn ? duty : 0); if (!pumpOn && digitalRead(BUTTON) == LOW) digitalWrite(ALARM_LED, LOW); static unsigned long lastPrint = 0; if (millis() - lastPrint > 1000) { lastPrint = millis(); Serial.print(F("pump=")); Serial.print(pumpOn); Serial.print(F(" speed%=")); Serial.println(duty * 100 / 255); } }