{"id":815,"date":"2018-06-19T12:16:43","date_gmt":"2018-06-19T12:16:43","guid":{"rendered":"https:\/\/ibex.tech\/c\/?p=815"},"modified":"2026-06-17T09:25:03","modified_gmt":"2026-06-17T08:25:03","slug":"buffers","status":"publish","type":"post","link":"https:\/\/ibex.tech\/c\/c\/memory\/buffers","title":{"rendered":"Buffers"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Move buffer down one place<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\t\/\/Move the buffer down 1\n\tfor (Count = 0; Count &lt; (MY_BUFFER_SIZE - 1); Count++)\n\t{\n\t\tMyBuffer&#91;Count] = MyBuffer&#91;(Count + 1)];\n\t}\n\tMyBuffer&#91;(MY_BUFFER_SIZE - 1)] = SomeNewValueBeingAdded;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Move buffer up one place<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\t\/\/Move the buffer up 1\n\tfor (Count = (MY_BUFFER_SIZE - 1); Count &gt; 0; Count--)\n\t{\n\t\tMyBuffer&#91;Count] = MyBuffer&#91;(Count - 1)];\n\t}\n\tMyBuffer&#91;0] = SomeNewValueBeingAdded;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Rolling sample buffer<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>  const int MY_BUFFER_SIZE = 2048;\n  static int MyBufferNextValueIndex = 0;\n  static uint16_t MyBuffer&#91;MY_BUFFER_SIZE];\n  static bool MyBufferIsFull = 0;\n  int Count;\n  int MaxIndex;\n  uint32_t Value;\n\n  \/\/Add the new value to the buffer\n  MyBuffer&#91;MyBufferNextValueIndex] = analogRead(A0);    \/\/&lt;&lt;&lt;&lt; Add your value to buffer\n  MyBufferNextValueIndex++;\n  if (MyBufferNextValueIndex >= MY_BUFFER_SIZE)\n  {\n    MyBufferNextValueIndex = 0;\n    MyBufferIsFull = 1;\n  }\n\n\n  \/\/Get the current average value\n  MaxIndex = MY_BUFFER_SIZE;\n  if (!MyBufferIsFull)\n    MaxIndex = MyBufferNextValueIndex;\n\n  Value = 0;\n  for (Count = 0; Count &lt; MaxIndex; Count++)\n    Value += MyBuffer&#91;Count];\n  \n  Value \/= MaxIndex;\n\n  \/\/Do something with it...<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Move buffer down one place Move buffer up one place Rolling sample buffer<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-815","post","type-post","status-publish","format-standard","hentry","category-memory"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts\/815","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/comments?post=815"}],"version-history":[{"count":9,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts\/815\/revisions"}],"predecessor-version":[{"id":1619,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts\/815\/revisions\/1619"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/media?parent=815"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/categories?post=815"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/tags?post=815"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}