Cycles and average value


how write loop witch after x cycles adds values (for example sensor connected analog input) , calculate average  ?

use loop, addition , division.

code: [select]
int val;
int tot = 0;
int ave;
int cnt = 5;

for(int i=0; i<cnt; i++) // loop cnt times
{
  val = analogread(pin); // read value
  tot += val; // sum values
}
ave = tot/cnt;

some things watch out include overflow in tot variable , division 0. change tot long, if needed. make sure cnt never 0.

you might want delay little in loop.


Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > Cycles and average value


arduino

Comments

Popular posts from this blog

CAN'T INSTALL MAMBELFISH 1.5 FROM DIRECTORY - Joomla! Forum - community, help and support

error: expected initializer before 'void'

CPU load monitoring using GPIO and leds - Raspberry Pi Forums