Lesson 52: Logic from words
From words to logic
- Exam questions describe a system in words. You write a logic expression, then a truth table.
- Find the inputs, then the words that join them: and, or, not.
- Here three switches are the sensors and an LED is the alarm.
Words for this lesson
| Word | Meaning |
|---|---|
| input | a sensor value, 0 or 1 |
| logic expression | inputs joined with AND, OR, NOT |
| truth table | the output for every row of inputs |
Words to logic
| In the question | Logic | C++ |
|---|---|---|
| it is dark (L = 0) | NOT L | !L |
| and | AND | && |
| motion or sound or both | M OR S | M || S |
- Three inputs give 2 × 2 × 2 = 8 rows.
In the exam
- 0478/23/M/J/25 Q7(b): the alarm sounds if it is dark and motion or sound or both are detected. (i) Mark scheme: A = NOT L AND (M OR S) (longer correct forms also get the marks). (ii) The truth table: A = 1 only in rows 001, 010 and 011; four marks for eight correct outputs.
Độ khó 3/5 · 200 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
A security system has three inputs. A switch that is on gives 1. L = 1 means light (0 means dark), M = 1 means motion is detected, S = 1 means sound is detected. The alarm on pin 13 sounds if it is đen and motion hoặc sound hoặc both are detected. The program reads L, M and S for you. Write the logic expression in C++ and switch the alarm on or off. Check tries all 8 rows of the truth table.
Nhấn Chạy để xem kết quả ở đây.
Độ khó 4/5 · 280 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Let the Arduino write the truth table. Finish the function alarm(L, M, S) so it returns the alarm output. The loops try every row and print it like 0 1 1 -> 1.
Nhấn Chạy để xem kết quả ở đây.