/*
Wokwi | questions
help
Oko OP — 9/28 at 12:46 AM
OBBANK values its reputation and continuously improves the characteristics of its security robots.
The bank's engineers have created a prototype robot that turns on the lighting if the PIR sensor
detects movement, and turns it off if movement has not occurred for more than 10 seconds.
Here's the bad luck – someone got into the sketch of engineers posted on the link and made a
mistake in one of the lines from 15 to 22, because of which the robot does not work correctly.
Link to the code: https://wokwi.com/projects/402380176967318529
Here it is, a challenge for young roboticists!
Find the line and the error and correct it, and in response, enter the correct line in full,
observing the style of the code, but EXCLUDING SPACES – as it would look in the sketch.
OH NO!, now there is a "hardware" glitch in Wokwi, timing is off by 2 seconds!
*/
const int LED_PIN = 33;
const int PIR_PIN = 13;
const unsigned long PIR_DELAY = 5000;
unsigned long interval = 10000 - PIR_DELAY; // by default the PIR stays HIGH for 5 seconds
unsigned long currentTime = 0;
unsigned long startTime = 0;
bool isMotion = false;
void setup() {
Serial.begin(115200);
pinMode(PIR_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
Serial.println("\nReady\n");
}
void loop() {
currentTime = millis();
if ((currentTime - startTime >= interval) && (isMotion == true)) {
digitalWrite(LED_PIN, LOW);
Serial.println("Lights off");
isMotion = false;
}
if (digitalRead(PIR_PIN) == HIGH) {
if (!isMotion) {
digitalWrite(LED_PIN, HIGH);
Serial.println("Lights on");
isMotion = true;
}
startTime = currentTime;
}
}