x +=y; is the same as x=x; ???

so the short version is that I am trying to do a running average with code like this:

bob += newData;
counter++;
temp_avg = bob / counter;

SHOULD be working fine, BUT just in case I changed to slightly in case my Uno was just having a bad day:

bob = bob + newData;
counter++;
temp_avg = bob / counter;

same result, so at least it is consistent. As far as I can tell the line that is breaking is the line:

bob += newData

what i wind up getting is:

bob = newData

no idea why (its also inside an infinite loop before someone makes a smart remark :stuck_out_tongue: )
I have used this same way of doing averaging dozens of times before… i also tried other methods that should have worked and got similar results. (basically math breaks… hardcore).

I am attaching the code I am running.

i am guessing that there MUST be something that I have just missed (like how you can’t do : String bob = "bob: " + myFunc() + “”:wink: or not thought about… even though I have checked and seen “y = y+1;” examples and statements all over.

running: arduino 1.0.5 -r2
using: Uno / Mega ADK (i get the problem on both)

needing: help… or to be declared insane. either way.

thanks in advance,
~Zack49

my_adc_try_1.ino (2.29 KB)

Your computer does not have "bad days".

x += y should be the same as x = x+y

 double reference=A0;
 double measurement= A1;

This looks like rubbish, but probably not what is causing your problem.

I don't see anywhere in your program, where you are attempting to use "+="

Arduino doesn't actually have "double".

As far as I know, you can use "double" and you will actually get float, so in your case, it should still work. But you might want to do some kind of test to verify that.

a0_avg = 0;

This statement near the end of the loop( ) function would appear to defeat your apparent objective of having some kind of running average.

looks like you are falling into to the integer arithmetic trap. That are the types of bob, and temp_arv ?.

Mark

 const double ledPin_modulating =  12;      // the number of the LED pin
...
 double ledState = LOW;             // ledState used to set the LED
..
  pinMode(ledPin_modulating, OUTPUT);

A floating-point type for pin number? A floating-point type for a LED state?

zack49: so the short version is that I am trying to do a running average with code like this:

Please post a complete sketch that demonstrates the problem - preferably one that demonstrates it in the simplest possible way without relying on any external hardware or non-standard libraries. Explain what your input values are, what output value you expect and what output values you actually get.

I can't find bob or temp_avg in that program anywhere.

Are you talking about a0_avg, which is being set back to zero at the end of each loop? Next to last line in loop(),

There is one condition where x+=y is the same as x=y and that is if x == zero. Adding to zero gives you back the same number you started with. That's first grade math. So if you set the value back to zero at the end of each loop it shouldn't be any surprise that the next time you reference it it will be zero. That's what you set it to be.

Here's that loop for those that don't want to download it.

void loop()
{
  looper = looper + 1;
  Serial.print("looper: ");Serial.println(looper);
  ledState = (ledState == LOW)?my_off():my_on();
  digitalWrite(ledPin_modulating, ledState);
  Serial.print("A0_avg: ");Serial.println(analogRead(A0));
  if(maxval)
  {
    double difference = (a0_on - a0_off);
    Serial.print("\t\t A0_inst: ");Serial.print(difference);
    a0_avg = (a0_avg + difference);
    loop_cntr++;
    double temp = a0_avg / loop_cntr;
    Serial.print(" A0_avg: ");Serial.print(a0_avg);Serial.print(" temp: ");Serial.println(temp);
  }
  a0_avg = 0;          
  delay(50);
  
}

This line may be confusing you...

Serial.print("A0_avg: ");Serial.println(analogRead(A0));

You think you're looking at aO_avg but you're not. You're looking at an analog reading. Put a0_avg in there and see what it prints.

1st: thanks for the replies and suggestions.
2nd: i am VERY sorry for the mess of code that I uploaded. :blush:
3rd: (i am as baffeled as anyone else about this) it works now…
/a quick explaination: i came back and started the comp back up, re-uploaded the code to the uno, and got the same results as last night. i did this 2 more times on the off chance it didnt correctly upload-- exactly the same. but after writing this post and just before submitting it i gave it one last shot… and now it works…??? so i guess it’s fixed…yeah…/

delta_g: i was only looking at the “A0_avg:” that is in the if loop. i forgot to change the text because i had stopped paying attention to it. nice catch though.

also in the code that got uploaded there was this line:

a0_avg = 0;

but in the code that i was running (and thought that I had uploaded) it appears this way:

if(!loop_cntr)a0_avg = 0;

so that when ever “loop_cntr” rolls-over I reset “a0_avg” to avoid data typing errors. (I dont know why it uploaded the 1st and not the second, and yes I had already saved before uploading. and i did run after saving with the same results.)

michinyon: i didnt know that arduino doesnt have doubles, now i do-- thanks!

as for all the variables that should have been some type other than “double”, this is again because I uploaded messy code. :blush: i had done a “ctrl+f” for all things "int " and a replace all with "double " because i had thought that it might have been a data typing error and knew that, while extremely wasteful, nothing bad would come of things like

int ledState = LOW; becoming double ledState = LOW;

thanks for all the help (and i promise to triple check the code i upload from now on)
~Zack49