Logic

 Logic

Programs don't just calculate.

They also need to make decisions.

Questions like:

  • Is the button pressed?
  • Is the temperature too high?
  • Is the distance less than 10 cm?
  • Did the player score enough points?

The Logic category allows programs to ask questions and decide what to do next.

Think of Logic as the decision-maker inside your robot.


What Is Logic?

Logic blocks ask questions.

Every question has one of two answers:

TRUE

or

FALSE

Example:

5 > 3

↓

TRUE


2 > 10

↓

FALSE

 Think of Logic as the brain of your program.


Logic Category

[INSERT CATEGORY IMAGE HERE: Logic]

Figure 3.1 – Blocks available in the Logic category.

The Logic category can be organized into:

COMPARE

   ↓

COMBINE

   ↓

REVERSE

   ↓

DECIDE


Logic Block Quick Reference

BlockPurposeMain Idea
ComparisonCompare valuesAsk a question
Range CheckCheck limitsBetween values
ANDBoth conditionsAll must be true
OREither conditionOne can be true
NOTReverse resultOpposite answer
TRUE/FALSEBoolean valuesYes or No
If True / If FalseChoose a valueDecision making
In / Not InCheck a listExists or not
To BooleanConvert to TRUE/FALSELogical value

Comparing Values

[INSERT BLOCK IMAGE HERE: Comparison]

Comparison blocks ask questions.


Comparison Options

QuestionMeaning
=Equal to
≠Not equal
<Less than
≤Less than or equal
>Greater than
≥Greater than or equal

Examples

5 = 5

↓

 TRUE


5 ≠ 5

↓

 FALSE


10 > 3

↓

 TRUE


Concept

Question

   ↓

TRUE/FALSE


Comparing Sensor Values

A sensor reading can be compared to a limit.

Example:

Temperature > 30

Result:

TRUE

or

FALSE


Concept

Sensor

   ↓

Compare

   ↓

Result


Range Checks

[INSERT BLOCK IMAGE HERE: Range Comparison]

Sometimes a value must stay between two limits.

Example:

0 < Score < 100

This asks:

Is the score between 0 and 100?


Concept

Lower Limit

      ↓

Value

      ↓

Upper Limit


TRUE and FALSE

[INSERT BLOCK IMAGE HERE: Boolean]

The most important Logic values are:

TRUE

and

FALSE


Think of Them As

TRUE = YES

FALSE = NO


Combining Conditions

[INSERT BLOCK IMAGE HERE: AND/OR]

Sometimes one question is not enough.


AND

AND requires both conditions to be true.

Example:

Button Pressed

AND

Distance < 10

Result:

TRUE

only if both are true.


Memory Tip

AND = BOTH


OR

OR requires only one condition to be true.

Example:

Button Pressed

OR

Remote Button Pressed


Memory Tip

OR = EITHER


AND vs OR

BlockMeaning
ANDBoth conditions
OROne or both conditions

Example

Rain

AND

Wind

↓

Need both


Rain

OR

Wind

↓

Need only one


NOT

[INSERT BLOCK IMAGE HERE: NOT]

NOT reverses the answer.


Examples

TRUE

↓

NOT

↓

FALSE


FALSE

↓

NOT

↓

TRUE


Memory Tip

NOT = OPPOSITE


Choosing Between Two Values

[INSERT BLOCK IMAGE HERE: If True / If False]

Sometimes we want one value if something is true and another value if it is false.

Example:

IF Temperature > 30

↓

HOT

ELSE

↓

OK


Concept

Question

    ↓

TRUE?

↙       ↘

Yes      No

↓         ↓

Value A  Value B


Checking a List

[INSERT BLOCK IMAGE HERE: In]

Logic can check if a value exists inside a list.

Example:

3 in [1,2,3,4]

↓

TRUE


8 in [1,2,3,4]

↓

 FALSE


Converting to Boolean

[INSERT BLOCK IMAGE HERE: To Boolean]

This block converts a value into:

TRUE

or

FALSE


Concept

Value

   ↓

Boolean

   ↓

TRUE/FALSE


Logic and Control

Logic and Control work together.

Logic asks the question.

Control decides what to do.

Example:

Distance < 10

↓

TRUE

↓

Turn LED ON


Concept

QUESTION

    ↓

TRUE/FALSE

    ↓

ACTION


Logic and Sensors

Logic becomes most useful when used with sensors.

Example:

DHT11

   ↓

Temperature

   ↓

Compare

   ↓

Decision


Temperature Alarm

Temperature > 30

↓

TRUE

↓

Turn Buzzer ON

Obstacle Detector

Distance < 10

↓

TRUE

↓

Stop Robot

Temperature Warning System

Temperature

    ↓

> 30?

    ↓

TRUE

    ↓

LED ON

Skills:

 Comparison

 Sensors

 Decisions

Obstacle Detector

Distance

    ↓

< 10?

    ↓

TRUE

    ↓

Buzzer

Skills:

 Ultrasonic Sensor

 Logic

 Outputs

Score Checker

Score

   ↓

>= 10?

   ↓

TRUE

   ↓

Winner!

Skills:

 Variables

 Comparisons

 Control


Quick Guide for Students

Need to compare values?

Use:

=  ≠  <  >  ≤  ≥

Need both conditions?

Use:

AND

Need only one condition?

Use:

OR

Need the opposite answer?

Use:

NOT

Need TRUE or FALSE?

Use:

Boolean

Need to check limits?

Use:

Range Comparison


Quick Reference

FamilyMain Idea
ComparisonAsk a question
RangeCheck limits
ANDBoth conditions
OREither condition
NOTOpposite result
BooleanTRUE/FALSE
Conditional ValueChoose between values
MembershipCheck a list
Boolean ConversionConvert to TRUE/FALSE

Summary

In this lesson, you learned how Logic helps programs make decisions.

You explored:

 Comparisons

 TRUE and FALSE

 AND

 OR

 NOT

 Range Checks

 Conditional Values

 Membership Tests

   Sensor Decisions

Logic helps programs decide what to do based on information they receive from sensors, variables, and user input.


Key Takeaway

Logic is the decision-maker inside your program. It asks questions, produces TRUE or FALSE answers, and helps your robot decide what action to take next.