So, I have come across the first thing in my Intro to Embedded Systems programming class that I honestly don't know how to do. I have searched around and I have found nothing. I try not to ask for this much help, but I have been racking my brain and I got nothing.
Here is the parameter given by the professor:
Use a function to convert (decode) each ASCII digit into the corresponding integer number. Hint: accept a "char" as a parameter, and return an "int" This function must be able to output the entire "int" range and handle issues if the user inputs a value outside the range, positive and negative.
He specifically noted that we cannot use the Serial.parseFloat or Serial.parseInt functions. The whole point is to write our own functions.
Tell the professor that I don't understand the instructions.
I think that a number of ascii characters are received.
They should be converted into a number, and you should detect if it is outside the 'int' range.
But I'm not sure.
You can take a look at the ascii values.
If someone would have typed a '3' in the serial monitor, the code 0x33 (or 51 decimal) is received. You have to convert that to a value.
That is done like this: value = received_character - '0';
By substracting the ascii value of zero, the result is an integer number.
Combine a few received characters into an integer value and check if it gets too high or too low to fit in an integer.
PaulS:
Did he also prohibit you from looking at the source code?
Collecting data into an array is not hard. NULL terminating the array is trivial. Calling atoi() on the result is a piece of cake.
Sorta, we are supposed to use only the Serial.read and Serial.available functions to get the data from the serial, then anything we do with that data has to be done with our own functions that we design. Looking at the source code was unofficially discouraged and I am pretty sure that I can't use the atoi() function because I did not design it.
Caltoa:
I think that a number of ascii characters are received.
They should be converted into a number, and you should detect if it is outside the 'int' range.
But I'm not sure.
You can take a look at the ascii values. ASCII - Wikipedia
If someone would have typed a '3' in the serial monitor, the code 0x33 (or 51 decimal) is received. You have to convert that to a value.
That is done like this: value = received_character - '0';
By substracting the ascii value of zero, the result is an integer number.
Combine a few received characters into an integer value and check if it gets too high or too low to fit in an integer.
I probably should have noted that this isn't the whole project. The entire project is a reverse polish notation calculator that prompts the user for the first number, then the second, then the operator. I have all that figured out though, I just don't know how to convert the bytes received from serial when a number is entered. I will try your suggestion and see how it goes. Thanks!
You can test the ascii value before substracting the ascii value of zero.
Using: if (received_char >= '0' && received_char <= '9')
Substracting the ascii value of zero is not some programming skill, it is common knowledge, you know it or you don't.
You may well say don't use any functions as you did not write them... so you may as well just present back with the theory on how it's done... as any code used will not entirely be your own!
Well after talking to him he gave me some hints and I should apparently be able to do it with a switch/case and/or bit masking. I think that I'll use the subtraction by ASCII 0 method though. That seems much simpler!