Lesson 35: Security alarm
Logic that protects a shop
- A security alarm has several sensors: light, motion and sound.
- It must not sound in the day when customers walk around. At night any motion or sound is an intruder.
- The rule is a logic expression: the alarm sounds when it is dark AND (there is motion OR a sound).
Words for this lesson
| Word | Meaning |
|---|---|
| intruder | a person who comes in without permission |
| logic expression | a rule made of AND, OR and NOT |
| bool | a variable that is true or false |
| truth table | every input pattern and the output for each one |
Logic in C++
| Logic gate | C++ | True when |
|---|---|---|
| NOT | !a |
a is false |
| AND | a && b |
both are true |
| OR | a || b |
at least one is true |
- Brackets work as in maths:
!light && (motion || sound)does the OR first.
In the exam
- 0478/23/M/J/25 Q7(b): a security system sounds an alarm (A = 1) if it is dark and if motion or sound or both are detected. L = 0 means dark, M = 1 motion, S = 1 sound. (i) Write the logic expression: A = NOT L AND (M OR S). (ii) Complete the truth table: it is the table your second task prints, with 8 rows from 0 0 0 to 1 1 1.
- 0478/12/F/M/21 Q5(b): an aeroplane warning system (W) sounds when it is low and descending, or descending and too fast. Draw the logic circuit: one AND gate for each condition, and an OR gate to join them.
Сложность 4/5 · 280 монет + небольшой бонус за первое завершение сегодня
A shop alarm sounds only at night: when it is dark AND (there is motion OR a sound). The light sensor is on A0 (more than 500 means light). The motion sensor is the button on pin 2 and the sound sensor is the button on pin 3 (pressed reads LOW). Make three bool variables, then sound the alarm on pin 8 with tone(8, 2000) when !light && (motion || sound), else noTone(8).
Нажмите Запустить, чтобы увидеть результат здесь.
Сложность 4/5 · 280 монет + небольшой бонус за первое завершение сегодня
Print the truth table of the alarm in the Serial Monitor. Use three for loops, one inside the other, so that L, M and S each go from 0 to 1. For each row print L, M, S and A = !L && (M || S), with a space between them, for example 0 1 0 1. Print the heading L M S A first.
Нажмите Запустить, чтобы увидеть результат здесь.