Created
October 26, 2019 04:49
-
-
Save futureshocked/445d97fd89dfbdc4e3684d08518defd4 to your computer and use it in GitHub Desktop.
n example of how to create an use an array of chars
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * An example of how to create an use an array of chars | |
| * We'll declare an array of 5 chars. | |
| * We'll print the contents in the serial monitor. | |
| * | |
| * The table of ASCII character from https://en.wikipedia.org/wiki/ASCII | |
| * is useful. | |
| * | |
| */ | |
| //char my_char[6] = {'a', 'b', 'c', 'd', 'e'} ; // We want this array to contain 5 char, | |
| // so we'll declare it with size 6. | |
| // Try the exact same experiment with byte | |
| //char my_char[6] = "abcde"; // Alternative definition 1 | |
| //String my_char = "abcde"; // Alternative definition 2, using a String | |
| void setup() { | |
| Serial.begin(9600); | |
| while (!Serial) { | |
| ; // wait for serial port to connect. Needed for native USB | |
| } | |
| Serial.println("Your array char:"); | |
| for (int i = 0; i<5; i++) // Experiment to see what happens if your index goes out of bounds | |
| { | |
| Serial.print("Index "); | |
| Serial.print(i); | |
| Serial.print(" contains char '"); | |
| Serial.print(my_char[i]); | |
| Serial.print("', ASCII decimal "); | |
| Serial.println(my_char[i], DEC); | |
| } | |
| Serial.println(); | |
| Serial.println("Let's do some calculations:"); | |
| for (int i = 0; i<4; i++) | |
| { | |
| Serial.print(my_char[i]); | |
| Serial.print(" + "); | |
| Serial.print(my_char[i+1]); | |
| Serial.print(" = "); | |
| Serial.println(my_char[i] + my_char[i+1]); | |
| } | |
| Serial.println(); | |
| Serial.println("Let's do some calculations, and store the results:"); | |
| for (int i = 0; i<4; i++) | |
| { | |
| Serial.print(my_char[i]); | |
| Serial.print(" + "); | |
| Serial.print(my_char[i+1]); | |
| Serial.print(" = "); | |
| char sum = my_char[i] + my_char[i+1]; // Beware, I am using a char here, which is | |
| // a single byte. This means that if this | |
| // calculation yields more than 256, it will | |
| // truncate without an error. | |
| my_char[i] = sum; | |
| Serial.print(my_char[i], DEC); | |
| Serial.print(" -> Index "); | |
| Serial.print(i); | |
| Serial.print(" now contains value "); | |
| Serial.println(my_char[i], DEC); | |
| } | |
| } | |
| void loop() { | |
| // put your main code here, to run repeatedly: | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment