I'm developing a program to calculate static margin of an aircraft through the calculations of mass,moment and arm. Essentially dealing with large numbers.

I'm having difficulty in setting up my code in the way so that the arithmetic is applied correctly to larger numbers. What I have so far works but only when the input is 0-9. The arithmetic fails to calculate digits beyond.

I understand that I'm breaking the digits into separate variables as to convert them from ASCII format to their intended value. The problem is in putting those digits back together.

I know this is probably very simple but I'm picking up arduino again after a long break and I'm very rusty.

Here's my code thus far:

long batterymoment,num1,num2,answer;
int batterymass = 720;
int batteryposition;
boolean mySwitch = false;

void setup() {
  Serial.println("Enter battery position");

void loop() {
  while (Serial.available()){
   batteryposition = Serial.read();

   if(batteryposition>47 && batteryposition<58){  

   Serial.print("Battery moment is: ");



Do you mean to repeat num1=0; in loop()?

It is only reading one character per loop() because loop() happens much faster than Serial.available() and Serial.read().

Do the reporting and clearing when you receive some sort of separator/non-digit character.

| improve this answer | |
  • so you mean have a statement that requires a non-digit character to be satisfied before 'answer=num1+num2'? – Jertise Mar 9 '16 at 18:28
  • 1
    Yes. You should be able to add an else { ... } around the last chunk of code to make this work. It would probably be cleaner to copy the characters into a buffer and then do atol(), atoi(), atof()... etc. on the buffer after you know you have the whole message. – Dave X Mar 9 '16 at 18:40

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.