Go Down

Topic: Need help urgently!! (Read 664 times) previous topic - next topic

radons

Jan 03, 2013, 03:29 pm Last 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

Looks to me that for each of 20 batteries it takes 10 readings, averages them, and saves that as the voltage for that battery.
Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

abrookfield

but i'd change... float BATT1[5]; to float BATT1[10];
www.reeftopper.com

UKHeliBob

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

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

Mark

radons

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

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.

abrookfield

#7
Jan 03, 2013, 03:52 pm Last 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;
 }
www.reeftopper.com

radons


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


Thanks for the reply zimboZA!


That's Jimbo   :) .... but you're very welcome
Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Go Up