Hello,
I recently purchased this LCD module from amazon to use in a metronome project:
In conjunction with an Arduino Nano 5V 16 Mhz, I was able to get the display to run the example given in the Arduino_GFX Library by moonournation:
I proceeded to set up a rotary encoder to increment/decrement an integer and display it on the LCD.
The problem I have is changing the value on the LCD. The best way I've found to get rid of the old text so far is by printing over the old value with the color set to be that of the Background. Then proceed to print the new value (in non-background color). This is the best solution found so far but parts of the LCD still "flash" when updating values. It's minute but still noticeable and undesirable.
I'm wondering if this is a limitation of the nano or if there's a better solution in the code. If anyone has had any experience with it, I would be grateful to hear. My code is posted below. Thank you.
#include <SPI.h>
#include <RotaryEncoder.h>
#include <Arduino_GFX_Library.h>
#include <Arduino.h>
#include <Adafruit_GFX.h>
#include <Fonts/FreeSans18pt7b.h>
#include <Fonts/FreeSans12pt7b.h>
// Example for Arduino UNO with input signals on pin 2 and 3
#define PIN_IN1 2
#define PIN_IN2 3
#define INC_ACC 4
#define DEC_ACC 5
#define BUZZER 10
//Metronome Parameters
int newBPM = 0;
int bpm = 100;
int minBPM = 40;
int maxBPM = 240;
int accent = 0;
char buf[20];
#if defined(DISPLAY_DEV_KIT)
Arduino_GFX *gfx = create_default_Arduino_GFX();
#else /* !defined(DISPLAY_DEV_KIT) */
Arduino_DataBus *bus = create_default_Arduino_DataBus();
Arduino_GFX *gfx = new Arduino_GC9A01(bus, 7 /* RST */, 0 /* rotation */, true /* IPS */);
#endif /* !defined(DISPLAY_DEV_KIT) */
// Setup a RotaryEncoder with 2 steps per latch for the 2 signal input pins:
RotaryEncoder encoder(PIN_IN1, PIN_IN2, RotaryEncoder::LatchMode::TWO03);
void SetBpm(){
encoder.tick();
newBPM = encoder.getPosition()/2;
if (bpm != newBPM) {
if(newBPM > maxBPM){ newBPM = maxBPM;encoder.setPosition(maxBPM*2);}
else if(newBPM < minBPM) { newBPM = minBPM; encoder.setPosition(minBPM*2);}
//Erase Previous
gfx->setFont(&FreeSans18pt7b);
gfx->setCursor(75, 75);
gfx->setTextColor(BLACK);
gfx->setTextSize(2);
gfx->print(bpm);
//Draw New
gfx->setFont(&FreeSans18pt7b);
gfx->setCursor(75, 75);
gfx->setTextColor(BLUE);
gfx->setTextSize(2);
gfx->print(newBPM);
//Draw BPM
gfx->setFont(&FreeSans18pt7b);
gfx->setCursor(75, 150);
gfx->setTextSize(1);
gfx->print(" ");
gfx->println("BPM");
// Serial.print("pos:");
// Serial.print(newBPM);
// Serial.print(" dir:");
// Serial.println((int)(encoder.getDirection()));
bpm = newBPM;
}
}
void setup() {
Serial.begin(9600);
Serial.println("Starting Encoder");
encoder.setPosition(200);
//Accent Buttons
pinMode(INC_ACC, INPUT_PULLUP);
pinMode(DEC_ACC, INPUT_PULLUP);
//Buzzer
pinMode(BUZZER, OUTPUT);
//LCD
gfx->begin();
gfx->fillScreen(BLACK);
#ifdef DF_GFX_BL
pinMode(DF_GFX_BL, OUTPUT);
digitalWrite(DF_GFX_BL, HIGH);
#endif
gfx->setCursor(50, 50);
gfx->setTextColor(RED);
gfx->setFont(&FreeSans18pt7b);
gfx->println("Hello World!");
Serial.println("Printed hollo");
delay(2000); // 5 second
gfx->setCursor(50, 50);
gfx->fillScreen(BLACK);
}
void loop() {
SetBpm();
}