Arduino Function Libraries

The Arduino environment can be extended through the use of libraries, just like most programming platforms. Libraries provide extra functionality for use in sketches, e.g. working with hardware or manipulating data.

 Arduino – I/O Functions

Pins Configured as INPUT

Syntax:

void setup()
{
pinMode(3,INPUT) ;   // set pin to input without using built in pull up resistor
}
void setup()
{
pinMode(5,INPUT_PULLUP) ;   // set pin to input using built in pull up resistor
}

Pins Configured as OUTPUT

void setup()

{
pinMode(5,OUTPUT) ;   // set pin to input using built in pull up resistor
}

digitalWrite() Function

The digitalWrite() function is used to write a HIGH or a LOW value to a digital pin. If the pin has been configured as an OUTPUT with pinMode(), its voltage will be set to the corresponding value: 5V (or 3.3V on 3.3V boards) for HIGH, 0V (ground) for LOW. If the pin is configured as an INPUT, digitalWrite() will enable (HIGH) or disable (LOW) the internal   pullup   on   the   input   pin.   It   is   recommended   to   set   the pinMode() to INPUT_PULLUP to enable the internal pull-up resistor.

Syntax:

Void loop()
{
digitalWrite (10,HIGH);
digitalWrite (11,LOW);
}

analogRead( ) function

Arduino is able to detect whether there is a voltage applied to one of its pins and report it through the digitalRead() function. There is a difference between an on/off sensor (which detects the presence of an object) and an analog sensor, whose value continuously changes. In order to read this type of sensor, we need a different type of pin.

By using the analogRead() function, we can read the voltage applied to one of the pins. This function returns a number between 0 and 1023, which represents voltages between 0 and 5 volts. For example, if there is a voltage of 2.5 V applied to pin number 0, analogRead(0) returns 512.

analogRead(analog_pin);
Arduino – Advanced I/O Function

analogReference() Function

Configures the reference voltage used for analog input (i.e. the value used as the top of the input range). The options are:

DEFAULT: The default analog reference of 5 volts (on 5V Arduino boards) or 3.3 volts (on 3.3V Arduino boards)

INTERNAL: An built-in reference, equal to 1.1 volts on the ATmega168 or ATmega328 and 2.56 volts on the ATmega8 (not available on the Arduino Mega)

INTERNAL1V1: A built-in 1.1V reference (Arduino Mega only)

INTERNAL2V56: A built-in 2.56V reference (Arduino Mega only)

EXTERNAL: The voltage applied to the AREF pin (0 to 5V only) is used as the reference.

Syntax:

analogReference (type);
Arduino – Character Functions

All data is entered into computers as characters, which includes letters, digits and various special symbols.

The following table summarizes the functions of the character-handling library. When using functions from the character-handling library, include the <cctype> header.

int isdigit( int c )
Returns 1 if c is a digit and 0 otherwise.

int isalpha( int c )
Returns 1 if c is a letter and 0 otherwise.

int isalnum( int c )
Returns 1 if c is a digit or a letter and 0 otherwise.

int isxdigit( int c )
Returns 1 if c is a hexadecimal digit character and 0 otherwise.

int islower( int c )
Returns 1 if c is a lowercase letter and 0 otherwise.

int isupper( int c )
Returns 1 if c is an uppercase letter; 0 otherwise.

int isspace( int c )
Returns 1 if c is a white-space character—newline (‘\n’), space(‘ ‘), form feed (‘\f’), carriage return (‘\r’), horizontal tab (‘\t’),or vertical tab (‘\v’)—and 0 otherwise.

int iscntrl( int c )
Returns 1 if c is a control character, such as newline (‘\n’), formfeed (‘\f’), carriage return (‘\r’), horizontal tab (‘\t’), vertical tab (‘\v’), alert (‘\a’), or backspace (‘\b’)—and 0 otherwise.

int ispunct( int c )
Returns 1 if c is a printing character other than a space, a digit, or a letter and 0 otherwise.

int isprint( int c )
Returns 1 if c is a printing character including space (‘ ‘) and 0 otherwise.

int isgraph( int c )
Returns 1 if c is a printing character other than space (‘ ‘) and 0 otherwise.

