just a question that requires a simple answer. How do I process a large number of arithmetic steps in arduino efficiently and precisely?
For example I have this code function setup:
void calc() {
batterymoment = input*batterymass;
totalmoment = 149650+batterymoment;
buoyancymoment = buoyancymass*centrebuoyancy;
wheelmoment = totalmoment - buoyancymoment;
wheelweight = totalmass - buoyancymass;
cgwheels = wheelmoment/wheelweight;
maindistance = mainarm - wheelweight;
nosedistance = wheelweight - nosearm;
mainmass = (((wheelweight)*(1-((maindistance)/(maindistance+nosedistance))))/2);
nosemass = (((wheelweight)*(1-((nosedistance)/(nosedistance+maindistance))))/2);
mainmoment = mainmass*mainarm;
nosemoment = nosemass*nosearm;
netmoment = buoyancymoment+mainmoment+nosemoment;
netcg = netmoment/totalmass;
staticmargin = adc - netcg;
answer = staticmargin/meanadc;
}
Now I've properly setup up data types for all of these variables and they are a combination of float, int and long. I don't think it's a problem with the data type. What I'm getting when I print final answer in the serial is the same number regardless the input the user enters.
I'm guessing I need to format the code in such a way that it performs each line step by step and verifies the answer before proceeding to the next arithmetic line. If so would I need a function for each arithmetic step? Or is there a simpler way to process this that I'm missing.
Any guidance is appreciated. Thanks.
Here is the code in its entirety:
unsigned long batterymoment,num1,num2,input,totalmoment;
double buoyancymoment,wheelmoment,cgwheels,mainmass,nosemass,maindistance,nosedistance;
double mainmoment,nosemoment,netmoment,netcg,staticmargin,answer;
double centrebuoyancy = 14.164;
double mainarm = 14.3;
double adc = 11.4;
double meanadc = 22.65;
double nosearm = 1.8;
int totalmass = 12404;
int batterymass = 720;
int buoyancymass = 4401;
int batteryposition,wheelweight;
boolean mySwitch = false;
void calc() {
batterymoment = input*batterymass;
totalmoment = 149650+batterymoment;
buoyancymoment = buoyancymass*centrebuoyancy;
wheelmoment = totalmoment - buoyancymoment;
wheelweight = totalmass - buoyancymass;
cgwheels = wheelmoment/wheelweight;
maindistance = mainarm - wheelweight;
nosedistance = wheelweight - nosearm;
mainmass = (((wheelweight)*(1-((maindistance)/(maindistance+nosedistance))))/2);
nosemass = (((wheelweight)*(1-((nosedistance)/(nosedistance+maindistance))))/2);
mainmoment = mainmass*mainarm;
nosemoment = nosemass*nosearm;
netmoment = buoyancymoment+mainmoment+nosemoment;
netcg = netmoment/totalmass;
staticmargin = adc - netcg;
answer = staticmargin/meanadc;
}
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);
}
}
if(batteryposition==61){
input=num1+num2;
calc();
Serial.print("Static margin is: ");
Serial.println(answer);
num1=0;
num1=0;
mySwitch=false;
}
}
}
So depending on the user input, the calculations will change the final output answer. For example if a 5 should be entered the output should be -4.22 based on my calculations in excel.