# Math and character functions in Arduino

There are many math functions that we can use in Arduino. These functions help us a lot in even in a complex calculation inside the Arduino. Arduino gives us some trigonometry functions also, but we will see that in any other tutorial. Let’s see the list of math functions that Arduino gives us.

1. abs()
2. constrain()
3. map()
4. max()
5. min()
6. sq()
7. sqrt()
8. pow()

We will now see these functions and their function in Arduino one by one

## abs function in Arduino

This function gives us absolute value of any number. It converts negative number into positive number. Assume we have a number ‘x’ and we have passed it into abs() as its parameter then it will return ‘x’ as it is if it is 0 or greater than 0. But if ‘x’ is less than 0 then abs function will remove the negative sign from the number and return a positive number. Let’s see an brief example code.

``````void setup() {			// put your setup code here, to run once:
Serial.begin(9600);		//initializing the serial communication
Serial.println(abs(12));	//serial printing the absolute value of 12
Serial.println(abs(0));	//serial printing the absolute value of 0
Serial.println(abs(-12));	//serial printing the absolute value of -12
}
void loop() {
// put your main code here, to run repeatedly:
}
``````

Output: 12

0

12

## Constrain function

This function brings any number in a specified range. Suppose you want anyone to serial input a number that should be in between 0 to100 but any user enters more or less than that range. This may break your program. To avoid that case you should use constrain function. After passing input number to constrain function it will bring in that range. Suppose user inputs a number below that range then it will set that number minimum according to that range. Let’s see an example code.

``````void setup() {					// put your setup code here, to run once:
Serial.begin(9600);				//initializing the serial communication
}
void loop() {					// put your main code here, to run repeatedly:
int input = Serial.parseInt(); 			 // keep other operations outside the constrain function
int constrainedInput = constrain(input, 0, 100);	//constraining the input
Serial.println(constrainedInput);
delay(1000);
}
``````

This will constrain the input number between 0 to 100.

## map function

map function re-map any number form on range to another range. Suppose we have a number 20 from a range  to 100. We want to remap that number in a range of 0 to 1000. Then this number will change to 100x(20/100) = 200. So, map function doesn’t change the weightage of that number. This may be very helpful in unit related application.

Let’s see the syntax of map function.

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

Where:

value = the actual value we want to map

fromLow = original range low value

fromHigh = original range high value

toLow = new range low value

toHigh = new range high value

## max function in Arduino

max function takes two number as input and returns the greater number.

Example: –

``````void setup() {		// put your setup code here, to run once:
Serial.begin(9600);	//initializing the serial communication
Serial.println(max(12,13));
}
void loop() {
// put your main code here, to run repeatedly:
}
``````

Output: 13

## min function in Arduino

min function also takes two number as input but returns the small number.

Example: –

``````void setup() {		// put your setup code here, to run once:
Serial.begin(9600);	//initializing the serial communication
Serial.println(min(12,13));
}
void loop() {
// put your main code here, to run repeatedly:
}
``````

Output: 12

## sq, sqrt and pow functions in Arduino

sq function gives us the square of number passed to it. sqrt gives us the square root of a number that is given as parameter. pow function can calculate any power of any number. We can use it find square roots also.

Example: –

``````void setup() {// put your setup code here, to run once:
Serial.begin(9600);
Serial.println(sq(8));
Serial.println(sqrt(16));
Serial.println(pow(3,2));
Serial.println(pow(16,0.5));
Serial.println(pow(16,0.25));
}
void loop() {
// put your main code here, to run repeatedly:
}
``````

Output: 64

4.00

9.00

4.00

2.00

## Character functions in Arduino

Arduino gives some function to perform operations on character. Such as whether the character is alpha (letter) or alphanumeric (letter or number) or any other. So, let’s see them.

## isAlpha()

isAlpha function checks if any character is alpha (letter). If character is letter then it returns 1 else it returns 0. Let’s see the syntax of this function.

isAlpha(char)

Let’s see an example code.

``````char character1 = '1';
char character2 = 'a';
void setup() {// put your setup code here, to run once:
Serial.begin(9600);
Serial.println(isAlpha(character1));
Serial.println(isAlpha(character2));
}
void loop() {
// put your main code here, to run repeatedly:
}
``````

Output: 0

1

## isAlphaNumeric()

isAlphaNumeric function checks if any character is alphanumeric (letter or number). It returns 1 if character is either letter or number and return. Let’s see an example code.

``````char character1 = '1';
char character2 = 'a';
char character3 = '@';
void setup() {// put your setup code here, to run once:
Serial.begin(9600);
Serial.println(isAlphaNumeric(character1));
Serial.println(isAlphaNumeric(character2));
Serial.println(isAlphaNumeric(character3));
}
void loop() {
// put your main code here, to run repeatedly:
}
``````