Example:

void setup ()
{
 Serial.begin (9600);
 Serial.print ("According to isdigit:\r");
Serial.print (isdigit( '8' ) ? "8 is a": "8 is not a");
Serial.print (" digit\r" );
Serial.print (isdigit( '8' ) ?"# is a": "# is not a") ;
Serial.print (" digit\r");
Serial.print ("\rAccording to isalpha:\r" );
Serial.print (isalpha('A' ) ?"A is a": "A is not a");
Serial.print (" letter\r");
Serial.print (isalpha('A' ) ?"b is a": "b is not a");
Serial.print (" letter\r");
Serial.print (isalpha('A') ?"& is a": "& is not a");
Serial.print (" letter\r");
Serial.print (isalpha( 'A' ) ?"4 is a":"4 is not a");
Serial.print (" letter\r");
Serial.print ("\rAccording to isalnum:\r");
Serial.print (isalnum( 'A' ) ?"A is a" : "A is not a" ); 
Serial.print (" digit or a letter\r" );
Serial.print (isalnum( '8' ) ?"8 is a" : "8 is not a" ) ;
Serial.print (" digit or a letter\r");
Serial.print (isalnum( '#' ) ?"# is a" : "# is not a" );
Serial.print (" digit or a letter\r");
Serial.print ("\rAccording to isxdigit:\r");
Serial.print (isxdigit( 'F' ) ?"F is a" : "F is not a" );
Serial.print (" hexadecimal digit\r" );
Serial.print (isxdigit( 'J' ) ?"J is a" : "J is not a" ) ;
Serial.print (" hexadecimal digit\r" );
 Serial.print (isxdigit( '7' ) ?"7 is a" : "7 is not a" ) ;
}
Serial.print (" hexadecimal digit\r" );
Serial.print (isxdigit( '$' ) ? "$ is a" : "$ is not a" );

Serial.print (" hexadecimal digit\r" );
Serial.print (isxdigit( 'f' ) ? “f is a" : "f is not a");
Serial.print (" hexadecimal digit\r" );
}
void loop ()
{
}

Result:

According to isdigit:
8 is a digit
# is not a digit
According to isalpha:
A is a letter
b is a letter
& is not a letter
4 is not a letter
According to isalnum:
A is a digit or a letter
8 is a digit or a letter
# is not a digit or a letter
According to isxdigit:
F is a hexadecimal digit
J is not a hexadecimal digit
7 is a hexadecimal digit
$ is not a hexadecimal digit
f is a hexadecimal digit

Example 2:

int thisChar = 0xA0;
void setup ()
{
 Serial.begin (9600);
 Serial.print ("According to islower:\r") ;
Serial.print (islower( 'p' ) ? "p is a" : "p is not a" );
Serial.print ( " lowercase letter\r" );
Serial.print ( islower( 'P') ? "P is a" : "P is not a") ;
Serial.print ("lowercase letter\r");
Serial.print (islower( '5' ) ? "5 is a" : "5 is not a" );
Serial.print ( " lowercase letter\r" );
Serial.print ( islower( '!' )? "! is a" : "! is not a") ;
Serial.print ("lowercase letter\r");
Serial.print ("\rAccording to isupper:\r") ;
Serial.print (isupper ( 'D' ) ? "D is a" : "D is not an" );
Serial.print ( " uppercase letter\r" );
Serial.print ( isupper ( 'd' )? "d is a" : "d is not an") ;
Serial.print ( " uppercase letter\r" );
Serial.print (isupper ( '8' ) ? "8 is a" : "8 is not an" );
Serial.print ( " uppercase letter\r" );
Serial.print ( islower( '$' )? "$ is a" : "$ is not an") ;
Serial.print ("uppercase letter\r ");
}
void setup ()
{
}

Result:

According to islower:
p is a lowercase letter
P is not a lowercase letter
5 is not a lowercase letter
! is not a lowercase letter
According to isupper:
D is an uppercase letter
d is not an uppercase letter
8 is not an uppercase letter
$ is not an uppercase letter

Example 3:

