Hi all,
I've been working with a sktch writen by Vladimir Ronamov found here;
The sketch was writen for 4 button swtches and I'm trying to re-write it to use an encoder.
So-far I've managed to get the encoder to scroll through the menu's, but I'm struggling to find a way to enter the values and change them.
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#include <Encoder.h>
Encoder myEnc(20, 21);
//Input & Button Logic
const int numOfInputs = 3;
const int inputPins[numOfInputs] = {20, 21, 53};
int inputState[numOfInputs];
int lastInputState[numOfInputs] = {LOW, LOW, LOW};
bool inputFlags[numOfInputs] = {LOW, LOW, LOW};
long lastDebounceTime[numOfInputs] = {0, 0, 0};
long debounceDelay = 5;
int backlight_pin10 = 10;
//LCD Menu Logic
const int numOfScreens = 10;
int currentScreen = 0;
const char* screens[numOfScreens][2] = {{"1Motor Voltage", "Volts"}, {"2Motor Current", "Amps"},
{"3Motor Rated HP", "HP"}, {"4Overload Temp.", "degC"}, {"5Accel Time", "Secs"}, {"6Restart Time", "Mins"},
{"7Analog Out. Curr.", "mA"}, {"8Input Temp.", "degC"}, {"9Run Time", "Hours"}, {"10Start Times", "times"}
};
int button = 53;
int led = 13;
int status = false;
int parameters[numOfScreens];
void setup() {
pinMode(backlight_pin10, OUTPUT);
analogWrite(10, 200);
for (int i = 0; i < numOfInputs; i++) {
pinMode(inputPins[i], INPUT);
digitalWrite(inputPins[i], HIGH); // pull-up 20k
}
pinMode(led, OUTPUT);
pinMode(button, INPUT_PULLUP); // set the internal pull up resistor, unpressed button is HIGH
Serial.begin(9600);
lcd.begin(20, 4);
}
long old = 0;
void loop() {
setInputFlags();
resolveInputFlags();
}
void setInputFlags() {
for (int i = 0; i < numOfInputs; i++) {
int reading = digitalRead(inputPins[i]);
if (reading != lastInputState[i]) {
lastDebounceTime[i] = millis();
//Serial.println(reading);
}
if ((millis() - lastDebounceTime[i]) > debounceDelay) {
if (reading != inputState[i]) {
inputState[i] = reading;
if (inputState[i] == HIGH) {
inputFlags[i] = HIGH;
}
}
}
lastInputState[i] = reading;
}
}
void resolveInputFlags() {
for (int i = 0; i < numOfInputs; i++) {
if (inputFlags[i] == HIGH) {
inputAction(i);
inputFlags[i] = LOW;
printScreen();
}
}
}
void inputAction(int input)
{
if (digitalRead(button) == true)
{
status = !status;
digitalWrite(led, status);
Serial.print(status);
}
delay(50); // keeps a small delay
long newP = myEnc.read() / 4;
{
if (newP < old)
{
old = newP;
if (currentScreen == 0 && 1)
{
currentScreen = numOfScreens - 1;
}
else
{
currentScreen--;
}
}
if (newP > old)
{
old = newP;
if (currentScreen == numOfScreens - 1)
{
currentScreen = 0;
}
else
{
currentScreen++;
}
}
}
if (digitalRead(button) == false);
{
status = !status;
delay(50); // keeps a small delay
{
if (newP < old)
old = newP;
{
parameterChange(1);
}
if (newP > old)
old = newP;
{
parameterChange(0);
}
}
}
}
void parameterChange(int key) {
if (key == 0) {
parameters[currentScreen]++;
} else if (key == 1) {
parameters[currentScreen]--;
}
}
void printScreen() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(screens[currentScreen][0]);
lcd.setCursor(0, 1);
lcd.print(parameters[currentScreen]);
lcd.print(" ");
lcd.print(screens[currentScreen][1]);
}
I would like to ask for some guidance.
Dizzwold.