Range Logic, Multiple Sensors & Threshold Windows
Learning Objective
Use AND/OR logic in two ways: test one sensor against a range, or combine conditions from two different sensors. Build stable threshold windows and smarter rules that react only when the right combination of real-world conditions is present.
Theory: Building a Comfortable Zone
The Problem with One Rule
Last week, we built an automatic nightlight. We created a rule to turn it On when it got dark. But to make it turn Off when the sun came back up, we needed a second rule! This second rule is not just convenience — it is essential for stability.
The Danger: Rapid Switching (Jitter)
Imagine if both rules triggered at exactly the same value (e.g., LDR = 50). If the light reading hovers right around 50 — going 49, 51, 50, 48, 52 — the MakerBuddy would rapidly switch the LED on, off, on, off, on, off hundreds of times a second. This is called Jitter and it can damage your components!
The solution is to create a gap between the ON threshold and the OFF threshold. This gap is called a Threshold Window.
Real-World Example: The Smart Incubator
Imagine you are taking care of baby chicks in an incubator. They need to be kept warm, but not too hot.
Notice the 6°C gap between 20 and 26. The heater only turns on when it gets cold (below 20) and only turns off when it gets hot (above 26). Between those two values, nothing changes — the system is stable!
For Teachers
This introduces Hysteresis in a kid-friendly way. Emphasize why having a gap between the ON and OFF threshold is critical. If both rules triggered at exactly 25°C, the heater would rapidly click on and off every millisecond — destroying the machine!
The Built-In Range Logic
Two rules is one way to describe a window. But the Rule Engine can also do it inside a single rule. Look at the bottom of the Loop Condition panel and you will find a checkbox labelled Use Range.
Until you tick it, the extra boxes are greyed out. Tick it and three controls wake up: Range Logic (AND / OR), Operator 2 and Threshold 2. Now your rule tests the sensor twice before deciding whether to run.

Tick Use Range (highlighted) to unlock the second comparison — Range Logic, Operator 2 and Threshold 2.
New: Combine Two Different Sensors
A range asks two questions about one sensor. The Add a second sensor option does something different: it adds a second, complete sensor condition to the same rule. The Rule Engine then combines the two results with AND or OR before deciding whether to run the action steps.
- Configure the first Sensor, Operator and Threshold as usual.
- Tick Add a second sensor. The second-condition panel opens.
- Under How should the two sensor conditions work together?, choose AND or OR.
- Choose the Second sensor, its Operator and its Threshold.
- If the second sensor also needs a window, tick Use second sensor range and configure its second comparison.
- Add the action steps and save the rule. The rule starts and repeats while the final combined result is true. If it becomes false during a cycle, MakerBuddy finishes that cycle and then stops.

Tick Add a second sensor, then choose whether both conditions must be true (AND) or either can be true (OR).

