Your loop() rewrites the entire display every time it runs. Alter your logic so that you only write to the display when something changes. This is the main thing affecting the speed of your sketch.
This:
if (Encoderbutton == LOW) {
// encoder button was pressed, increment menu position
MenuSelect++;
if (MenuSelect >= 8) {
MenuSelect = 0;
}
else (MenuSelect);
}
Will cycle through the menu select as fast as possible for as long as the button is held down. Probably not what you want. What you need is "if the button is held down, and either it wasn't held down last time I looped or it has been held down for more than 200ms since last time I incremented the variable, increment the variable".
Oh, and can you please stop doing this:
else (ModeSelect);{}
It compiles, but it doesn't mean anything.
Varioius other things.
const int button2 = 23; //top button
const int button3 = 24; //middle button
const int button4 = 25; //bottom button
Why not name the variables "topButton", "middleButton", and "bottomButton"? "button3" doesn't tell you anything.
I always like to suffix variables containing numbers with "Pin", eg, "middleButtonPin". That way, you can see in code if you are supposed to be using the value or if you are supposed to be digital reading it.
Note this:
if (Button3 == LOW) {
// turn LED on:
//Serial.println("Button 3 pushed");
ModeSelect++;
if (ModeSelect > 2) {
ModeSelect = 0;
}
else (ModeSelect);
}
The buttton is named "button 3", but it's entirely to do with the mode selection. And using upper and lower case is a really, really bad idea. Why not
modeSelectButton = digitalRead(modeSelectButtonPin);
if (modeSelectButton == LOW) {
// turn LED on:
//Serial.println("Mode select pushed");
ModeSelect++;
if (ModeSelect > 2) {
ModeSelect = 0;
}
}
Rather than
void loop() {
if(BandSelect == 0){
tft.println("40M Band");
}
else if(BandSelect == 1){
tft.println("20M Band");
}
else if(BandSelect == 2){
tft.println("10M Band");
}
else if(BandSelect == 3){
tft.println("6M Band ");
}
}
I would tend to use
char *bandName[] = {
"40M Band",
"20M Band",
"10M Band",
"6M Band "
};
void loop() {
tft.println(bandName[BandSelect]);
}
I would also be inclined to use a class for these selection things of yours.
class ButtonLooper {
const int N; // the looper will loop between 0 and N-1
const byte pin;
const uint32_t repeatRate;
int n = 0; // current number
uint32_t mostRecentRepeat;
byte state = HIGH;
public:
ButtonLooper(int setN, byte attachPin, uint32_t setRepeatRate) :
N(setN), pin(attachPin), repeatRate(setRepeatRate) {
}
void setup() {
pinMode(pin, INPUT_PULLUP);
}
// rewturns true if n has *changed* as a result of this read.
boolean read() {
byte prevState = state;
state = digitalRead(pin);
if (state == HIGH) return false;
if (prevState == LOW && millis() - mostRecentRepeat < repeatRate) return false;
mostRecentRepeat = millis();
if (++n >= N) n = 0; // note the use of pre-increment!
return true;
}
int getN() {
return n;
}
};
ButtonLooper bandSelect(4, 23, 200); // four states 0-3, pin 23, 200ms delay
ButtonLooper modeSelect(3, 24, 200); // three states 0-2, pin 24, 200ms delay
void setup() {
bandSelect.setup();
modeSelect.setup();
}
void loop() {
boolean needToRedraw = false;
if (bandSelect.read()) needToRedraw = true;
if (modeSelect.read()) needToRedraw = true;
if (needToRedraw) {
// redraw the LCD here.
// use 'bandSelect.getN()' to get the value.
}
}