void setup ()
{
 Serial.begin (9600);
 Serial.print ( " According to isspace:\rNewline ") ;
Serial.print (isspace( '\n' )? " is a" : " is not a" );
Serial.print ( " whitespace character\rHorizontal tab") ;
Serial.print (isspace( '\t' )? " is a" : " is not a" );
Serial.print ( " whitespace character\n") ;
Serial.print (isspace('%')? " % is a" : " % is not a" );
Serial.print ( " \rAccording to iscntrl:\rNewline") ;
Serial.print ( iscntrl( '\n' )?"is a" : " is not a" ) ;
Serial.print (" control character\r");
Serial.print (iscntrl( '$' ) ? " $ is a" : " $ is not a" );
Serial.print (" control character\r");
Serial.print ("\rAccording to ispunct:\r");
Serial.print (ispunct(';' ) ?"; is a" : "; is not a" ) ;
Serial.print (" punctuation character\r");
Serial.print (ispunct('Y' ) ?"Y is a" : "Y is not a" ) ;
Serial.print ("punctuation character\r");
Serial.print (ispunct('#' ) ?"# is a" : "# is not a" ) ;
Serial.print ("punctuation character\r");
Serial.print ( "\r According to isprint:\r");
Serial.print (isprint('$' ) ?"$ is a" : "$ is not a" );
Serial.print (" printing character\rAlert ");
Serial.print (isprint('\a' ) ?" is a" : " is not a" );
Serial.print (" printing character\rSpace "); 
Serial.print (isprint(' ' ) ?" is a" : " is not a" ); 
Serial.print (" printing character\r");
Serial.print ("\r According to isgraph:\r");
Serial.print (isgraph ('Q' ) ?"Q is a" : "Q is not a" );
Serial.print ("printing character other than a space\rSpace ");
Serial.print (isgraph (' ') ?" is a" : " is not a" );
Serial.print ("printing character other than a space ");
}
void loop ()
{
}

Result:

According to isspace:
Newline is a whitespace character
Horizontal tab is a whitespace character
% is not a whitespace character
According to iscntrl:
Newline is a control character
$ is not a control character
According to ispunct:
; is a punctuation character
Y is not a punctuation character
# is a punctuation character
According to isprint:
$ is a printing character
Alert is not a printing character
Space is a printing character
According to isgraph:
Q is a printing character other than a space
Space is not a printing character other than a space
Arduino – Time Functions

delay()

Syntax:

delay(ms);  // ms is the time in milliseconds to pause (unsigned long).

delayMicroseconds()

Syntax:

delayMicroseconds(us);  // us is the number of microseconds to pause (unsigned int)

millis()

millis();  // This function returns milliseconds from the start of the program.

micros()

micros();  // This function returns number of microseconds since the program started (unsigned long)

 

Arduino – Math Library

The Arduino Math library (math.h) includes a number of useful mathematical functions for manipulating floating-point numbers.

Macro Definition Documentation

#define acosf   acos
The alias for acos().

#define asinf   asin
The alias for asin().

#define atan2f   atan2
The alias for atan2().

#define atanf   atan
The alias for atan().

#define cbrtf   cbrt
The alias for cbrt().

#define ceilf   ceil
The alias for ceil().

#define copysignf   copysign
The alias for copysign().

#define cosf   cos
The alias for cos().

#define coshf   cosh
The alias for cosh().

#define expf   exp
The alias for exp().

#define fabsf   fabs
The alias for fabs().

#define fdimf   fdim
The alias for fdim().

#define floorf   floor
The alias for floor().

#define fmaf   fma
The alias for fma().

#define fmaxf   fmax
The alias for fmax().

#define fminf   fmin
The alias for fmin().

#define fmodf   fmod
The alias for fmod().

#define frexpf   frexp
The alias for frexp().

#define hypotf   hypot
The alias for hypot().

#define INFINITY   __builtin_inf()
INFINITY constant.

#define isfinitef   isfinite
The alias for isfinite().

#define isinff   isinf
The alias for isinf().

#define isnanf   isnan
The alias for isnan().

#define ldexpf   ldexp
The alias for ldexp().

#define log10f   log10
The alias for log10().

#define logf   log
The alias for log().

