Go Down

### Topic: Need help urgently!! (Read 1 time)previous topic - next topic

#### radons

##### Jan 03, 2013, 03:29 pmLast Edit: Jan 03, 2013, 03:41 pm by radons Reason: 1
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;
}

#### JimboZA

#1
##### Jan 03, 2013, 03:35 pm
Looks to me that for each of 20 batteries it takes 10 readings, averages them, and saves that as the voltage for that battery.
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

#### abrookfield

#2
##### Jan 03, 2013, 03:38 pm
but i'd change... float BATT1[5]; to float BATT1[10];

#### UKHeliBob

#3
##### Jan 03, 2013, 03:39 pm
Looks to me as though there is a semi-colon missing
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

#### holmes4

#4
##### Jan 03, 2013, 03:41 pm
Yep, but float BATT1[5] is only 5 long so it over writes something else. BATT1 is not required.

Mark

#### radons

#5
##### Jan 03, 2013, 03:43 pm
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?

#### PaulS

#6
##### Jan 03, 2013, 03:51 pm
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.
The art of getting good answers lies in asking good questions.

#### abrookfield

#7
##### Jan 03, 2013, 03:52 pmLast Edit: Jan 03, 2013, 03:57 pm by abrookfield Reason: 1
adding comments into the code ALWAYS helps

Code: [Select]
`  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;  }`

#### radons

#8
##### Jan 03, 2013, 04:11 pm

adding comments into the code ALWAYS helps

Code: [Select]
`  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!!

#### JimboZA

#9
##### Jan 03, 2013, 05:30 pm

Thanks for the reply zimboZA!

That's Jimbo    .... but you're very welcome
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

Go Up