IR sensor for obstacle detection in Arduino - Time & Space Complexity
When using an IR sensor for obstacle detection, the program often checks sensor readings repeatedly.
We want to understand how the time spent grows as the program runs longer or checks more times.
Analyze the time complexity of the following code snippet.
const int irSensorPin = A0;
const int threshold = 500;
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(irSensorPin);
if (sensorValue > threshold) {
Serial.println("Obstacle detected");
}
delay(100);
}
This code reads the IR sensor value repeatedly and prints a message if an obstacle is detected.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
loop()function runs continuously, reading the sensor each time. - How many times: It repeats indefinitely, checking the sensor every 100 milliseconds.
Each cycle reads the sensor once and does a simple check.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 (checks) | 10 sensor reads and checks |
| 100 (checks) | 100 sensor reads and checks |
| 1000 (checks) | 1000 sensor reads and checks |
Pattern observation: The number of operations grows directly with the number of sensor checks.
Time Complexity: O(n)
This means the time spent grows linearly with how many times the sensor is checked.
[X] Wrong: "The sensor reading inside the loop makes the program run in constant time because it's just one line."
[OK] Correct: Even though one reading is simple, the loop repeats many times, so total time grows with the number of checks.
Understanding how repeated sensor checks affect time helps you explain how embedded programs handle real-time data efficiently.
"What if we added a nested loop to check multiple sensors each cycle? How would the time complexity change?"
Practice
Solution
Step 1: Understand IR sensor function
IR sensors emit infrared light and detect the light reflected back from objects.Step 2: Identify what is detected
The sensor measures reflected infrared light, not visible light, sound, or magnetic fields.Final Answer:
Reflected infrared light from nearby objects -> Option DQuick Check:
IR sensor = reflected infrared light [OK]
- Confusing IR sensor with light or sound sensors
- Thinking it detects visible light
- Assuming it detects magnetic fields
Solution
Step 1: Identify correct function for analog reading
analogRead(pin) reads analog sensor values from specified pin.Step 2: Check pin and function usage
Pin A0 is analog input; analogRead(A0) is correct syntax.Final Answer:
int sensorValue = analogRead(A0); -> Option AQuick Check:
Use analogRead for analog sensors [OK]
- Using digitalRead() for analog sensors
- Using analogWrite() which is for output
- Using digitalWrite() which is for digital output
int sensorValue = 350;
int threshold = 300;
if(sensorValue > threshold) {
Serial.println("No obstacle");
} else {
Serial.println("Obstacle detected");
}Solution
Step 1: Compare sensorValue with threshold
350 is greater than 300, so condition sensorValue > threshold is true.Step 2: Determine printed output
Since condition is true, code prints "No obstacle".Final Answer:
No obstacle -> Option BQuick Check:
350 > 300 = true, prints "No obstacle" [OK]
- Confusing greater than with less than
- Expecting sensor value to print
- Thinking else block runs when condition is true
int sensorPin = A0;
int threshold = 400;
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue = digitalRead(sensorPin);
if(sensorValue < threshold) {
Serial.println("Obstacle detected");
}
delay(100);
}Solution
Step 1: Check sensor reading function
IR sensor outputs analog values, so analogRead() must be used, not digitalRead().Step 2: Verify other code parts
Serial.begin is present, delay is reasonable, comparison operator is correct.Final Answer:
Using digitalRead instead of analogRead for analog sensor -> Option AQuick Check:
Analog sensor needs analogRead() [OK]
- Using digitalRead for analog sensor pins
- Forgetting Serial.begin() (not the case here)
- Misusing delay() values
Solution
Step 1: Understand range condition
To detect values between 200 and 400, sensorValue must be greater than 200 AND less than 400.Step 2: Analyze each option
if(sensorValue < 200 && sensorValue > 400) { Serial.println("Obstacle detected"); }: impossible condition (< 200 && > 400); if(sensorValue >= 200 || sensorValue <= 400) { Serial.println("Obstacle detected"); } uses || which allows all values; if(sensorValue > 200 && sensorValue < 400) { Serial.println("Obstacle detected"); }: > 200 && < 400 correct; if(sensorValue == 200 && sensorValue == 400) { Serial.println("Obstacle detected"); } checks equality to both values simultaneously, impossible.Final Answer:
if(sensorValue > 200 && sensorValue < 400) { Serial.println("Obstacle detected"); } -> Option CQuick Check:
Use AND (&&) for range checks [OK]
- Using || instead of && for range
- Checking impossible conditions
- Using equality instead of range
Ready to prove it?
Pass the test and this pattern moves unknown
