[U8GLIB] Problem with "software" overriding (i suppose)

In last time I'm working on oscilloscope code.

This is first version from Chinese forum :

/*
    arduino数字示波器
    作者:�ntwhq@tom.com
    2013 08
 */
#include <U8glib.h> //声明库
U8GLIB_PCD8544 u8g(7, 5, 6);
int Input = A0;  //声明输入引脚
int Key_add = 8;  //声明按钮引脚
int Key_sub = 9;
int Key_hold = 10;
int x, y; //声明坐标
int i, i1, i2, V_min, V_max, V_mid, t, t0, t1, sta, Key = 1, hold = 0;
long Freq;
float Vpp;
int Y[96]; //声明信号值储存数组
int Buffer[192];

void setup( )
{
  pinMode(Key_add, INPUT);
  digitalWrite(Key_add, HIGH);
  pinMode(Key_sub, INPUT);
  digitalWrite(Key_sub, HIGH);
  pinMode(Key_hold, INPUT);
  digitalWrite(Key_hold, HIGH);
  ADMUX = 0x60;
  ADCSRA = 0xe2;
  u8g.setFont(u8g_font_micro);
  u8g.setRot180();
}

void loop( )
{
  sample( );
  Measure( );
  Transform( );
  Key_scan( );
  if (hold == 0)
  {
    u8g.firstPage( );
    do
    {
      draw( );
    }
    while ( u8g.nextPage( ));
  }
}

void sample( )
{ for (i = 0; i < 192; i++)
  {
    Buffer[i] = ADCH;
    switch (Key)
    {
      case 1:
        break;
      case 2:
        delayMicroseconds(4);
        break;
      case 3:
        delayMicroseconds(10);
        break;
      case 4:
        delayMicroseconds(23);
        break;
      case 5:
        delayMicroseconds(60);
        break;
      case 6:
        delayMicroseconds(123);
        break;
      case 7:
        delayMicroseconds(248);
        break;
      case 8:
        delayMicroseconds(623);
        break;
      case 9:
        delayMicroseconds(1247);
        break;
      default: break;
    }
  }
}

void Measure()
{
  V_max = Buffer[0];
  V_min = Buffer[0];
  for (i = 0; i < 192; i++)
  {
    if (Buffer[i] > V_max)
      V_max = Buffer[i];
    if (Buffer[i] < V_min)
      V_min = Buffer[i];
  }
  V_mid = (V_max + V_min) / 2;
  Vpp = (V_max - V_min) * 50.0 / 255;
  for (i = 0; i < 97; i++)
  {
    if (Buffer[i] < V_mid && Buffer[i + 1] >= V_mid)
    {
      i1 = i;
      break;
    }
  }
  for (i = i1 + 1; i < 98 + i1; i++)
  {
    if (Buffer[i] < V_mid && Buffer[i + 1] >= V_mid)
    {
      i2 = i;
      break;
    }
  }
  t = i2 - i1;
  if (t > 0)
    Freq = 8000 / t;
  else
    Freq = 0;
}

void Transform( )
{
  for (sta = 0; sta < 96; sta++)
  {
    if (Buffer[sta] < 128 && Buffer[sta + 2] > 128)
      break;
  }
  for (i = 0; i < 96; i++)
    Y[i] =  63 - (Buffer[i + sta] >> 2);
}

