Go Down

### Topic: How to scroll a graph pixel by pixel on LCD st7920 (Read 297 times)previous topic - next topic

#### JorgenC

##### Mar 11, 2018, 06:40 pm
When graph shall print to pixelpos 119 I want the graph to scroll one pixel left and put the last value in pos 119, but it always moves 8 pixels at a time with the last value as a 8-pixel line.
In the serial monitor I can see that there is a loop going 8 times with the values moving to  the left before the drawpixel command. Why this loop ? And how can I just take the first loop-values ??
I use a DHT22 sensor on pin 2 and the LCD in serial.

Code: [Select]
`if (y == 119) {    for (x = 9; x < 119; x++) {      hum[x] = hum[x + 1];      temp[x] = temp[x + 1];      if (hum[x] > maxVal) {        hum[x] = maxVal;      }     // Serial.print (x) ; Serial.println(hum[x]);  Just to test                u8g.drawPixel((x), (64 - hum[x] / 1.6));      u8g.drawPixel((x), (64 - temp[x] / 1.6));    }  }  else {    for (x = 9; x < 119; x++) {      if (hum[x] > maxVal) {        hum[x] = maxVal;      }      //Serial.print (x) ; Serial.println(hum[x]);      u8g.drawPixel((x), (64 - hum[x] / 1.6));      u8g.drawPixel((x), (64 - temp[x] / 1.6));    }  }`

#### robtillaart

#1
##### Mar 11, 2018, 10:43 pm

1) What I would consider is to implement a circular buffer for the measurements.
That way I never need to shift them. just update an index

2) I would allways draw the latest value at 119, that means always shift the whole screen
Code: [Select]
`for (i=0...N-1){   remove line[i]   line[i] = line [i+1]}draw line 119`
This way every new value is handled in an indentical manner.
no special cases to handle == shorter code  == easier to understand.

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### JorgenC

#2
##### Mar 12, 2018, 02:53 pmLast Edit: Mar 12, 2018, 03:10 pm by JorgenC
Here is a picture of my graph. It draws pixels one by one and when it reaches the right edge of the frame (x pos 119) I want it to scroll one pixel left  and draw the next pixel at x_pos 119. The frame is at pos 120. First graph pixel is at pos 9.

#### robtillaart

#3
##### Mar 12, 2018, 05:00 pm
OK, algorithm is still same.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### JorgenC

#4
##### Mar 13, 2018, 01:26 pm
OK. Know nothing about circular buffer but, found an example sketch I shall try to use. Thanks!

#### JorgenC

#5
##### Mar 15, 2018, 01:01 am
I've solved my problem with  this library :#include <U8g2lib.h>

and this constructor with a frame buffer :

U8G2_ST7920_128X64_F_SW_SPI u8g2(U8G2_R0, /* clock=*/ 13, /* data=*/ 11, /* CS=*/ 10, /*

reset=*/ U8X8_PIN_NONE);

and I used this command :

u8g2.clearBuffer();

draw();

u8g2.sendBuffer();

Go Up