Automated plant watering system in Raspberry Pi - Time & Space Complexity
When building an automated plant watering system, it is important to understand how the time it takes to check and water plants grows as you add more plants.
We want to know how the program's work changes when the number of plants increases.
Analyze the time complexity of the following code snippet.
plants = ["plant1", "plant2", "plant3", "plant4"]
for plant in plants:
moisture = read_moisture_sensor(plant)
if moisture < threshold:
water_plant(plant)
This code checks each plant's soil moisture and waters it if the soil is dry.
- Primary operation: Looping through each plant in the list.
- How many times: Once for every plant in the list.
As the number of plants increases, the program checks each one once, so the work grows steadily.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 moisture checks and possible watering actions |
| 100 | About 100 moisture checks and possible watering actions |
| 1000 | About 1000 moisture checks and possible watering actions |
Pattern observation: The work grows directly in proportion to the number of plants.
Time Complexity: O(n)
This means the time to check and water plants grows in a straight line as you add more plants.
[X] Wrong: "The program only takes a fixed amount of time no matter how many plants there are."
[OK] Correct: Because the program must check each plant, more plants mean more work and more time.
Understanding how your program scales with more plants shows you can think about efficiency, which is a useful skill for many real-world projects.
"What if we added a nested loop to check each plant multiple times per cycle? How would the time complexity change?"
Practice
What is the main purpose of an automated plant watering system using a Raspberry Pi?
Solution
Step 1: Understand the system goal
The system uses sensors to check soil moisture.Step 2: Identify the watering condition
It waters plants only when soil is dry to save water.Final Answer:
To water plants only when the soil is dry -> Option DQuick Check:
Water only if dry = A [OK]
- Thinking it waters plants all the time
- Confusing soil moisture with sunlight
- Assuming it controls temperature
Which of the following Python code snippets correctly sets up a GPIO pin 17 as an output to control a water pump?
import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) # What goes here?
Solution
Step 1: Identify pin mode setup
GPIO.setup(pin_number, GPIO.OUT) sets pin as output.Step 2: Choose correct function and mode
GPIO.setup(17, GPIO.OUT) correctly sets pin 17 as output.Final Answer:
GPIO.setup(17, GPIO.OUT) -> Option AQuick Check:
Setup output pin = A [OK]
- Using GPIO.IN instead of GPIO.OUT for output pins
- Calling GPIO.output instead of GPIO.setup to set mode
- Confusing pin numbers or modes
What will be the output of this Python code controlling a pump based on soil moisture sensor reading?
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
soil_moisture = 300 # sensor value
if soil_moisture < 400:
GPIO.output(17, GPIO.HIGH)
print("Pump ON")
else:
GPIO.output(17, GPIO.LOW)
print("Pump OFF")Solution
Step 1: Check soil moisture condition
Value 300 is less than 400, so condition is true.Step 2: Determine pump action and output
GPIO.output sets pin HIGH and prints "Pump ON".Final Answer:
Pump ON -> Option CQuick Check:
300 < 400 triggers Pump ON [OK]
- Misreading the comparison operator
- Confusing HIGH and LOW states
- Ignoring print output
Identify the error in this Raspberry Pi watering system code snippet:
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
soil_moisture = 500
if soil_moisture > 400
GPIO.output(17, GPIO.LOW)
print("Pump OFF")Solution
Step 1: Check if statement syntax
Python requires a colon (:) after the if condition.Step 2: Identify missing colon error
The code misses ':' after 'if soil_moisture > 400', causing syntax error.Final Answer:
Missing colon (:) after if condition -> Option BQuick Check:
if statement needs ':' = D [OK]
- Forgetting colon after if
- Confusing GPIO.output with GPIO.setup
- Assuming pin number is wrong
You want to improve the watering system to water plants only if soil moisture is below 400 and only between 6 AM and 8 AM. Which Python code snippet correctly adds this time condition?
import datetime
now = datetime.datetime.now()
soil_moisture = 350
if soil_moisture < 400 and ???:
print("Watering plants")
else:
print("No watering")Solution
Step 1: Understand time range condition
We want hours from 6 to 8 inclusive, so hour >= 6 and hour <= 8.Step 2: Combine soil moisture and time conditions
Use 'and' to check both soil moisture and time range.Final Answer:
now.hour >= 6 and now.hour <= 8 -> Option AQuick Check:
Check hour between 6 and 8 = B [OK]
- Using 'or' instead of 'and' for time range
- Using strict inequalities excluding 6 or 8
- Checking only exact hours 6 or 8
Ready to prove it?
Pass the test and this pattern moves unknown
