I cant understand what is going on here , I was trying to find way to send through Serial a number for example 12 and then extract from it number 1 and 2 and store them in two different variables , but suddenly i was unable to make a simple division !!
2 Answers
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++;
}
-
the point here they are still one number i need to separate them Sep 13, 2014 at 17:21
-
1Okay, let me make sure I understand you. It sounds like you have two questions, #1 how to make division work properly, #2 how to read multiple characters from the serial port and do something with them. Have we solved #1 for you? Would you like to open a new question for #2 where you can describe what you want to read and what you want to do with it?– Bob LutzSep 13, 2014 at 17:40
-
i didnt ask for Question one , and Q2 is exactly what i need Sep 13, 2014 at 17:47
Do you want to eventually do 12 / 10
or 1 / 10
and 2 / 10
? If the second one, you'll have to read all the serial values into an array and convert those characters to their plain integer values (subtracting 48 from each value). After that, you'll have to add them together with the correct placeholders (A * 100 + B * 10 + C
).
Basically, the read()
function returns one character in the ASCII format. ACII basically representing characters on your screen with numbers. 48 displays a 0 on your screen, 49 a 1, and so on.
You're also facing a problem where your numbers are getting truncated. This means it cuts off the decimal. For when you send 100
, it is 48 / 10 = 4.8
, however 4.8
becomes just 4
. The same thing happens for 49.
To fix this, you need to use a float
data type. This allows decimal values. You also want to make sure that the conversion from float to string works correctly for the Serial.print()
so you don't end up with 4
again.
Final code:
while(Serial.available()) {
float number = (float)(Serial.read() - 48) / 10;
Serial.println(number, 4);
}
-
I used your code and i send 12 the result is 0.00000.0000 !! Sep 13, 2014 at 15:48
-
@AbdelrahmanTarief: Did you read the first paragraph of the answer? Do you understand why I commented on the question? Sep 13, 2014 at 16:34
-
1the repeated dots are from different numbers. You need to print a space between printing the digits for it to make sense. 0.10000.20000 is supposed to be 0.1000, 0.2000. 0.000.00 would be 0.00, 0.00.– BrettAMSep 13, 2014 at 16:42
-
-
1@AbdelrahmanTarief that is what my code does... in fact we have the same exact code. I'm not sure what you need. Sep 13, 2014 at 19:04
Serial.read()
does?