Output: 1

1

0

## isAscii()

isAscii() function checks if character is ASCII. If character is ASCII then it returns 1 else it returns 0.

``````char character1 = '1';
char character2 = 97;	//97 will be treated as ASCII value since data type is character
char character3 = 254;	// same here
void setup() {// put your setup code here, to run once:
Serial.begin(9600);
Serial.println(isAscii(character1));
Serial.println(isAscii(character2));
Serial.println(isAscii(character3));
}
void loop() {
// put your main code here, to run repeatedly:
}
``````

Output: 1

1

0

## isControl()

isControl function checks whether the character is control character or not. Let’s see some control words and their ASCII code.

First 32 (0 to 31) ASCII values are control character. Control character does not print on screen such as backspace, esc etc.

Example code –

``````char character = 31;	//31 will be treated as ASCII value since data type is character
void setup() {// put your setup code here, to run once:
Serial.begin(9600);
Serial.println(isControl(character));
}
void loop() {
// put your main code here, to run repeatedly:
}
``````

Output: 1

## isDigit()

isDigit function checks whether character is number or not. If char is number then it returns 1 else 0.

``````char character1 = '1';
char character2 = 'a';
void setup() { // put your setup code here, to run once:
Serial.begin(9600);
Serial.println(isDigit(character1));
Serial.println(isDigit(character2));
}
void loop() {
// put your main code here, to run repeatedly:
}
``````

Output: 1

0

## isGraph()

isGraph function checks whether character is printable and have some content or not. Remember space is a printable character but has no content. If character is printable and has content then this function will return 1.

``````char character1 = ' ';
char character2 = 'a';
void setup() {// put your setup code here, to run once:
Serial.begin(9600);
Serial.println(isGraph(character1));
Serial.println(isGraph(character2));
}
void loop() {
// put your main code here, to run repeatedly:
}
``````

Output: 0

1

isHexadecimalDigit function checks whether the number is hexadecimal or not (like it contains 0-9 and A-F or not). This function returns 1 if character is hexadecimal.

``````not). This function returns 1 if character is hexadecimal.
char character1 = ' ';
char character2 = 'a';
void setup() {// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
}
``````

Output: 0

1

## isLowerCase()

isLowerCase function checks if character contains a lower case letter.

``````char character1 = ' ';
char character2 = 'a';
void setup() {// put your setup code here, to run once:
Serial.begin(9600);
Serial.println(isLowerCase(character1));
Serial.println(isLowerCase(character2));
}
void loop() {
// put your main code here, to run repeatedly:
}
``````

Output: 0

1

## isPrintable()

isPrintable function checks whether character is printable or not. It is similar to isGraph function except this function return 1 in space character also.

``````char character1 = ' ';
char character2 = 'a';
void setup() {// put your setup code here, to run once:
Serial.begin(9600);
Serial.println(isPrintable(character1));
Serial.println(isPrintable(character2));
}
void loop() {
// put your main code here, to run repeatedly:
}
``````

Output: 1

1

## isPunct()

isPunct function checks if the number is a punctual letter that is comma(,), a semicolon(;), exclamation mark(!), and so on.

``````char character1 = ' ';
char character2 = 'a';
char character3 = '!';
void setup() {// put your setup code here, to run once:
Serial.begin(9600);
Serial.println(isPunct(character1));
Serial.println(isPunct(character2));
Serial.println(isPunct(character3));
}
void loop() {
// put your main code here, to run repeatedly:
}
``````

Output: 0

0

1

## isSpace()

isSpace function returns the 1 if character is space, tab(\t), vertical tab (\v), carriage return (\r), or form feed(\f).

``````char character1 = ' ';
char character2 = '\f';
char character3 = '\n';
void setup() {// put your setup code here, to run once:
Serial.begin(9600);
Serial.println(isSpace(character1));
Serial.println(isSpace(character2));
Serial.println(isSpace(character3));
}
void loop() {
// put your main code here, to run repeatedly:
}
``````

Output: 1

1

1

## isUpperCase()

isUpperCase function checks if character is in upper case or not.

``````char character1 = 'a';
char character2 = 'A';
void setup() {// put your setup code here, to run once:
Serial.begin(9600);
Serial.println(isUpperCase(character1));
Serial.println(isUpperCase(character2));
}
void loop() {
// put your main code here, to run repeatedly:
}
``````

Output: 0

1

## isWhitespace()

isWhitespace function returns 1 if the character is a space or a horizontal tab (\t).

``````char character1 = ' ';
char character2 = '\t';
void setup() {// put your setup code here, to run once:
Serial.begin(9600);
Serial.println(isWhitespace(character1));
Serial.println(isWhitespace(character2));
}
void loop() {
// put your main code here, to run repeatedly:
}
``````

Output: 1

1