#define lrintf   lrint
The alias for lrint().

#define lroundf   lround
The alias for lround().

#define M_1_PI   0.31830988618379067154 /* 1/pi */
The constant 1/pi.

#define M_2_PI   0.63661977236758134308 /* 2/pi */
The constant 2/pi.

#define M_2_SQRTPI   1.12837916709551257390 /* 2/sqrt(pi) */
The constant 2/sqrt(pi).

#define M_E   2.7182818284590452354
The constant e.

#define M_LN10   2.30258509299404568402 /* log_e 10 */
The natural logarithm of the 10.

#define M_LN2   0.69314718055994530942 /* log_e 2 */
The natural logarithm of the 2.

#define M_LOG10E   0.43429448190325182765 /* log_10 e */
The logarithm of the e to base 10.

#define M_LOG2E   1.4426950408889634074 /* log_2 e */
The logarithm of the e to base 2.

#define M_PI   3.14159265358979323846 /* pi */
The constant pi.

#define M_PI_2   1.57079632679489661923 /* pi/2 */
The constant pi/2.

#define M_PI_4   0.78539816339744830962 /* pi/4 */
The constant pi/4.

#define M_SQRT1_2   0.70710678118654752440 /* 1/sqrt(2) */
The constant 1/sqrt(2).

#define M_SQRT2   1.41421356237309504880 /* sqrt(2) */
The square root of 2.

#define NAN   __builtin_nan(“”)
NAN constant.

#define powf   pow
The alias for pow().

#define roundf   round
The alias for round().

#define signbitf   signbit
The alias for signbit().

#define sinf   sin
The alias for sin().

#define sinhf   sinh
The alias for sinh().

#define squaref   square
The alias for square().

#define tanf   tan
The alias for tan().

#define tanhf   tanh
The alias for tanh().

#define truncf   trunc
The alias for trunc().

Function Documentation

Double acos (double __x)
The acos() function computes the principal value of the arc cosine of __x. The returned value is in the range [0, pi] radians. A domain error occurs for arguments not in the range [-1, +1].

double asin (double __x)
The asin() function computes the principal value of the arc sine of __x. The returned value is in the range [-pi/2, pi/2] radians. A domain error occurs for arguments not in the range [-1, +1].

double atan(double __x)
The atan() function computes the principal value of the arc tangent of __x. The returned value is in the range [-pi/2, pi/2] radians.

double atan2(double __y, double __x)
The atan2() function computes the principal value of the arc tangent of __y / __x, using the signs of both arguments to determine the quadrant of the return value. The returned value is in the range [-pi, +pi] radians.

double cbrt(double __x)
The cbrt() function returns the cube root of __x.

double ceil(double __x)
The ceil() function returns the smallest integral value greater than or equal to __x, expressed as a floating-point number.

static double copysign(double __x, double __y)
The copysign() function returns __x but with the sign of __y. They work even if __x or __y are NaN or zero.

 

double exp(double __x)
The exp() function returns the exponential value of __x.

double fabs(double __x)
The fabs() function computes the absolute value of a floating-point number __x.

double fdim(double __x, double __y)
The fdim() function returns max(__x – __y, 0). If __x or __y or both are NaN, NaN is returned.

double floor(double __x)
The floor() function returns the largest integral value less than or equal to __x, expressed as a floating-point number.

double fma(double __x, double __y, double __z)
The fma() function performs floating-point multiply-add. This is the operation (__x * __y) + __z, but the intermediate result is not rounded to the destination type. This can sometimes improve the precision of a calculation.

double fmax(double __x, double __y)
The fmax() function returns the greater of the two values __x and __y. If an argument is NaN, the other argument is returned. If both arguments are NaN, NaN is returned.

double fmin(double __x, double __y)
The fmin() function returns the lesser of the two values __x and __y. If an argument is NaN, the other argument is returned. If both arguments are NaN, NaN is returned.

double fmod(double __x, double __y)
The function fmod() returns the floating-point remainder of __x / __y.

double frexp(double __x, int * __pexp)
The frexp() function breaks a floating-point number into a normalized fraction and an integral power of 2. It stores the integer in the int object pointed to by __pexp.

If __x is a normal float point number, the frexp() function returns the value v, such that v has a magnitude in the interval [1/2, 1) or zero, and __x equals v times 2 raised to the power __pexp. If __x is zero, both parts of the result are zero. If __x is not a finite number, the frexp() returns __x as is and stores 0 by __pexp.
Note: This implementation permits a zero pointer as a directive to skip a storing the exponent.

double hypot (double __x, double __y)
The hypot() function returns sqrt(__x*__x + __y*__y). This is the length of the hypotenuse of a right triangle with sides of length __x and __y, or the distance of the point (__x, __y) from the origin. Using this function instead of the direct formula is wise, since the error is much smaller. No underflow with small __x and __y. No overflow if result is in range.

static int isfinite(double __x)
The isfinite() function returns a nonzero value if __x is finite: not plus or minus infinity, and not NaN.

int isinf (double __x)
The function isinf() returns 1 if the argument __x is positive infinity, -1 if __x is negative infinity, and 0 otherwise.

int isnan(double __x)
The function isnan() returns 1 if the argument __x represents a “not-a-number” (NaN) object, otherwise 0.

double ldexp(double __x, int __exp)

The ldexp() function multiplies a floating-point number by an integral power of 2. It returns the value of __x times 2 raised to the power __exp.

double log(double __x)
The log() function returns the natural logarithm of argument __x.

double log10(double __x)
The log10() function returns the logarithm of argument __x to base 10.

long lrint(double __x)
The lrint() function rounds __x to the nearest integer, rounding the halfway cases to the even integer direction. (That is both 1.5 and 2.5 values are rounded to 2). This function is similar to rint() function, but it differs in type of return value and in that an overflow is possible.
Returns: The rounded long integer value. If __x is not a finite number or an overflow was, this realization returns the LONG_MIN value (0x80000000).

long lround(double __x)
The lround() function rounds __x to the nearest integer, but rounds halfway cases away from zero (instead of to the nearest even integer). This function is similar to round() function, but it differs in type of return value and in that an overflow is possible.
Returns: The rounded long integer value. If __x is not a finite number or an overflow was, this realization returns the LONG_MIN value (0x80000000).

double modf(double __x, double * __iptr)
The modf() function breaks the argument __x into integral and fractional parts, each of which has the same sign as the argument. It stores the integral part as a double in the object pointed to by __iptr.
The modf() function returns the signed fractional part of __x.
Note: This implementation skips writing by zero pointer. However, the GCC 4.3 can replace this function with inline code that does not permit to use NULL address for the avoiding of storing.

float modff(float __x,float *  __iptr)
An alias for modf().

double pow(double __x, double __y)
The function pow() returns the value of __x to the exponent __y.

double round(double __x)
The round() function rounds __x to the nearest integer, but rounds halfway cases away from zero (instead of to the nearest even integer). Overflow is impossible.
Returns: The rounded value. If __x is an integral or infinite, __x itself is returned. If __x is NaN, then NaN is returned.

int signbit(double __x)
The signbit() function returns a nonzero value if the value of __x has its sign bit set. This is not the same as `__x < 0.0′, because IEEE 754 floating point allows zero to be signed. The comparison `-0.0 < 0.0′ is false, but `signbit (-0.0)’ will return a nonzero value.

 

double sqrt(double __x)
The sqrt() function returns the non-negative square root of __x.

float sqrtf(float)
An alias for sqrt().

double square(double __x)
The function square() returns __x * __x.
Note: This function does not belong to the C standard definition.

 

double trunc(double __x)
The trunc() function rounds __x to the nearest integer not larger in absolute value.

Arduino – Trigonometric Functions

double tan(double __x)
The tan() function returns the tangent of __x, measured in radians.

double tanh(double __x)
The tanh() function returns the hyperbolic tangent of __x.

double sin(double __x)
The sin() function returns the sine of __x, measured in radians.

double sinh(double __x)
The sinh() function returns the hyperbolic sine of __x.

double cos(double __x)
The cos() function returns the cosine of __x, measured in radians.

double cosh(double __x)
The cosh() function returns the hyperbolic cosine of __x.

Advertisements
%d bloggers like this: