Time & Math Library
Abdul Alim Shakir
Topics
1. delay() 11. abs()
2. delayMicroseconds() 12. pow()
3. millis() 13. sqrt()
4. micros() 14. constrain()
5. min() 15. Bits
6. max() 16. Bytes
7. map() 17. Simple programs
8. random()
9. randomseed()
10. Trigonometric functions
delay()
Pauses the program for the amount of time in milliseconds
Syntax
delay(ms)
Parameters :
ms : the number of milliseconds to pause
No other reading of sensors, mathematical calculations or pin manipulation
can go on during the delay() function. So knowledgeable programmers try to
avoid this delay() function.
delayMicroseconds()
Pauses the program for the amount of time in microseconds.
Syntax
delayMicroseconds(ms)
Parameters :
ms : the number of microseconds to pause
For delays longer than a few thousand microseconds, delay() function should
be used.
It will not work precisely for smaller delay times.
millis()
Returns the number of milliseconds since the Arduino board began running
the current program.
Syntax
Time = millis()
The data type of Time variable must be unsigned long.
It can be useful for approximately 50 days. So after that the value will
initialize to zero.
Using millis(), we can avoid delay() function.
micros()
Returns the number of microseconds since the Arduino board began running
the current program.
Syntax
Time = micros()
The data type of Time variable must be unsigned long.
It can be useful for approximately 70 minutes. So after that the value will
initialize to zero.
min()
Calculates the minimum of two numbers.
Syntax
min(x,y)
Parameters
x: the first number, any data type
y: the second number, any data type
Returns
The smaller of the two numbers.
max()
Calculates the maximum of two numbers.
Syntax
max(x,y)
Parameters
x: the first number, any data type
y: the second number, any data type
Returns
The larger of the two numbers.
map()
Re-maps a number from one range to another.
Syntax
map(value, fromLow, fromHigh, toLow, toHigh)
Parameters
value: the number to map
fromLow: the lower bound of the value’s current range
fromHigh: the upper bound of the value’s current range
toLow: the lower bound of the value’s target range
toHigh: the upper bound of the value’s target range
Returns
The mapped value.
random()
The random function generates pseudo-random numbers.
Syntax
random(max)
random(min, max)
Parameters
min - lower bound of the random value, inclusive (optional)
max - upper bound of the random value, exclusive
Returns
A random number between min and max-1 (long).
randomSeed()
Initializes the pseudo-random number generator, causing it to start at an
arbitrary point in its random sequence.
Syntax
randomSeed(seed)
Parameters
seed - number to initialize the pseudo-random sequence (unsigned long).
Returns nothing.
It is better to use randomSeed() to initialize the random number generator
with a fairly random input, such as analogRead() on an unconnected pin.
Trigonometric Functions
Calculates the sine, cosine or tangent value of an angle.
Syntax
sin(rad)
cos(rad)
tan(rad)
Parameters
rad: The angle in Radians (float).
Returns
The corresponding sine, cosine or tangent of the angle (double).
abs()
Calculates the absolute value of a number.
Syntax
abs(number)
Parameters
number : an integer or float
Returns
x: if x is greater than or equal to 0.
-x: if x is less than 0.
pow()
Calculates the value of a number raised to a power.
Syntax
pow(base,exponent)
Parameters
base: the number (float)
exponent: the power to which the base is raised (float)
Returns
The result of the exponentiation(double).
sqrt()
Calculates the square root of a number.
Syntax
sqrt(number)
Parameters
number : all data types
Returns
The number’s square root(double).
constrain()
Constrains a number to be within a range.
Syntax
constrain(x,a,b)
Parameters
x: the number to constrain, all data types
a: the lower end of the range, all data types
b: the upper end of the range, all data types
Returns
x: if x is between a and b
a: if x is less than a
b: if x is greater than b
Bits
bitRead : Reads a bit of a number.
bitWrite : Writes a bit of a numeric variable.
Syntax
bitRead(x,n)
bitWrite(x,n,b)
Parameters
x: the numeric variable to which to write
n: bit of the number, starting at 0 for the least-significant (rightmost) bit
b: the value to write to the bit (0 or 1)
Byte
lowByte : Extracts the low-order (rightmost) byte of a variable.
highByte : Extracts the high-order (leftmost) byte of a variable.
Syntax
lowByte(value)
highByte(value)
Parameters
value : a vaule of any type
Returns
byte
Now you have the power to
write programs…