Pages: [1]   Go Down
Author Topic: Need help urgently!!  (Read 636 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
Online Online
Faraday Member
**
Karma: 108
Posts: 4736
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

The Elders of the Internet know who I am
I'm on LinkedIn: http://www.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: 118
Posts: 4349
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: 52
Posts: 2382
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
Online Online
Brattain Member
*****
Karma: 642
Posts: 50411
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
Online Online
Faraday Member
**
Karma: 108
Posts: 4736
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the reply zimboZA!

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

The Elders of the Internet know who I am
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Pages: [1]   Go Up
Jump to: