Array Segments - wrap around


i have googled simple method sum elements in segment of circular array. far nothing better first attempt.

perhaps best use real world example, 1 started quest. have built irrigation controller, , record rain in mm each hour (0-23). easy, used mrmp array.

code: [select]
 *(rainduringhour210 + hourtoday( *tz300 - 1)) = mmb;  // byte array of 24 elements 0 23 matching previous hour.

in graphic, 2 mm recorded in element 0, , 8 mm in element 6, totaling 10mm.



my irrigate controller must know total rain during previous x hours.

now tricky part...
the code created perform task flexible, in allows me reuse index calculation other circular arrays. handles indexes multiple times size of circular array.

in case, have array, *rainduringhour210, starting @ 0, , ending @ 23.

i want indexes each element 05:00, 9 hours 21:00. notice crosses 0, not difficult handle.

code: [select]
// constrain index
// returns absoulte index rolling range start + index.

int constrainfromindexint(int minimum, int maximum, int start, int index)
{
 maximum++;
 start = constrain(start,minimum,maximum); // sure start within range
 if(minimum < 0)
 {
   maximum--;
   minimum--;
 }
 index = (index + start) % (maximum - minimum);  // go round , round , land within minimum , maximum

   if(index > maximum)
 {
   index = minimum + (index - maximum);
 }

 if(index < minimum)
 {
   index = maximum + index;
 }
 return index;
}



so how use it?


code: [select]
// return total rain in mm x10 in previous x hours
int rainprevioushours(int hours)
{
 hours = constrain(hours,1,24);
 int mm = 0;
 

 for(hours; hours >= 1 ; hours--)
 {
   // segments array rainduringhour210 , wraps around @ 23 -> 0

   mm = mm + *(rainduringhour210 + constrainfromindexint(0, 23, hourtoday(*tz300), -hours));
 }
 return mm * 10;  // 200 series array did not save decimal.
}



any suggestions simplifying constrainfromindexint(


- george

what mrmp arrays?
http://www.mmcs.com/~gmatthews/fav1-00012d85/s0005027b-0005027b

quote
*(rainduringhour210 + hourtoday( *tz300 - 1)) = mmb;


any particular reason this?

why not shorter, , more intuitive
quote
rainduringhour210 [hourtoday( *tz300 - 1)] = mmb;

?


Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > Array Segments - wrap around


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