void draw( )
{
  for (x = 0; x < 60; x++)
    u8g.drawLine(x, Y[x], x, Y[x + 1]); //画线

  //画边框
  u8g.drawFrame(0, 0, 60, 48);
  // 画坐标轴
  u8g.drawLine(30, 0, 30, 48);
  u8g.drawLine(0, 24, 60, 24);
  for (x = 0; x < 60; x += 6)
    u8g.drawLine(x, 23, x, 25);
  for (y = 0; y < 48; y += 6)
    u8g.drawLine(29, y, 31, y);
  //画网格
  for (x = 6; x < 60; x += 6)
  {
    for (y = 6; y < 48; y += 6)
      u8g.drawPixel(x, y);
  }
  //显示参数
  u8g.drawStr(62, 6, "MS");
  u8g.drawStr(62, 18, "V");
  u8g.drawStr(62, 24, "0.324");
  u8g.drawStr(62, 30, "Vpp");
  u8g.setPrintPos( 62, 36);
  u8g.print(Vpp);
  u8g.drawStr(78, 36, "V");
  u8g.drawStr(62, 42, "F(HZ)");
  switch (Key)
  {
    case  1:
      u8g.drawStr(62, 12, "0.02");
      u8g.setPrintPos( 62, 48);
      u8g.print(Freq * 50);
      break;
    case  2:
      u8g.drawStr(62, 12, "0.05");
      u8g.setPrintPos( 62, 48);
      u8g.print(Freq * 20);
      break;
    case  3:
      u8g.drawStr(62, 12, " 0.1");
      u8g.setPrintPos( 62, 48);
      u8g.print(Freq * 10);
      break;
    case  4:
      u8g.drawStr(62, 12, " 0.2");
      u8g.setPrintPos( 62, 48);
      u8g.print(Freq * 5);
      break;
    case  5:
      u8g.drawStr(62, 12, " 0.5");
      u8g.setPrintPos( 62, 48);
      u8g.print(Freq * 2);
      break;
    case  6:
      u8g.drawStr(62, 12, "  1");
      u8g.setPrintPos( 62, 48);
      u8g.print(Freq);
      break;
    case  7:
      u8g.drawStr(62, 12, "  2");
      u8g.setPrintPos( 62, 48);
      u8g.print(Freq / 2);
      break;
    case  8:
      u8g.drawStr(62, 12, "  5");
      u8g.setPrintPos( 62, 48);
      u8g.print(Freq / 5);
      break;
    case  9:
      u8g.drawStr(62, 12, " 10");
      u8g.setPrintPos( 62, 48);
      u8g.print(Freq / 10);
      break;
    default: break;
  }
}

//键盘扫描
void Key_scan()
{
  if (digitalRead(Key_add) == LOW)
  {
    while (digitalRead(Key_add) == LOW);
    Key++;
    if (Key == 10)
      Key = 9;
    delay(10);
  }
  if (digitalRead(Key_sub) == LOW)
  {
    while (digitalRead(Key_sub) == LOW);
    Key--;
    if (Key == 0)
      Key = 1;
    delay(10);
  }
  if (digitalRead(Key_hold) == LOW)
  {
    while (digitalRead(Key_hold) == LOW);
    hold = ~hold;
    delay(10);
  }
}

That code working on Arduino, without any errors/problems.

And this is my "optimized" version (I have reduced size of the first version) :

#include <U8glib.h>
U8GLIB_PCD8544 u8g(7, 5, 6);
const byte Input = A0;
byte i, Key = 1, hold = 0;
int Freq;
int Vpp;
int Y[96];
int Buffer[192];
const char* myStrings[] = {"", "0.02", "0.05", " 0.1", " 0.2", " 0.5", "  1", "  2", "  5", " 10"};
const int myArray[] = {0, 500, 200, 100, 50, 20, 10, 5, 2, 1};
const int myArrayOne[] = {0, 1, 4, 10, 23, 60, 123, 248, 623, 1247};

int main(void)
{
  init();
  {
    DDRB &= ~(1 << PB0);
    PORTB |= (1 << PB0);
    DDRB &= ~(1 << PB1);
    PORTB |= (1 << PB1);
    DDRB &= ~(1 << PB2);
    PORTB |= (1 << PB2);
    ADMUX = 0x60;
    ADCSRA = 0xe2;
    u8g.setFont(u8g_font_micro);
  }
  while (1)
  {
    sample();
    Measure();
    Transform();
    Key_scan();
    if (hold == 0)
    {
      u8g.firstPage();
      do
      {
        draw();
      }
      while (u8g.nextPage());
    }
  }
}

