Nokia 5510 screen, efficiently drawing lines based on joystick input

Hi people,

I need some small assistance in making efficient way to use my Nokia 5110 screen. It has PCD8544 chip and I’m using LCD5110_Graph library from RinkyDinkyElectronics.com:

The manual of library.

I’m trying to make a boat similar to this. The remote will have a joystick module, 5110 screen to display battery status, signal quality, visual representation of acceleration and directions and I will use nrf24l01 for wireless transmission.

The joystick module is connected to analog inputs of arduino. And I’m trying to display a progressbar like graphics on my screen to reflect how much the joystick’s X and Y axis’ are pushed

The thing is, due to the way I’m appying myGLCD.clrScr(); command in void loop which clears screen, I get screen flickers. But if I choose not to use myGLCD.clrScr(); then any shape that I drew remains on the screen. I mean my progressbar DOES NOT get updated once the the joystick is retracted.

I’m using myGLCD.drawLine(x,y,x,y); command and I know I must use myGLCD.clrLine(x,y,x,y); command afterwards to solve this issue. But I can’t wrap my head around a code that checks how much of joystick is pressed and released and then draws & clears lines on the screen based on that.

Please watch this video to see the code in action:

Here 's my code so far:

#include <LCD5110_Graph.h>  
LCD5110 myGLCD(8,9,10,11,12);
extern unsigned char TinyFont[];
extern uint8_t BigNumbers[];

void setup() {
pinMode(7, OUTPUT);
digitalWrite(7, LOW); //5110 backlight
Serial.begin(9600);
myGLCD.InitLCD();
}

void loop() {
myGLCD.clrScr();
int speed = analogRead(0);
int direction = analogRead(1);
int speed_forward = map(speed,0,540,0,23);
speed_forward = constrain(speed_forward,0,23);
int speed_backward = map(speed,540,1024,23,48);
speed_backward= constrain(speed_backward,23,48);
frame();
accelerometer(speed_forward, speed_backward);
//Serial.print(speed);
//Serial.print(" ");
//Serial.print(direction);
//Serial.println(" ");
delay(100);
}

void frame() {
myGLCD.drawLine(0,47,10,47);
myGLCD.drawLine(0,0,10,0);
myGLCD.drawLine(0,0,0,47);
myGLCD.drawLine(10,0,10,47);
myGLCD.drawLine(0,23,10,23);
myGLCD.update();
}

void accelerometer(int line_forward, int line_backward) {
myGLCD.drawLine(1,23,1,line_forward);
myGLCD.drawLine(2,23,2,line_forward);
myGLCD.drawLine(3,23,3,line_forward);
myGLCD.drawLine(4,23,4,line_forward);
myGLCD.drawLine(5,23,5,line_forward);
myGLCD.drawLine(6,23,6,line_forward);
myGLCD.drawLine(7,23,7,line_forward);
myGLCD.drawLine(8,23,8,line_forward);
myGLCD.drawLine(9,23,9,line_forward);
myGLCD.drawLine(10,23,10,line_forward);

myGLCD.drawLine(1,23,1,line_backward);
myGLCD.drawLine(2,23,2,line_backward);
myGLCD.drawLine(3,23,3,line_backward);
myGLCD.drawLine(4,23,4,line_backward);
myGLCD.drawLine(5,23,5,line_backward);
myGLCD.drawLine(6,23,6,line_backward);
myGLCD.drawLine(7,23,7,line_backward);
myGLCD.drawLine(8,23,8,line_backward);
myGLCD.drawLine(9,23,9,line_backward);
myGLCD.drawLine(10,23,10,line_backward);


myGLCD.update();
Serial.print(line_forward);
Serial.print(" ");
Serial.println(line_backward);

}

My immediate reaction is that you should not clear the screen every time through loop(). It is only necessary to redraw the screen (or parts of it) when something changes. For instance, why do you call frame() every time through loop when the frame will always be the same ? The same goes for the lines drawn in accelerometer(). Draw the lines once and only print line_forward and line_backward when their value changes

The principle should be to draw the fixed parts of the display once in setup() and then to update only the parts of the screen that change and only then when they actually change

In order to detect a change in joystick position read it every time through loop() but save the value before reading it again. If the new value differs significantly (you decide what constitutes a significant change) from the previous value then erase the previous value from the screen and replace it with the new one.