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.begin(9600);
num1=0;
num2=0;
Serial.println("Enter battery position");
}
void loop() {
while (Serial.available()){
batteryposition = Serial.read();
if(batteryposition>47 && batteryposition<58){
if(!mySwitch){
num1=(num1*10)+(batteryposition-48);
}else{
num2=(num2*10)+(batteryposition-48);
}
}
answer=num1+num2;
batterymoment=answer*batterymass;
Serial.print("Battery moment is: ");
Serial.println(batterymoment);
num1=0;
num1=0;
mySwitch=false;
}
}
Thanks.