The text that is sent, you only look for the presence of '2' or '3'.
What text do you expect to receive? A '2' or '3' or line end?
Reason is simple.. you can read text a single char at a time to check for '2' or '3' without putting any of it into a C string or C++ String. Just watch the text stream for what you want, there is no reason to save any of it.
Yes you can get help with the code, the hard part may be believing that something so simple can work. It does and it works faster than making the computer do more -- no surprise there hey? We want simple, it takes fewer lines which means fewer lines to debug.
When I debounce a button, the possible states that I might care about are:
button staying down
button staying up
button transition up to down
button transition up to down
button is bouncing
If I only care to catch button just pressed then I need 1 bit to represent T/F.
If I want to catch button release as well then I need 2 bits for 4 binary states:
state 00: equal to if (( just pressed == false ) && ( released == false ))
state 01: equal to if (( just pressed == true ) && ( released == false ))
state 10: equal to if (( just pressed == false ) && ( released == true ))
state 11: equal to if (( just pressed == true ) && ( released == true ))
Where state 11 (decimal 3) should never happen except by bug or chip malfunction which tight, complete code error checks for, but that is professional software.
I think that you only need to detect button presses, only need 1 bit True if just pressed.
Then Voltage state bit, True if > 2V
Then 1 bit for True if measuring time interval?
And then 2 bits for 3 possible Serial states, in binary
00 is neither '2' nor '3' is read, most of the time Serial.available() Will be 0, no char.
01 is that a '2' has been received.
10 is that a '3' has been received.
11 is literally impossible except by bug or chip malf, an extreme error state!
So looked at like this I get 5 bits to cover every possible combination of Inputs.
I would collect each Input and build that 5 (or however many) bit Total State one tiny step at a time, the code will be simple and fast.
When I have that, how about the single if(), right?
We can use a switch-case statement to cover every possible outcome, make a case for the "legal" combinations and catch the rest as the default case which catches errors.
Spend time defining what to do in every case, it will be simple. Each case is an if ( with 5 different compares ) that the switch value matches... the state bits evaluate themselves by matching a case or becoming the default that matched no other case.
The wonderfully simplifying switch-case statement!
As always, practice will strengthen then complete your understanding.