Temperature graph, possibly incorrect shifting of array values

Hello, my problem is that the code below correctly gets the temperature (which is converted from double to int, but it doesn't matter now), correctly moves to the right, it successfully prints the pixels UNTIL I reach the right side of the screen.
Then some shifting occurs, but as shown in my video- it shifts several values at once and then prints a straight line at some specified level. The same happens with millis() limit changed to 10 seconds.
What is wrong with my reasoning/algorithm?

For the purpose of video I changed the millis() limit to 1 second, instead of 1 minute.
PS. Should drawgraph and loop be synchronised?

int x=0;//index of array
int rev=0; //indicates that the screen was filled from left to right, x=63 was reached
int array[64];
int tempsave;//saved temperature

U8GLIB_DOGM128 u8g(11, 10, U8G_PIN_NONE); //U8GLIB_DOGM128(cs, a0 [, reset]) 

void setup()
{
    memset(array,0,sizeof(array));
    
}
void loop()
{
    // READ DATA
    int chk = DHT.read22(DHT22_PIN);
    
    unsigned long currentMillis = millis();
    static unsigned long lastMillis = 0;
    // This code gets executed every minute
    if ((currentMillis - lastMillis) >= (60*1000UL)) 
    {
       lastMillis += (60*1000UL);
       tempsave = DHT.temperature;
       if(x<64)
        {
		array[x] = tempsave;
		x++;
    	}
		
       else
	{
		rev=1;
		array[63] = tempsave;
	}
    }
    
    // The rest gets executed every time
    u8g.firstPage();  
    do {
      draw();
      
    } while( u8g.nextPage() );
    
    // rebuild the picture after some delay
    delay(800);
}

void draw(void) {
    drawgraph(rev);
}

void drawgraph(int &rev) { 
  
    if(rev == 0)
    {
	
        for(int i=0;i<=x;i++)
        {
           u8g.drawPixel(i,array[i]);
        }
        
    } 
    else 
    {
	for(int i=0;i<63;i++)
	{
	  array[i]=array[i+1];
  	  u8g.drawPixel(i,array[i]);
	}
    }
}
       else
	{
		rev=1;
		array[63] = tempsave;
	}

This will cause array[63] to be over written time and time again.

Mark

Oh and DON'T call it array give it a sensible name.

@holmes4
Yes, that's correct. I want it to happen.
After array is populated with values, a shift should happen and the rightmost field should be updated with new temperature reading, while everything on the left is moved leftwards.

After array is populated with values, a shift should happen and the rightmost field should be updated with new temperature reading, while everything on the left is moved leftwards.

The draw function should do exactly that - draw the data. It is NOT where the shifting should happen. The shifting should happen when the array gets full, before you add the new value at the end.

Logically separating code like you are doing is not the way to write code that works well.

I think the problem is here:

 // The rest gets executed every time
  u8g.firstPage();  
  do {
    draw();
  } 
  while( u8g.nextPage() );

Since you shift the data every time 'draw()' is called it is getting shifted even when there is no new data. I suspect there is no need to update the graph when there is no new data.

The published sketch will not compile because the necessary libraries are not included.

PaulS:

After array is populated with values, a shift should happen and the rightmost field should be updated with new temperature reading, while everything on the left is moved leftwards.

The draw function should do exactly that - draw the data. It is NOT where the shifting should happen. The shifting should happen when the array gets full, before you add the new value at the end.

Logically separating code like you are doing is not the way to write code that works well.

Thank You. Moving

for(int i=0;i<63;i++)
	{
	  array[i]=array[i+1];
  	}

to loop function (precisely to if>=64 condition) helped and solved the problem.

@johnwasser
I know :wink: I got rid of all the unnecessary code including #include