Enable Use second sensor range when the second sensor also needs two comparisons.
AND — both environments must agree
Use AND when every condition is required. A security light should run only when it is dark and motion is detected.
OR — either sensor can trigger
Use OR when either danger is enough. An alarm can run when gas is high or temperature is dangerously high.
(Temperature > 20 AND Temperature < 30) AND (Humidity > 40 AND Humidity < 60) makes one rule run only when both measurements are inside their comfort zones.AND makes a window. OR makes an alarm.
The little AND/OR choice completely changes the meaning of your rule:
AND — inside the window
Both tests must be true at the same time. This describes the values between two numbers.
Runs only while the temperature sits between 20°C and 30°C. Perfect for a "conditions are good" green light.
OR — outside the window
Either test being true is enough. This describes the values outside two numbers.
Runs whenever the temperature is too cold or too hot. Perfect for a single "something is wrong" alarm.
So Which Should You Use?
This is the real decision, and it comes down to how many different actions you need. A single rule has only one list of steps, so it can only do one thing.
| You want to… | Build it as… | Why |
|---|---|---|
| Do one thing while a value sits in a band | One rule, Use Range + AND | Only one action is needed, so one rule is enough — and it is tidier. |
| Do one thing whenever a value leaves a safe band | One rule, Use Range + OR | One alarm covers both the too-low and too-high cases. |
| Do different things at each end (heat vs. cool) | Two separate rules | Two different actions need two different step lists. |
| React to two different sensors | One rule, Add a second sensor + AND/OR | Both sensor results control the same action list, so they belong in one combined rule. |
For Teachers
This is students' first meeting with real boolean logic, and the AND/OR distinction is worth drilling with a physical demonstration before anyone touches the software. Ask two students to stand up only when both of two statements are true, then repeat with either. The "AND gives you the inside, OR gives you the outside" framing is the one that sticks. Watch for the classic error of writing > 30 AND > 20, which looks like a range but is simply > 30.
Activity: The Climate Controller
Let's program our MakerBuddy to maintain a comfortable temperature window using the RGB LED as visual indicators — Red = Heater, Blue = Air Conditioner.
Step-by-Step Instructions:
- Rule 1 — Too Cold (Heater): Create a Condition rule named "TooCold". Sensor → Temperature (DHT11), Operator → <, Threshold → 22. Step 1: Action → RGB Red, Delay → 100 ms. Save.
- Rule 2 — Too Hot (AC): Create a Condition rule named "TooHot". Sensor → Temperature (DHT11), Operator → >, Threshold → 28. Step 1: Action → RGB Blue, Delay → 100 ms. Save.
- The Comfortable Zone: What happens if the temperature is 25°C? Both rules are false, so neither triggers. This is perfect — nothing changes when the environment is already comfortable!
- Test it: Breathe warmly on the DHT11 sensor to raise the temperature and trigger the "AC" rule. Watch the RGB change to blue!
Why two rules here? Because we wanted two different actions — red at one end, blue at the other. One rule could not do both.
Part B — The Same Job with One Rule
Now build a "conditions are perfect" green light using Use Range. This time only one action is needed, so one rule is enough.
- Create a Condition rule named ComfortZone.
- Sensor → Temperature (DHT11), Operator → >, Threshold → 22.
- Tick the Use Range checkbox. The greyed-out boxes become active.
- Range Logic → AND, Operator 2 → <, Threshold 2 → 28.
- Step 1: Action → RGB Green, Delay → 100 ms. Save.
- Test it: at room temperature the light should be green. Breathe on the sensor to push the temperature past 28 and watch the green switch off, because the condition is no longer true.
You have just written Temperature > 22 AND Temperature < 28 — a genuine threshold window in a single rule.
Part C — A Two-Sensor Security Light
Now use the new feature to build the smart-home flowchart from Week 20. The light should turn on only when somebody is present and the room is dark.
- Create a Condition rule named SecurityLight.
- First sensor → Light Level (LDR), Operator → <, Threshold → your measured dark value.
- Tick Add a second sensor and choose Conditions logic → AND.
- Second sensor → Motion Sensor, Operator → ==, Threshold → 1.
- Step 1: Action → LED On, Delay → 100 ms. Save.
- Test all four combinations: bright/no motion, bright/motion, dark/no motion and dark/motion. Only the last combination should run the rule.
Challenge: The Ultimate Nightlight
Fixing Last Week's Bug
Last week, our nightlight turned on in the dark, but it never turned off! Now that you know about Threshold Windows, you can fix it. Build a complete nightlight with two rules:
- Rule 1 (Lights Off!): Sensor → LDR, Operator → <, Threshold → 40. Step: Action → LED On, Delay → 100 ms.
- Rule 2 (Morning!): Sensor → LDR, Operator → >, Threshold → 60. Step: Action → LED Off, Delay → 100 ms.
Temperature < 18 as the first test and Temperature > 30 as the second. Then explain to a partner why this would never work with AND.Coming Up Next Week:
Week 28: Smart Safety Systems: MQ-2 Gas Alerts
