Here is my code:
#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel pixel(1,11);
const String a[119] = {"-","h","he","li","be","b","c","n","o",
"f","ne","na","mg","al","si","p","s","cl",
"ar","k","ca","sc","ti","v","cr","mn","fe",
"co","ni","cu","zn","ga","ge","as","se","br",
"kr","rb","sr","y","zr","nb","mo","tc","ru",
"rh","pd","ag","cd","in","sn","sb","te","i",
"xe","cs","ba","la","ce","pr","nd","pm","sm",
"eu","gd","tb","dy","ho","er","tm","yb","lu",
"hf","ta","w","re","os","ir","pt","au","hg",
"tl","pb","bi","po","at","rn","fr","ra","ac",
"th","pa","u","np","pu","am","cm","bk","cf",
"es","fm","md","no","lr","rf","db","sg","bh",
"hs","mt","ds","rg","cn","nh","fl","mc","lv",
"ts","og"};
const String b[119] = {"-","-","h","l","b","-","-","-","-",
"-","n","n","m","a","s","-","-","c",
"a","-","c","s","t","-","c","m","f",
"c","n","c","z","g","g","a","s","b",
"k","r","s","-","z","n","m","t","r",
"r","p","a","c","i","s","s","t","-",
"x","c","b","l","c","p","n","p","s",
"e","g","t","d","h","e","t","y","l",
"h","t","-","r","o","i","p","a","h",
"t","p","b","p","a","r","f","r","a",
"t","p","-","n","p","a","c","b","c",
"e","f","m","n","l","r","d","s","b",
"h","m","d","r","c","n","f","m","l",
"t","o"};
String x;
int y;
String tmp1;
String tmp2;
int z;
void setup(){
pixel.begin();
pixel.setPixelColor(0, 0, 0, 20);
pixel.show();
}
void loop(){
Serial.begin(9600);
while(!Serial){}
x = Serial.readString();
back:
if(x.length() > 0){
if(x.startsWith(" ")){ //Space
x.remove(0,1);
Serial.print(" ");
}else if(x.startsWith("n")){ //Number
x.remove(0,1);
y = x.toInt();
Serial.print(y);
tmp2 = y;
z = tmp2.length();
x.remove(0,z);
}else if(x.startsWith("a")){ //Letter "a"
x.remove(0,1);
y = x.toInt();
Serial.print(a[y]);
removeInt();
}else if(x.startsWith("A")){ //Capital "A"
x.remove(0,1);
y = x.toInt();
tmp1 = a[y];
tmp1.toUpperCase();
Serial.print(tmp1);
removeInt();
}else if(x.startsWith("b")){ //Letter "b"
x.remove(0,1);
y = x.toInt();
Serial.print(b[y]);
removeInt();
}else if(x.startsWith("B")){ //Capital "B"
x.remove(0,1);
y = x.toInt();
tmp1 = b[y];
tmp1.toUpperCase();
Serial.print(tmp1);
removeInt();
}
goto back;
}
}
void removeInt(){
if(y < 10){
x.remove(0,1);
}else if(y < 100){
x.remove(0,2);
}else{
x.remove(0,3);
}
}
I am using the Adafruit QT Py (ATSAMD21E18)
To clarify:
const String a[119]
and const String b[119]
are lookup tables containing all of the strings that are needed.
String x
is the variable that holds string(s) sent through the Serial monitor.
int y
is variable that holds the index used for the lookup tables.
String tmp1
and String tmp2
aka "Temporary 1" and "Temporary 2" respectively, are variables used for working with strings as the lookup tables cannot/should not be modified.
int z
is simply a temporary variable used to store the length of some strings.
This code is meant to take a string sent through the Serial monitor and then use that string to print back a readable string.
What this code is supposed to do:
Read a string (with a particular format) sent through the Serial monitor.
Print a string based on the first character(s):
Lowercase "a" > Using the number following the "a" as the index, print a string from lookup table a[119], and remove the letter and number.
Uppercase "A" > Same as Lowercase, but change the string to Uppercase before printing.
Uppercase and Lowercase "B" > same as "A".
Space > Print a space.
The letter "n" > Print the number following the "n", remove the letter and number.
Repeat this process until the entire string is gone (or rather, until the length of the String x is equal to 0).
If I send "A92a7a19a102a74a7 A23a13a92b63 n123456" with the Serial monitor, it prints back the string "Unknown Value 123456", which is exactly as intended.
However, if I send the string (or any string) again, it prints back nothing. Trying to send something once or twice more causes everything to freeze up and I am unable to interact with the IDE, and it must be forcibly closed using the Task Manager.
Although it works as intended the first time, I would like to be able to repeat the process by sending multiple strings.
Can anyone help me understand why it only functions once and what is causing this freezing?
I am somewhat new to programming, so perhaps my methodology isn't ideal.