Skip to Content
Getting Started with Arduino
book

Getting Started with Arduino

by Massimo Banzi
February 2009
Beginner
128 pages
2h 45m
English
O'Reilly Media, Inc.
Content preview from Getting Started with Arduino

MATH FUNCTIONS

Arduino includes many common mathematical and trigonometric functions:

min(x, y)

Returns the smaller of x and y.

Example:

val = min(10,20); // val is now 10

max(x, y)

Returns the larger of x and y.

Example:

val = max(10,20); // val is now 20

abs(x)

Returns the absolute value of x, which turns negative numbers into positive. If x is 5 it will return 5, but if x is –5, it will still return 5.

Example:

val = abs(-5); // val is now 5

constrain(x, a, b)

Returns the value of x, constrained between a and b. If x is less than a, it will just return a and if x is greater than b, it will just return b.

Example:

val = constrain(analogRead(0), 0, 255); // reject values bigger than 255

map(value, fromLow, fromHigh, toLow, toHigh)

Maps a value in the range fromLow and maxLow to the range toLow and toHigh. Very useful to process values from analogue sensors.

Example:

val = map(analogRead(0),0,1023,100, 200); // maps the value of
                                          // analog 0 to a value
                                          // between 100 and 200

double pow(base, exponent)

Returns the result of raising a number (base) to a value (exponent).

Example:

double x = pow(y, 32); // sets x to y raised to the 32nd power

double sqrt(x)

Returns the square root of a number.

Example:

double a = sqrt(1138); // approximately 33.73425674438

double sin(rad)

Returns the sine of an angle specified in radians.

Example:

double sine = sin(2); // approximately 0.90929737091

double cos(rad)

Returns the cosine of an angle specified in radians.

Example:

double cosine = cos(2); // approximately -0.41614685058

double ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Getting Started with Arduino, 3rd Edition

Getting Started with Arduino, 3rd Edition

Massimo Banzi, Michael Shiloh
Mastering Arduino

Mastering Arduino

Jon Hoffman, Ejike (George Steve) Ifeanyichukwu, Pratik Desai
Arduino Cookbook

Arduino Cookbook

Michael Margolis

Publisher Resources

ISBN: 9780596155704Errata Page