Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logic Lab mini-course #1072

Merged
merged 4 commits into from Jul 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion docs/courses.md
Expand Up @@ -25,9 +25,14 @@ Courses on coding the @boardname@ with hand-on experiments.
"description": "A series of tutorials about using the pin inputs and outputs, includes experiments.",
"url":"/learnsystem/pins-tutorial",
"imageUrl":"/static/cp/learn/pins-tutorial/pins-tutorial.png"
},{
"name": "Logic Lab",
"description": "Learn the basics of logic and conditional expressions.",
"url":"/learnsystem/logic-lab",
"imageUrl":"/static/cp/learn/logic-lab/logic-lab.png"
}]
```

## See Also

[Maker](/courses/maker), [Using the Pins](/learnsystem/pins-tutorial)
[Maker](/courses/maker), [Using the Pins](/learnsystem/pins-tutorial), [Logic Lab](/learnsystem/logic-lab)
15 changes: 15 additions & 0 deletions docs/learnsystem/logic-lab.md
@@ -0,0 +1,15 @@
# Logic Lab

![Logic lab header image](/static/cp/learn/logic-lab/logic-lab-header.jpg)

A basic aspect of knowledge and understanding is whether something is true or not. Considering conditions around you and making a conclusion about something being true or false means that you are using logic. Computers and, in fact, all of digital electronics rely on this idea of logic to process information and give results in terms of conditions being true or false. Logic is used almost everywhere in the programs you write in places where you want decide to do one task or another.

## Logic topics

These topic sections teach you about applying logic to conditions and using Boolean algebra to express them. Also, you will see how logical operators work and how they combine to form more complex logic structures.

* [Logic and Expressions](/learnsystem/logic-lab/expressions)
* [Boolean Elements](/learnsystem/logic-lab/elements)
* [Logic Explorer](/learnsystem/logic-lab/explorer)
* [Logic Gates](/learnsystem/logic-lab/logic-gates)
* Programmable Logic
207 changes: 207 additions & 0 deletions docs/learnsystem/logic-lab/elements.md
@@ -0,0 +1,207 @@
# Boolean elements

Whether creating equations in Boolean algebra or using them in your programs, you'll form both simple and complex logical expressions that use basic operations to combine the logical conditions.

## Notation

Boolean (logical) equations are expressed in a way similar to mathmatical equations. Variables in Boolean expressions though, have only two possible values, ``true`` or ``false``. For an equation using a logical expression, the equivalant sides of the equal sign ,``=``, will be only ``true`` or ``false`` too.

The following list shows the basic notation elements for Boolean expressions.

* ``~A``: the inverse (**NOT**) of ``A``, when ``A`` is ``true``, ``~A`` is ``false``
* ``A + B``: the value of ``A`` **OR** ``B``
* ``A · B``: the value of ``A`` **AND** ``B``
* ``A ⊕ B``: the value of the exclusive OR (**XOR**) of ``A`` with ``B``
* ``Q``: equivalent result (OUTPUT) value of a logical expression

A resulting value, ``Q``, from a logical expression in is shown like:

``Q`` = ``A + B``

An equation to show logically equivalent expressions (where both sides have the same resulting value) can look like this:

``~(A + B)`` = ``~A · ~B``

## Logical operators

All Boolean expressions result from a combination of conditions and operators. These operators join individual conditons together and evaluate into a single ``true`` or ``false`` condition. The following are the basic logical operators. Their use in both Boolean algebra and in code is shown along with their truth table.

### Identity

Identity means that a result value is the same as the condition itself.

``Q = A``

```block
let A = false
let Q = A
```

#### Example - Blink pixels on press

```blocks
let A = false
forever(function () {
A = input.buttonA.isPressed()
if (A) {
light.setAll(0xff0000)
} else {
light.clear()
}
pause(500)
})
```

#### Truth table

A | A
-|-
F | F
T | T

### NOT (Negation)

The NOT operator is called negation or the inverse. It takes a single logical value and makes it have the opposite value, ``true`` goes to ``false`` and ``false`` goes to ``true``.

``Q = ~A``

```block
let A = false
let Q = !A
```

#### Example - Blink pixels on not pressed

```blocks
let A = false
forever(function () {
A = input.buttonA.isPressed()
if (!(A) {
light.setAll(0xff0000)
} else {
light.clear()
}
pause(500)
})
```

#### Truth table

A | ~A
-|-
F | T
T | F

### OR (Disjunction)

The OR operator results in ``true`` when one or more conditions are ``true``.

``Q = A + B``

```block
let A = false
let B = false
let Q = A || B
```

#### Example - Blink on any press

```blocks
let A = false
let B = false
forever(function () {
A = input.buttonA.isPressed()
B = input.buttonB.isPressed()
if (A || B) {
light.setAll(0xff0000)
} else {
light.clear()
}
pause(500)
})
```

#### Truth table

A | B | A + B
-|-|-
F | F | F
T | F | T
F | T | T
T | T | T

### AND (Conjunction)

The AND operator requires that all conditions are ``true`` for the result to be ``true``.

``Q = A · B``

```block
let A = false
let B = false
let Q = A && B
```

#### Example - Blink on double press only

```blocks
let A = false
let B = false
forever(function () {
A = input.buttonA.isPressed()
B = input.buttonB.isPressed()
if (A && B) {
light.setAll(0xff0000)
} else {
light.clear()
}
pause(500)
})
```

#### Truth table

A | B | A · B
-|-|-
F | F | F
T | F | F
F | T | F
T | T | T

### XOR (Exclusive OR) #xor

Exclusive OR (XOR) means that only one or the other condition is true. Both conditions can't be true at the same time. XOR is common in Boolean algebra but it has no operator in JavaScript. Its operation can be made from combining a few simple expressions.

``Q = A ⊕ B``

```block
let A = false
let B = false
let Q = (A || B) && !(A && B)
```

#### Example - Blink on one press or the other

```blocks
let A = false
let B = false
forever(function () {
A = input.buttonA.isPressed()
B = input.buttonB.isPressed()
if ((A || B) && !(A && B)) {
light.setAll(0xff0000)
} else {
light.clear()
}
pause(500)
})
```

#### Truth table

A | B | A ⊕ B
-|-|-
F | F | F
T | F | T
F | T | T
T | T | F