Math

Math

Numbers are everywhere in programming.

Programs use numbers to:

  • Count points
  • Measure temperature
  • Control LEDs
  • Move servos
  • Measure distance
  • Create random events

The Math category gives your program the ability to calculate, compare, transform, and control values.

Think of Math as the calculator inside your robot.


What Is Math?

Math blocks allow a program to work with numbers.

Examples:

5 + 3 = 8

10 - 2 = 8

4 × 2 = 8

16 ÷ 2 = 8

Math helps programs transform numbers into useful information.

Think of Math as the calculator inside your project.


Math Category

[INSERT CATEGORY IMAGE HERE: Math]

Figure 4.1 – Blocks available in the Math category.

The category can be organized into:

NUMBERS

   ↓

CALCULATE

   ↓

RANDOM

   ↓

LIMIT

   ↓

MAP


Math Block Quick Reference

BlockPurposeMain Idea
NumberCreate a valueStarting number
Add (+)Add valuesBigger number
Subtract (-)Take awaySmaller number
Multiply (×)Scale valuesRepeat groups
Divide (÷)Split valuesShare equally
Random IntegerRandom whole numberSurprise value
Random DecimalRandom decimal valueMore precise randomness
RoundSimplify decimalsCleaner number
MaxLargest valueBiggest wins
MinSmallest valueSmallest wins
ConstrainSet limitsStay in range
MapChange rangesScale values
Convert to IntegerRemove decimalsWhole number
Constantsπ and eSpecial math values
Variable MathChange stored valuesCounters and scores

Numbers

Everything starts with a number.

Examples:

0

10

50

100

Numbers can be connected to almost every category.

Examples:

Number → Math

Number → Logic

Number → Variables

Number → Servo

Number → Sensor Threshold


Basic Calculations

[INSERT BLOCK IMAGE HERE: Arithmetic Operations]

The most important Math blocks are:

OperationExample
Add5 + 3
Subtract10 - 2
Multiply4 × 2
Divide12 ÷ 3

Concept

Number

    +

Number

      ↓

Result


Example

Temperature

      +

5

↓

New Temperature


Updating Values

[INSERT BLOCK IMAGE HERE: Variable Math]

Sometimes a value changes over time.

Example:

score += 1

Meaning:

Current Score

      +

1

      ↓

New Score

Useful for:

 Counters

 Scores

 Timers

 Tracking events


Random Numbers

[INSERT BLOCK IMAGE HERE: Random Integer]

Programs can create random values.


Possible results:

12

57

98


Uses

 Games

 Random challenges

 Simulations

 Surprise effects


Rounding Numbers

[INSERT BLOCK IMAGE HERE: Round]

Sometimes numbers have many decimal places.

Example:

4.83

↓

Round

↓

5

round(4.83)

Maximum and Minimum

Programs often compare values.

Maximum

max(10,20)

Result:

20

Minimum

min(10,20)

Result:

10


Easy Memory Tip

MAX = Largest

MIN = Smallest


Constrain

[INSERT BLOCK IMAGE HERE: Constrain]

Constrain keeps values inside limits.

Example:

Range:

0 → 100

Value:

120

Result:

100


Concept

Value

   ↓

Limit

   ↓

Allowed Value


Constrain sets boundaries.


Map

[INSERT BLOCK IMAGE HERE: Map]

Map converts a value from one range into another.

Example

Sensor Range:

0 → 100

Servo Range:

0 → 180

Concept

Old Range

     ↓

Map

     ↓

New Range


math_map(50,1,100,1,180)

Map changes scale.


Math and Sensors

Math becomes powerful when combined with sensors.

Example:

Temperature Sensor

      ↓

Reading

      ↓

Math

      ↓

Display

Another example:

Distance Sensor

      ↓

Map

      ↓

Servo Position


Math and Logic

Math and Logic often work together.

Math:

5 + 3

↓

8

Logic:

8 > 5

↓

TRUE

Programs often follow:

Sensor

   ↓

Math

   ↓

Logic

   ↓

Action


 Soccer Score Counter

Concept:

Goal

   ↓

Score + 1

   ↓

LCD

Skills:

 Variables

 Addition

 Counting


 Temperature Converter

Concept:

Temperature Sensor

      ↓

Math

      ↓

New Value

      ↓

LCD

Skills:

 Sensors

 Arithmetic

 Displays


 LED Brightness Controller

Concept:

Sensor

   ↓

Map

   ↓

LED Brightness

Skills:

 Mapping

 Sensor Values

 Outputs


 Distance Controlled Servo

Concept:

Ultrasonic Sensor

       ↓

Distance

       ↓

Map

       ↓

Servo Position

Skills:

 Sensors

 Mapping

 Servos


Quick Guide for Students

Need a calculation?

Use:

+  -  ×  ÷


Need a random value?

Use:

Random

Need a clean whole number?

Use:

Round

Need the largest value?

Use:

Max

Need the smallest value?

Use:

Min

Need limits?

Use:

Constrain

Need to convert one range into another?

Use:

Map


Quick Reference

FamilyMain Idea
NumberCreate a value
ArithmeticCalculate
Variable MathUpdate values
RandomGenerate values
RoundSimplify numbers
MaxLargest value
MinSmallest value
ConstrainKeep values within limits
MapChange ranges

Summary

In this lesson, you learned how Math helps programs work with numbers.

You explored:

 Numbers

 Addition

 Subtraction

 Multiplication

 Division

 Variables

 Random Numbers

 Rounding

 Maximum and Minimum

 Constrain

 Map

Math helps transform raw numbers into useful information that can control displays, sensors, motors, LEDs, and robots.


Key Takeaway

Math is the calculator inside your program. It helps turn numbers into useful information that can control sensors, displays, LEDs, servos, and entire robotics projects.