Hi!
I have a project, that uses a module which has variable output. The module communicates using RS485. To interface with it, I use an LCD and 2 buttons. One button is of digit selection, and the other is for value (+1).
A slight issue I have is that the screen is slightly visibly flickering.
I am in no way a good programmer, so I also want some comments on my way of execution. I feel that the way I change the module with variables is a bit hacky, so I am open for suggestions.
#include <LiquidCrystal.h>
// Module command
char command[12] = "#010+00.000\r";
//Temporary storage for conversions.
char writetemp;
char readtemp;
int readtemp_int;
// Pin declarations
const int sw = 7;
const int sw2 = 8;
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
// Switch counters
int digitselect = 5;
//Display
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
pinMode(8, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
}
void loop() {
// Reads button state
int sw_state = digitalRead(sw);
int sw2_state = digitalRead(sw2);
//Enables blinking on lcd (To know which digit is selected).
lcd.blink();
// DIGIT SELECTION
if (sw_state == 0) {
delay(100);
digitselect++;
if (digitselect-5 == 2) { digitselect++;}
if (digitselect > 10) { digitselect = 5;}
}
// Clears the previous value.
delay(50);
lcd.clear();
// Print value on lcd
for (int b = 5;b > 4 && b < 11; b++) {
lcd.print(command[b]);
}
lcd.print(" mA");
// Sets the cursor on the selected digit.
lcd.setCursor(digitselect-5, 0);
//Reads selected digit from command array.
readtemp = command[digitselect];
//Converts character to integer
readtemp_int = readtemp - '0';
// If button is pressed, increases integer by 1,
// in case integer is > 9 resets to 0.
if (sw2_state == 0) {
delay(100);
readtemp_int++;
if (readtemp_int > 9) {
readtemp_int = 0;
}
// Converts integer back to character.
writetemp = '0' + readtemp_int;
//Changes digit in character array.
command[digitselect] = writetemp;
}
//Sends command to module.
Serial.write(command);
}