If you are an Arduino enthusiast or an electronics hobbyist, you might have heard about the isAscii() function of Arduino. This function is widely used in Arduino projects, and it is an essential function in the Arduino programming language. This article covers the isAscii() function syntax, parameter and return.
Table of Content
What is the isAscii() Function?
The isAscii() function is a built-in function of Arduino that checks whether the given character is an ASCII character or not. ASCII stands for American Standard Code for Information Interchange, and it is a character encoding system used in computers and electronic devices. The isAscii() function returns true if the given character is an ASCII character and false if it is not.
How Does the isAscii() Function Work?
The isAscii() function takes a single argument, which is the character to be checked. It checks whether the character is within the range of ASCII characters, which is from 0 to 127.
If the character is within this range, the function will give us true, indicating that the input character is an ASCII. If the character is outside this range, the function returns false, indicating that the character is not an ASCII character.
Syntax
The syntax of the isAscii() function is as follows:
Parameters
The isAscii() function takes a single parameter:
value: The value to be checked whether it is an ASCII character or not. This parameter can be of type char, int, byte, or unsigned int.
Return value
The isAscii() gives a Boolean output value:
-
- true: If the value is an ASCII character.
- false: If the value is not an ASCII character.
Note that the ASCII character set consists of 128 characters, including the letters A-Z, a-z, numbers 0-9, and various special characters such as punctuation marks and control characters. The isAscii() function checks if the input character lies within range or not.
Example of isAscii() Function
Here’s an example Arduino code that demonstrates the use of the isAscii() function:
Serial.begin(9600); // Start the serial communication
}
void loop() {
char ch = 'H'; // Define a character variable
if (isAscii(ch)) { // Check if the character is ASCII
Serial.print(ch); // Print the character
Serial.println(" is an ASCII character."); // Print a message
} else {
Serial.print(ch); // Print the character
Serial.println(" is not an ASCII character."); // Print a message
}
delay(1000); // Wait for a second
}
In this example, we define a character variable ch and set its value to the character ‘H’. We then use the isAscii() function to check if the input is an ASCII or not. If it is an ASCII character, we print a message saying that it is an ASCII character. If it is not an ASCII character, we print a message saying that it is not an ASCII character. We use the Serial library to show output on the Arduino serial terminal.
When you upload this code to your Arduino board and open the serial monitor, you should see the following output:
Since the character ‘H’ is an ASCII character, the isAscii() function always returns true, and the message “H is an ASCII character” is printed repeatedly every second.
Check ASCII Character in User Input Using isAscii() Arduino Function
Here’s an example Arduino code that takes user input from the serial monitor and checks if it is ASCII or not using the isAscii() function:
Serial.begin(9600); // Initialize serial communication
}
void loop() {
if (Serial.available() > 0) { // If there's data available in serial buffer
char userInput = Serial.read(); // Read the user input
Serial.print("Your Input Character is: ");
Serial.println(userInput);
if (isAscii(userInput)) { // Check if the user input is ASCII
Serial.println("Input is ASCII"); // Print message to serial monitor
} else {
Serial.println("Input is not ASCII"); // Print message to serial monitor
}
}
}
In this code, the setup() function initializes the serial communication. The loop() function continuously checks if there is data available in the serial buffer using the Serial.available() function. If there is data available, it reads the user input using Serial.read().
The isAscii() function returns true if the input character is an ASCII character, and false if it is not. We pass the user input to this function to check if it is ASCII or not. If it is ASCII, it prints the message “Input is ASCII” to the serial monitor using Serial.println(). If it is not ASCII, it prints the message “Input is not ASCII” to the serial monitor using Serial.println().
We have sent a character “a” which is an ASCII character so following output will appear:
Now we have sent the character “é” (e with an acute accent), which is a non-ASCII character. So, we can see a message printed that this is a non-ASCII character on the Arduino serial terminal.
Conclusion
The isAscii() function is a useful function in Arduino that checks whether a given character is an ASCII character or not. It has various applications in Arduino projects, such as user input validation, data transmission, and text processing. Understanding the isAscii() function is essential for anyone who works with Arduino and wants to build Arduino based projects.