Need user input from 0 to 9999 inclusive, and separate each digit

NOTE I plan on summarizing code to keep this short as possible.
I have int userInput and int numberLocker[] = {}; declared globally. My approach is that in the void loop, there is the standard:

if(Serial.available() >0)
{
//[omitted code exists here]
}

to detect if there is data to be read, and if there is then read it:
userInput = Serial.parseInt();

After being read I convert from ASCII to its true value. Then I display its value to the serial monitor and the value appears just fine. I also have conditionals that work properly, preventing inputs that are negative or greater than 9999.

void digit_sort(void)
{
  float thousand, hundred, ten, one;
  
  thousand = int(userInput/1000);
  Serial.println(thousand);

  hundred = int(userInput/100);
  hundred = int(hundred) - int(hundred/10)*10;

  ten = int(userInput)/10;
  ten = int(ten) - int(ten/10)*10;

  one = int(userInput - int(userInput/10)*10);
  numberLocker[0] = thousand;
  numberLocker[1] = hundred;
  numberLocker[2] = ten;
  numberLocker[3] = one;
}

However, the array remains full of zeros and does not update any of its values. Any insight to why that is? The purpose of each digit to be separated into base 10 numbers, is to then use the number as an index in another array:

const unsigned char digitTable = {//all 10 of the binary patterns that pertain to LSBFIRST}

which has binary patterns that will display it to a 4-digit seven-segment display

You should probably not use floats in digit_sort().

You can use the modulo operator (%) to get individual decimal digits:

void digit_sort(void)
{
  numberLocker[0] = (userInput / 1000) % 10; // Modulo just in case over 9999
  Serial.println(numberLocker[0]);
  numberLocker[1] = (userInput / 100) % 10;
  numberLocker[2] = (userInput / 10) % 10;
  numberLocker[3] = userInput % 10;
}

I don't see anything clearly wrong in the small portion of the code that you provided so I have to assume that your mistake is in some other part of the code. Maybe your loop() has a local variable named userInput so the digit_sort() function is using the global variable which is still set to the default of zero because loop() is setting its local variable.

KianRoybal:
detect if there is data to be read, and if there is then read it:
userInput = Serial.parseInt();
**After being read I convert from ASCII to its true value. **

The Serial.parseInt() function does the conversion from ASCII to its 'true value'. What is it that you are doing in your 'conversion'?!?

Thank you! Helpful information for sure. I forgot I changed from Serial.read() to the Serial.parseInt(). Hence:

  • '0'

for conversion. I was originally going to have a while loop that broke after the counter looped four times, but I ran into the issue of the array storing the integers in the opposite order. I found the read to be convenient since it already treats a digit as separate.

Instead of Serial.parseInt() you might be better off reading char at a time into an array. Each digit then already exists in its own index (just subtract 48 to get decimal from ASCII).

KianRoybal:
However, the array remains full of zeros and does not update any of its values.

In my case, the array's elements are updated correctly with the values of thousand, hundred,..., and etc. However, you have to take care for the consistent data types of the variables.

I ended up going with Serial.parseInt(), and had all the digits sorted correctly. Instead of there being no parameters to pass, I ended up tidying up my code and making userInput local to loop(), but passed through my function as

void digit_sort(int number)
{
//omitted code here
}

Worked just fine! It took care of the issue of zeros being returned. Thanks for all the help.