enter image description here

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 !!

  • 1
    Have you read what Serial.read() does? – Ignacio Vazquez-Abrams Sep 13 '14 at 15:39
  • @IgnacioVazquez-Abrams : yes it reads what ever i send to the serial – Abdelrahman Elshafiey Sep 13 '14 at 15:49
  • @IgnacioVazquez-Abrams meant what datatype it returns. – Anonymous Penguin Sep 13 '14 at 18:07

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:

  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 );
  • the point here they are still one number i need to separate them – Abdelrahman Elshafiey Sep 13 '14 at 17:21
  • 1
    Okay, 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 Lutz Sep 13 '14 at 17:40
  • i didnt ask for Question one , and Q2 is exactly what i need – Abdelrahman Elshafiey Sep 13 '14 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 !! – Abdelrahman Elshafiey Sep 13 '14 at 15:48
  • @AbdelrahmanTarief: Did you read the first paragraph of the answer? Do you understand why I commented on the question? – Ignacio Vazquez-Abrams Sep 13 '14 at 16:34
  • 1
    the 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. – BrettAM Sep 13 '14 at 16:42
  • @AbdelrahmanTarief fixed the code, a few typos. – Anonymous Penguin Sep 13 '14 at 17:17
  • 1
    @AbdelrahmanTarief that is what my code does... in fact we have the same exact code. I'm not sure what you need. – Anonymous Penguin Sep 13 '14 at 19:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.