Skip to content
MakerBuddy

Term 4 · Weeks 25–32

Creating smart automated systems with complex rules, capstone projects and a final expo. Each lesson includes its full classroom material, activity and challenge.

Range Logic, Multiple Sensors & Threshold Windows

Week 27 Term 4 45-60 minutes

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.

Rule 1 (Too Cold):   If Temp < 20°C → Relay On (Heater ON)
Rule 2 (Too Hot):     If Temp > 26°C → Relay Off (Heater OFF)
✅ Comfortable Zone: 20°C — 26°C (heater stays off, no changes)

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.

The Use Range controls in the Rule Engine: Range Logic AND/OR, Operator 2 and Threshold 2

Tick Use Range (highlighted) to unlock the second comparison — Range Logic, Operator 2 and Threshold 2.

Range Logic means one sensor, two tests. Both comparisons in this panel watch the same selected sensor. To compare this result with a different sensor, use the separate Add a second sensor option explained below.

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.

Condition 1: Light Level < 40
Conditions logic: AND
Condition 2: Motion Sensor == 1
Result: (Light < 40) AND (Motion == 1)
  1. Configure the first Sensor, Operator and Threshold as usual.
  2. Tick Add a second sensor. The second-condition panel opens.
  3. Under How should the two sensor conditions work together?, choose AND or OR.
  4. Choose the Second sensor, its Operator and its Threshold.
  5. If the second sensor also needs a window, tick Use second sensor range and configure its second comparison.
  6. 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.
The updated Add a second sensor panel with AND and OR combination options

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

The updated Use second sensor range controls

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.

Dark AND Motion → LED On

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.

Gas High OR Temperature High → Alarm
Power-user pattern: each of the two sensor conditions can have its own range. For example, (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.

Temperature > 20 AND Temperature < 30

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.

Temperature < 20 OR Temperature > 30

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 bandOne rule, Use Range + ANDOnly one action is needed, so one rule is enough — and it is tidier.
Do one thing whenever a value leaves a safe bandOne rule, Use Range + OROne alarm covers both the too-low and too-high cases.
Do different things at each end (heat vs. cool)Two separate rulesTwo different actions need two different step lists.
React to two different sensorsOne rule, Add a second sensor + AND/ORBoth 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:

  1. 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.
  2. 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.
  3. 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!
  4. 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.

  1. Create a Condition rule named ComfortZone.
  2. Sensor → Temperature (DHT11), Operator → >, Threshold → 22.
  3. Tick the Use Range checkbox. The greyed-out boxes become active.
  4. Range Logic → AND, Operator 2 → <, Threshold 2 → 28.
  5. Step 1: Action → RGB Green, Delay → 100 ms. Save.
  6. 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.

  1. Create a Condition rule named SecurityLight.
  2. First sensor → Light Level (LDR), Operator → <, Threshold → your measured dark value.
  3. Tick Add a second sensor and choose Conditions logic → AND.
  4. Second sensor → Motion Sensor, Operator → ==, Threshold → 1.
  5. Step 1: Action → LED On, Delay → 100 ms. Save.
  6. Test all four combinations: bright/no motion, bright/motion, dark/no motion and dark/motion. Only the last combination should run the rule.
Remember: this rule turns the LED on; it does not define the off action. Add a separate rule for the safe/off state when you need a complete installation.

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.
Question: Why did we use 40 and 60 instead of the same number for both rules? What would happen if the LDR read exactly 50 and both rules used 50 as their threshold?
Bonus — the OR alarm: build a single rule that sounds the buzzer whenever the room is at an uncomfortable temperature, either too cold or too hot. Use one Condition rule with Use Range ticked, OR selected, 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