Pages: [1]   Go Down
Author Topic: Need help urgently!!  (Read 577 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can anybody explain this code for me!!!

  double result = 0;
  float BATT1[5];
  float BATTERY[21]; // J from 1 to 20
  for(int J=1;J<=20;J++)
  {
    for(int k=0;k<10;k++)
    {
      BATT1[k] = getAnalog(J);
      result += BATT1[k];
    }
    
      result = result / 10;
      BATTERY[J] = result;
      result = 0;
  }
  
« Last Edit: January 03, 2013, 09:41:17 am by radons » Logged

Johannesburg. UTC+2
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3840
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Looks to me that for each of 20 batteries it takes 10 readings, averages them, and saves that as the voltage for that battery.
Logged

Although I answer in good faith, remember always: Your Mileage May Vary.
Get me on LinkedIn: za.linkedin.com/in/jimbrownza/

Atlanta
Offline Offline
Full Member
***
Karma: 4
Posts: 128
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

but i'd change... float BATT1[5]; to float BATT1[10];
Logged


East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 109
Posts: 4066
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Looks to me as though there is a semi-colon missing
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 50
Posts: 2202
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yep, but float BATT1[5] is only 5 long so it over writes something else. BATT1 is not required.

Mark
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the reply zimboZA! But I don't really understand how is it done.

Does it do the inner loop first and if the condition is not true, i.e k=11, it will continue to divide by 10?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Does it do the inner loop first and if the condition is not true, i.e k=11, it will continue to divide by 10?
Stick a Serial.print() statement in there and learn for yourself.
Logged

Atlanta
Offline Offline
Full Member
***
Karma: 4
Posts: 128
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

adding comments into the code ALWAYS helps

Code:
 double result = 0;
  float BATT1[10];
  float BATTERY[21];

  // loop Battery J from 1 to 20
  for(int J=1;J<=20;J++)
  {
    // take 10 readings from Battery J and store sum in "result"
    for(int k=0;k<10;k++)
    {
      BATT1[k] = getAnalog(J);
      result += BATT1[k]; // short way of doing result=result+BATT1[k]
    }

    // store average of "result" back into Battery J location
    result = result / 10;
    BATTERY[J] = result;
      
    // reset "result" for next battery
    result = 0;
  }
« Last Edit: January 03, 2013, 09:57:29 am by abrookfield » Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

adding comments into the code ALWAYS helps

Code:
 double result = 0;
  float BATT1[10];
  float BATTERY[21];

  // loop Battery J from 1 to 20
  for(int J=1;J<=20;J++)
  {
    // take 10 readings from Battery J and store sum in "result"
    for(int k=0;k<10;k++)
    {
      BATT1[k] = getAnalog(J);
      result += BATT1[k]; // short way of doing result=result+BATT1[k]
    }

    // store average of "result" back into Battery J location
    result = result / 10;
    BATTERY[J] = result;
      
    // reset "result" for next battery
    result = 0;
  }

Thanks alot!!
Logged

Johannesburg. UTC+2
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3840
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the reply zimboZA!

That's Jimbo   smiley .... but you're very welcome
Logged

Although I answer in good faith, remember always: Your Mileage May Vary.
Get me on LinkedIn: za.linkedin.com/in/jimbrownza/

Pages: [1]   Go Up
Jump to: