Hello everyone.
I have the following code where I want to extract the unsigned long value from a millis() calculation and divide it by an integer:
unsigned long startTime;
unsigned long endTime;
unsigned long newDuration;
byte timerRunning;
unsigned long lastPosition = 0;
void calculate_new_last_position(){
int A = 30000L;
int B = 100L;
int X = newDuration;
int Y = (B*X)/A; //A simple rule of three calculation
lastPosition = Y;
Serial.println(String("Total Traveled Position: ") + (int)lastPosition + String(" %"));// here I get just 0, why?
}
setup(){...}
void loop() {
if (timerRunning == 0 && pressed){
startTime = millis();
timerRunning = 1;
}
if (timerRunning == 1 && !pressed){
endTime = millis();
timerRunning = 0;
newDuration = (endTime - startTime);
Serial.print (String("Total Traveled in ms: ") + newDuration + String(" ms"));// this value here prints like a charm
}
}
If I do this on calculate_new_last_position() it works and it prints 50 to the Serial:
int A = 30000L;
int B = 100L;
int X = 15000L;// an example number that could come from the loop
int Y = (B*X)/A; //A simple rule of three calculation to get a percentage
lastPosition = Y;
Serial.println(String("Total Traveled Position: ") + (int)lastPosition + String(" %"));// here I get 50 that's correct
I am not a programmer just a hobbyist, so please, ELI5.
What am I doing wrong?
Thank you.