| 1 |
/* |
| 2 |
Character analysis operators |
| 3 |
|
| 4 |
Examples using the character analysis operators. |
| 5 |
Send any byte and the sketch will tell you about it. |
| 6 |
|
| 7 |
created 29 Nov 2010 |
| 8 |
modified 2 Apr 2012 |
| 9 |
by Tom Igoe |
| 10 |
|
| 11 |
This example code is in the public domain. |
| 12 |
|
| 13 |
https://www.arduino.cc/en/Tutorial/BuiltInExamples/CharacterAnalysis |
| 14 |
*/ |
| 15 |
|
| 16 |
void setup() { |
| 17 |
// Open serial communications and wait for port to open: |
| 18 |
Serial.begin(9600); |
| 19 |
while (!Serial) { |
| 20 |
; // wait for serial port to connect. Needed for native USB port only |
| 21 |
} |
| 22 |
|
| 23 |
// send an intro: |
| 24 |
Serial.println("send any byte and I'll tell you everything I can about it"); |
| 25 |
Serial.println(); |
| 26 |
} |
| 27 |
|
| 28 |
void loop() { |
| 29 |
// get any incoming bytes: |
| 30 |
if (Serial.available() > 0) { |
| 31 |
int thisChar = Serial.read(); |
| 32 |
|
| 33 |
// say what was sent: |
| 34 |
Serial.print("You sent me: \'"); |
| 35 |
Serial.write(thisChar); |
| 36 |
Serial.print("\' ASCII Value: "); |
| 37 |
Serial.println(thisChar); |
| 38 |
|
| 39 |
// analyze what was sent: |
| 40 |
if (isAlphaNumeric(thisChar)) { |
| 41 |
Serial.println("it's alphanumeric"); |
| 42 |
} |
| 43 |
if (isAlpha(thisChar)) { |
| 44 |
Serial.println("it's alphabetic"); |
| 45 |
} |
| 46 |
if (isAscii(thisChar)) { |
| 47 |
Serial.println("it's ASCII"); |
| 48 |
} |
| 49 |
if (isWhitespace(thisChar)) { |
| 50 |
Serial.println("it's whitespace"); |
| 51 |
} |
| 52 |
if (isControl(thisChar)) { |
| 53 |
Serial.println("it's a control character"); |
| 54 |
} |
| 55 |
if (isDigit(thisChar)) { |
| 56 |
Serial.println("it's a numeric digit"); |
| 57 |
} |
| 58 |
if (isGraph(thisChar)) { |
| 59 |
Serial.println("it's a printable character that's not whitespace"); |
| 60 |
} |
| 61 |
if (isLowerCase(thisChar)) { |
| 62 |
Serial.println("it's lower case"); |
| 63 |
} |
| 64 |
if (isPrintable(thisChar)) { |
| 65 |
Serial.println("it's printable"); |
| 66 |
} |
| 67 |
if (isPunct(thisChar)) { |
| 68 |
Serial.println("it's punctuation"); |
| 69 |
} |
| 70 |
if (isSpace(thisChar)) { |
| 71 |
Serial.println("it's a space character"); |
| 72 |
} |
| 73 |
if (isUpperCase(thisChar)) { |
| 74 |
Serial.println("it's upper case"); |
| 75 |
} |
| 76 |
if (isHexadecimalDigit(thisChar)) { |
| 77 |
Serial.println("it's a valid hexadecimaldigit (i.e. 0 - 9, a - F, or A - F)"); |
| 78 |
} |
| 79 |
|
| 80 |
// add some space and ask for another byte: |
| 81 |
Serial.println(); |
| 82 |
Serial.println("Give me another byte:"); |
| 83 |
Serial.println(); |
| 84 |
} |
| 85 |
} |