void sample()
{
  do
  {
    Buffer[i] = ADCH;
    delayMicroseconds(myArrayOne[Key]);
  }
  while (i < 192);
}

void Measure()
{
  int i1, i2, V_min, V_max, V_mid, t;
  V_max = Buffer[0];
  V_min = Buffer[0];
  do
  {
    if (Buffer[i] > V_max) {
      V_max = Buffer[i];
    }
    if (Buffer[i] < V_min) {
      V_min = Buffer[i];
    }
  }
  while (i < 192);
  V_mid = (V_max + V_min) / 2;
  Vpp = (V_max - V_min) * 50.0 / 255;
  do
  {
    if (Buffer[i] < V_mid && Buffer[i + 1] >= V_mid) {
      i1 = i;
      break;
    }
  }
  while (i < 97);
  for (i = i1 + 1; i < 98 + i1; i++) {
    if (Buffer[i] < V_mid && Buffer[i + 1] >= V_mid) {
      i2 = i;
      break;
    }
  }
  t = i2 - i1;
  if (t > 0) {
    Freq = 8000 / t;
  } else {
    Freq = 0;
  }
}

void Transform() {
  byte sta;
  do
  {
    if (Buffer[sta] < 128 && Buffer[sta + 2] > 128)
      break;
  }
  while (sta < 96);
  do
  {
    Y[i] =  63 - (Buffer[i + sta] >> 2);
  }
  while (i < 96);
}

void draw()
{
  byte x, y;
  do
  {
    u8g.drawLine(x, Y[x], x, Y[x + 1]);
  }
  while (x < 60);

  u8g.drawLine(30, 0, 30, 48);
  u8g.drawLine(0, 24, 60, 24);

  for (x = 6; x < 60; x += 6) {
    for (y = 6; y < 48; y += 6) {
      u8g.drawPixel(x, y);
    }
  }
  u8g.setPrintPos(62, 36);
  u8g.print(Vpp);
  u8g.setPrintPos(62, 12);
  u8g.print(myStrings[Key]);
  u8g.setPrintPos(62, 48);
  u8g.print((Freq * myArray[Key]) / 10);
}

void Key_scan()
{
  if ((PINB & (1 << PB0)) == 0) {
    while ((PINB & (1 << PB0)) == 0);
    Key++;
    if (Key == 10) {
      Key = 9;
    }
    delayMicroseconds(10);
  }
  if ((PINB & (1 << PB1)) == 0) {
    while ((PINB & (1 << PB1)) == 0);
    Key--;
    if (Key == 0) {
      Key = 1;
    }
    delayMicroseconds(10);
  }
  if ((PINB & (1 << PB2)) == 0) {
    while ((PINB & (1 << PB2)) == 0);
    hold = ~hold;
    delayMicroseconds(10);
  }
}

In the simulator code working like first version. All seems like normal.
BUT there is one thing, that I cant understand.

  1. I'm uploaded first version of code onto Arduino board, and all is working.
  2. Then I've uploaded second version of code. And on the screen (i'm using u8glib with nokia 84x48 display) STILL displays the FIRST version of code. Not working (not reading analog input and not displays the signal and other) BUT displaying something like last "screenshot" of the woking version.

HOW it can be done ? Im just not understand the situation. Its like my Arduino not want to override. BUT I can upload first version of code to the board AFTER second version, AND all is working again.

SO can someone look at my optimized version of code AND tell me what is wrong ?

I have warnings turned up and it notes some errors. Note that unlike global variables, local variables are NOT initialized to 0 for you.

sketch_sep05a.ino: In function 'void Transform()':
sketch_sep05a.ino:98:19: warning: 'sta' is used uninitialized in this function [-Wuninitialized]

sketch_sep05a.ino: In function 'void draw()':
sketch_sep05a.ino:114:34: warning: 'x' is used uninitialized in this function [-Wuninitialized]

Since 'x' is used in a while(x<60) loop and x is not modified in the loop, this is likely to hang the 'draw()' function.