Skip to main content
Added an array
Source Link

Anonymous Penguin's code works for me on an Uno after fixing a couple of typos. It is often best to make sure that both the numerator and the denominator are floats to ensure that the result is also treated as a float. So please try the following where the 10 is replaced with a 10.0:

while(Serial.available()) 
{
  float number = (float)(Serial.read() - 48) / 10.0;
  Serial.println(number, 4);
}

If you want to deal with each digit separately an array might be best but that will depend on your end goal.

const int MAX_LENGTH = 10;
char buffer[MAX_LENGTH];
char lineFeed = 10;

int length = Serial.readBytesUntil(lineFeed, buffer, MAX_LENGTH);
int i = 0;
while( i < length )
{
  //do what ever needs to be done to each character here, I'll just convert them to an int and print them as an example
  Serial.println( buffer[i]-48 );
  i++;
}

Anonymous Penguin's code works for me on an Uno after fixing a couple of typos. It is often best to make sure that both the numerator and the denominator are floats to ensure that the result is also treated as a float. So please try the following where the 10 is replaced with a 10.0:

while(Serial.available()) 
{
  float number = (float)(Serial.read() - 48) / 10.0;
  Serial.println(number, 4);
}

Anonymous Penguin's code works for me on an Uno after fixing a couple of typos. It is often best to make sure that both the numerator and the denominator are floats to ensure that the result is also treated as a float. So please try the following where the 10 is replaced with a 10.0:

while(Serial.available()) 
{
  float number = (float)(Serial.read() - 48) / 10.0;
  Serial.println(number, 4);
}

If you want to deal with each digit separately an array might be best but that will depend on your end goal.

const int MAX_LENGTH = 10;
char buffer[MAX_LENGTH];
char lineFeed = 10;

int length = Serial.readBytesUntil(lineFeed, buffer, MAX_LENGTH);
int i = 0;
while( i < length )
{
  //do what ever needs to be done to each character here, I'll just convert them to an int and print them as an example
  Serial.println( buffer[i]-48 );
  i++;
}
Source Link

Anonymous Penguin's code works for me on an Uno after fixing a couple of typos. It is often best to make sure that both the numerator and the denominator are floats to ensure that the result is also treated as a float. So please try the following where the 10 is replaced with a 10.0:

while(Serial.available()) 
{
  float number = (float)(Serial.read() - 48) / 10.0;
  Serial.println(number, 4);
}