addition problems adding long/int to a rolling total

good morning all i am reposting this as my last post went off on a bit of a tangent, and i didn't explain the issue very well.

[u]Summary[/u] issue..addition of long's int's not working as expected. needs..to add the variable value of Y to X evrytime the loop gose round. hardware..mega2650 knowledge..ALOT less than yourself :)(some understanding of the language and equations) end game .. to come out with a X pre hour read out to serial monitor.

[u]my attempt to explain[/u]

i gess first off, normaly the value of Y is from a mapped analog input

  int aVal = analogRead(A0);
  // map the value to nessasery value requierments
int Y =map(aVal, 532, 1023 , 0 , 80)

for debug prepossess i substituted this for a constant value for Y, when running the below code i am not getting an increce in totalX as i would exspect.

int y=10;
void setup() {

  Serial.begin(9600);
}

void loop() {

long milisec = millis();            // calculate time in milliseconds
long time = milisec / 1000 ;        // convert milliseconds to 1/10th seconds
long totalX =totalX + y;            // calculate total X
long avgX = totalX / time ;         // average X
long Xphr = (avgX * time ) / 3600 ; // Xhr
long mXphr = (Xphr * 1000 ) ;      // mXhr
  Serial.print("  time. ");
  Serial.print(time);    //time in seconds
  Serial.print( "  totalX. ");
  Serial.print(totalX); //total increment of X over time
  Serial.print("  avgX. ");
  Serial.print(avgX);   // ?
  Serial.print("  Xphr. ");
  Serial.print(Xphr);    // output in hours
  Serial.print("  mXphr. ");
  Serial.println(mXphr);// output in 1000th


}

by my mind it should be adding the value of Y to the totalX value evry time the loop goes around, so in the case of the planed code increacing totalX by the value of Y at the time of analogread.

however at this line,

long totalX =totalX + y;            // calculate total X

the code seems to give up, returning a 0 regardless of the value of Y .

i have spent the time looking through google, the arduino refrences , and the fourums here and have not found a usefule awnswer .

so any help will be useful and much appreciated.

[u]side notes[/u]

as you can probably see form my code i am trying to get a X per hr reading from this and this code has been proven to work in someone else's project unfortunately contacting this person has not worked .

long totalX =totalX + y;            // calculate total X

That line declares a new variable named totalX and because it is local to a function sets it to an indeterminate value each time through loop()

Either declare totalX as a global and leave out the long in the line of code or declare it static long in the function to prevent it being reset each time through loop() and set it to zero, and do the addition in a separate line of code

@ helibob

thankyou i shall give it a try

@helibob

exselent that is working now thankyou sir

powerball230:
@helibob

exselent that is working now thankyou sir

Which solution did you use ?
Do you understand why your original code did not work ?

i made it global aka put it at the the very start

and i think it was because it was resetting up the long every time the loop went round thus always returning a 0 value .

that's how i have understood it any who. :)

i think it was because it was resetting up the long every time the loop went round thus always returning a 0 value .

Declaring the variable was certainly resetting its value but because it was being done inside function its value could have been anything laying around in the memory allocated to it, unlike global (and static) variables which are initialised to zero if no value is specified

long time = milisec / 1000 ;        // convert milliseconds to 1/10th seconds

milliseconds divided by 1000 are seconds.