user70755: Here is my code I am collecting data using an ECG sensor and trying to enhance the incoming data by stretching it across the x axis. Which results in this.

After this the screen eventually just goes black. What am I doing wrong?
This is my code.
#include <U8glib.h> 
U8GLIB_SSD1306_128X64 u8g(12, 11, 8, 9, 10);
int Buffer[128];int Buffer2[128];int max=0;
//int largest;
void pageECG();     // Page-1


const int pageCount = 2;
int p;
void (*pages[pageCount])() = {pageECG};
int duration [pageCount] = {10000};
void setup() {
  
  u8g.setFont(u8g_font_unifont);
  u8g.setColorIndex(1);
  p = 0;
  Serial.begin(9600); 

}

void loop()
{
u8g.firstPage();
  do {  
    (*pages[p])();
  } while( u8g.nextPage() );
  delay(duration[p]);
  p = p+1;
  if (p == pageCount)
    p=0;
}
void pageECG() {  
for(int x = 0;x < 128;x++) 
{
Buffer[x] =(analogRead(A0)*0.075);//This loop takes data input
delay(5);
if(Buffer[x]<32&&Buffer[x]!=2)
x-=2;
if(Buffer[x]>32&&Buffer[x]!=62)
x+=2;
Serial.println(Buffer[x]);
}  

for(int x = 0;x < 127;x++) 
{
u8g.setFont(u8g_font_unifont);
u8g.drawStr( 0, 60, "ECG");
u8g.drawLine(x, Buffer[x], x,Buffer[x+1]); //OP to OLED for BP

}}


user70020: Well, one problem is this: You have made an array of pointer to function, but only initialized the first of the two, to pageECG.
const int pageCount = 2;
int p;
void (*pages[pageCount])() = {pageECG};

The last line there roughly equivalent to:
void (*pages[pageCount])() = {pageECG, nullptr};

Later in loop() you have:
  u8g.firstPage();
  do {  
    (*pages[p])();
  } while( u8g.nextPage() );
  delay(duration[p]);
  p = p+1;
  if (p == pageCount)
    p=0;

After the first execution of loop(), p is 1.  Since it was not 2, it was not reset by your if (p == pageCount) p=0;  So on the next pass through loop(), (*pages[p])(); is     (*pages[1])();  where pages[1] is nullptr.
In practice, what you're doing is generating and executing an AVR icall (indirect call) instruction to address 0x0000 (nullptr representation) to the reset vector.  So, your sketch starts over almost like a reset, except the hardware has not been reset.  Importantly, the interrupts are still configured as they were and the global interrupt flag is on, which means all manner of potential weirdness if one of them manages to execute before you get back ground setup().  So, it's difficult to say exactly why you're seeing what you're seeing.  But, it doesn't surprise me as a potential outcome.  And it may not be the only problem; just a problem.

70755: <p>Here is my code I am collecting data using an ECG sensor and trying to enhance the incoming data by stretching it across the x axis. Which results in this.
<a href="https://i.stack.imgur.com/jqY7T.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jqY7T.jpg" alt="enter image description here" /></a></p>
<p>After this the screen eventually just goes black. What am I doing wrong?
This is my code.</p>
<pre><code>#include &lt;U8glib.h&gt; 
U8GLIB_SSD1306_128X64 u8g(12, 11, 8, 9, 10);
int Buffer[128];int Buffer2[128];int max=0;
//int largest;
void pageECG();     // Page-1


const int pageCount = 2;
int p;
void (*pages[pageCount])() = {pageECG};
int duration [pageCount] = {10000};
void setup() {
  
  u8g.setFont(u8g_font_unifont);
  u8g.setColorIndex(1);
  p = 0;
  Serial.begin(9600); 

}

void loop()
{
u8g.firstPage();
  do {  
    (*pages[p])();
  } while( u8g.nextPage() );
  delay(duration[p]);
  p = p+1;
  if (p == pageCount)
    p=0;
}
void pageECG() {  
for(int x = 0;x &lt; 128;x++) 
{
Buffer[x] =(analogRead(A0)*0.075);//This loop takes data input
delay(5);
if(Buffer[x]&lt;32&amp;&amp;Buffer[x]!=2)
x-=2;
if(Buffer[x]&gt;32&amp;&amp;Buffer[x]!=62)
x+=2;
Serial.println(Buffer[x]);
}  

for(int x = 0;x &lt; 127;x++) 
{
u8g.setFont(u8g_font_unifont);
u8g.drawStr( 0, 60, &quot;ECG&quot;);
u8g.drawLine(x, Buffer[x], x,Buffer[x+1]); //OP to OLED for BP

}}
</code></pre>
 70020: <p>Well, one problem is this: You have made an array of pointer to function, but only initialized the first of the two, to <code>pageECG</code>.</p>
<pre class="lang-cpp prettyprint-override"><code>const int pageCount = 2;
int p;
void (*pages[pageCount])() = {pageECG};
</code></pre>
<p>The last line there roughly equivalent to:</p>
<pre class="lang-cpp prettyprint-override"><code>void (*pages[pageCount])() = {pageECG, nullptr};
</code></pre>
<p>Later in <code>loop()</code> you have:</p>
<pre class="lang-cpp prettyprint-override"><code>  u8g.firstPage();
  do {  
    (*pages[p])();
  } while( u8g.nextPage() );
  delay(duration[p]);
  p = p+1;
  if (p == pageCount)
    p=0;
</code></pre>
<p>After the first execution of <code>loop()</code>, <code>p</code> is 1.  Since it was not 2, it was not reset by your <code>if (p == pageCount) p=0;</code>  So on the next pass through <code>loop()</code>, <code>(*pages[p])();</code> is     <code>(*pages[1])();</code>  where <code>pages[1]</code> is <code>nullptr</code>.</p>
<p>In practice, what you're doing is generating and executing an AVR <code>icall</code> (indirect call) instruction to address 0x0000 (<code>nullptr</code> representation) to the reset vector.  So, your sketch starts over almost like a reset, except the hardware has not been reset.  Importantly, the interrupts are still configured as they were and the global interrupt flag is on, which means all manner of potential weirdness if one of them manages to execute before you get back ground <code>setup().</code>  So, it's difficult to say exactly why you're seeing what you're seeing.  But, it doesn't surprise me as a potential outcome.  And it may not be the <em>only</em> problem; just <em>a</em> problem.</p>
