Author Topic: LCD Menu with Keys Encoder  (Read 48 times)

dizzwold

  • Newbie
  • *
  • Posts: 9
    • View Profile
LCD Menu with Keys Encoder
« on: November 29, 2017, 02:47:04 PM »
Hi All,

 Firstly can I say thank you for your time, effort and in depth explanations. Excellent work!

 Sadly I can't figure out my own faults.

 I've changed the string array to a char array, as I've always been tols to stir clear of string arrays. This seemed to work fine.

 I've then added a backlight control. Again this seemed fine.

 I would like to use a Keys Rotary Encoder to scroll through the menu's and use the swith on the encoder to then select to change the values. I'm using the PJRC Encoder Library.

 So far I have change the void inputAction to what I think should scroll through the menu's, but I have failed as I get an Error for inputActions(i); in void resolveInputFlags.

 Would someone be good enough to guide me to correct this error so I can move to the next stage of using the Encoder to select values.

 My end aim is for the Encoder to scroll through the menu's, then click the Encoder switch to then scroll through values to set them, then click the Encoder switch again to then again scroll through the menu's.

 The LCD Menu with 3 Buttons may have been of some help, but unfortunatly to sketch has not been posted.
Code: [Select]
#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] = {53, 44, 45};
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] = {{"Motor Voltage", "Volts"}, {"Motor Current", "Amps"},
  {"Motor Rated HP", "HP"}, {"Overload Temp.", "degC"}, {"Accel Time", "Secs"}, {"Restart Time", "Mins"},
  {"Analog Out. Curr.", "mA"}, {"Input Temp.", "degC"}, {"Run Time", "Hours"}, {"Start Times", "times"}
};
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
  }
  Serial.begin(9600);
  lcd.begin(20, 4);
}

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();
    }
  }
}

long old = -999:

void inputAction(int input)
{
  long newP = myEnc.read();
  {
    if (newP < old)
    {
      if (currentScreen == 0)
      {
        currentScreen = numOfScreens - 1;
      }
      else
      {
        currentScreen--;
      }
    }
    if (newP > old)
    {
      if (currentScreen == numOfScreens - 1)
      {
        currentScreen = 0;
      }
      else
      {
        currentScreen++;
      }
    }
  }

  else if (input == 1)
  {
    parameterChange(0);
  }
  else if (input == 2)
  {
    parameterChange(1);
  }
}

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]);
}

 Regards.

 Dizzwold.
« Last Edit: November 29, 2017, 02:49:38 PM by dizzwold »

dizzwold

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: LCD Menu with Keys Encoder
« Reply #1 on: November 30, 2017, 10:20:30 AM »
Hi,

 I found the compile error.

 I had an : instead of a ; on the line "long old = -999"

 This then gave me another error, to which I have uncommented the parameterchange and also change the input pins.

 This will now scroll through the menu's, which is a start, but it now starts / on boot with menu 4, Overload Temp. This would be because it's a quadarature encoder.

 I've tried placing divide by 4 "/4", in a few places but no have worked.

 Any ideas where or how to rectify this?

 Dizzwold.

*Edit. Forgot to add the code. Duh!
Code: [Select]
#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] = {{"Motor Voltage", "Volts"}, {"Motor Current", "Amps"},
  {"Motor Rated HP", "HP"}, {"Overload Temp.", "degC"}, {"Accel Time", "Secs"}, {"Restart Time", "Mins"},
  {"Analog Out. Curr.", "mA"}, {"Input Temp.", "degC"}, {"Run Time", "Hours"}, {"Start Times", "times"}
};
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
  }
  Serial.begin(9600);
  lcd.begin(20, 4);
}

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();
    }
  }
}

long old = -999;

void inputAction(int input)
{
  long newP = myEnc.read();
  {
    if (newP < old)
    {
      if (currentScreen == 0)
      {
        currentScreen = numOfScreens - 1;
      }
      else
      {
        currentScreen--;
      }
    }
    if (newP > old)
    {
      if (currentScreen == numOfScreens - 1)
      {
        currentScreen = 0;
      }
      else
      {
        currentScreen++;
      }
    }
  }

/*  else if (input == 1)
  {
    parameterChange(0);
  }
  else if (input == 2)
  {
    parameterChange(1);
  }*/
}

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]);
}

 Dizzwold.
« Last Edit: November 30, 2017, 10:22:24 AM by dizzwold »

dizzwold

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: LCD Menu with Keys Encoder
« Reply #2 on: November 30, 2017, 10:32:04 AM »
Hi,

 I'm feeling quite lonely hear...

 Anyway fixed the divide by 4 issue "old = newP /4;"
Code: [Select]
#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] = {{"Motor Voltage", "Volts"}, {"Motor Current", "Amps"},
  {"Motor Rated HP", "HP"}, {"Overload Temp.", "degC"}, {"Accel Time", "Secs"}, {"Restart Time", "Mins"},
  {"Analog Out. Curr.", "mA"}, {"Input Temp.", "degC"}, {"Run Time", "Hours"}, {"Start Times", "times"}
};
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
  }
  Serial.begin(9600);
  lcd.begin(20, 4);
}

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();
    }
  }
}

long old = -999;

void inputAction(int input)
{
  long newP = myEnc.read();
  {
    if (newP < old)
    {
      if (currentScreen == 0)
      {
        old = newP /4;
        currentScreen = numOfScreens - 1;
      }
      else
      {
        currentScreen--;
      }
    }
    if (newP > old)
    {
      old = newP /4;
      if (currentScreen == numOfScreens - 1)
      {
        currentScreen = 0;
      }
      else
      {
        currentScreen++;
      }
    }
  }

/*  else if (input == 1)
  {
    parameterChange(0);
  }
  else if (input == 2)
  {
    parameterChange(1);
  }*/
}

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]);
}

 Time to move on to the parameterschanges.

 Dizzwold.

dizzwold

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: LCD Menu with Keys Encoder
« Reply #3 on: November 30, 2017, 12:13:44 PM »
Hi,

 Not fixed.

 It starts at menu 2 Motor Current.

 Any ideas?

 Dizzwold.

dizzwold

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: LCD Menu with Keys Encoder
« Reply #4 on: December 01, 2017, 11:06:40 AM »
Hi All,

 Okay, some progress made.

 The sketch will now count CW through the menu's nicely. Menu 1,2,3,4,5,6,7,8,9,10,1,2,3 etc.

 The problem was CCW as it would jump by 2 menu's. starting at menu 1 it would go as 1,10,8,6,4,2,10,8,6,4 etc.

 This is how I had this part of the sketch;
Code: [Select]
long old = 0;

void inputAction(int input)
{
  long newP = myEnc.read()/4;
  {
    if (newP < old)
    {
      if (currentScreen == 0)
      {
        old = newP;
        currentScreen = numOfScreens - 1;
      }
      else
      {
        currentScreen--;
      }
    }
    if (newP > old)
    {
      old = newP;
      if (currentScreen == numOfScreens - 1)
      {
        currentScreen = 0;
      }
      else
      {
        currentScreen++;
      }
    }
  }

/*  else if (input == 1)
  {
    parameterChange(0);
  }
  else if (input == 2)
  {
    parameterChange(1);
  }*/
}

 This got me thinking, the CW is working fine and noticed in the sketch snippet above that the CW "old > newP or the ++ screens", was different to the CCW "old < newP or the -- screens", so I've tried to change this so they are both writen in a similar fasion;
Code: [Select]
long old = 0;

void inputAction(int input)
{
  long newP = myEnc.read()/4;
  {
    if (newP < old)
    {
      old = newP;
      if (currentScreen == numOfScreens 0)
      {
       
        currentScreen = - 1;
      }
      else
      {
        currentScreen--;
      }
    }
    if (newP > old)
    {
      old = newP;
      if (currentScreen == numOfScreens - 1)
      {
        currentScreen = 0;
      }
      else
      {
        currentScreen++;
      }
    }
  }

/*  else if (input == 1)
  {
    parameterChange(0);
  }
  else if (input == 2)
  {
    parameterChange(1);
  }*/
}

 The problem is I now get a compile error for line "if (currentScreen == numOfScreens 0)", expected ')' before numeric constant. I've tried alsorts of things like ,0 =0 ==0 !=0 -0 +0 but when you go from menu 1 CCW to ten this then start displaying all kind's of rubbish.

 Can anyone advise me on my error and correct me?
Code: [Select]
#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 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
  }
  Serial.begin(9600);
  lcd.begin(20, 4);
}

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();
    }
  }
}

long old = 0;

void inputAction(int input)
{
  long newP = myEnc.read()/4;
  {
    if (newP < old)
    {
      old = newP;
      if (currentScreen == numOfScreens 0)
      {
       
        currentScreen = - 1;
      }
      else
      {
        currentScreen--;
      }
    }
    if (newP > old)
    {
      old = newP;
      if (currentScreen == numOfScreens - 1)
      {
        currentScreen = 0;
      }
      else
      {
        currentScreen++;
      }
    }
  }

/*  else if (input == 1)
  {
    parameterChange(0);
  }
  else if (input == 2)
  {
    parameterChange(1);
  }*/
}

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]);
}

 Dizzwold.

dizzwold

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: LCD Menu with Keys Encoder
« Reply #5 on: December 01, 2017, 12:28:13 PM »
Hi All,

 I've answered my own question and fixed the encoder to scroll both CW and CCW.

 Whoop Whoop!

 Scrolling through the menu's with the encoder is now like feeding strawberries to pig's, if you excuss the my smugness, it's so fast.

 A quick question before I move on to attempt "again", using the encoder's switch / button to enter values;

 Has anyone modified the sketch for a 20,04 display so it displays menu's 1 & 2, then 3 & 4, 5 & 6, 7 & 8, 9 & 10?

 Current sketch with encoder scrolling through the menu's CW and CCW;
Code: [Select]
#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 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
  }
  Serial.begin(9600);
  lcd.begin(20, 4);
}

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();
    }
  }
}

long old = 0;

void inputAction(int input)
{
  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++;
      }
    }
  }

  /*  else if (input == 1)
    {
      parameterChange(0);
    }
    else if (input == 2)
    {
      parameterChange(1);
    }*/
}

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 hope other\s may find this useful.

 Dizzwold.

dizzwold

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: LCD Menu with Keys Encoder
« Reply #6 on: December 01, 2017, 02:09:32 PM »
Hi All,

 I could do with a little help with this part, to use the encoder's switch to change the value's.

 I can't stop the void inputaction from running to allow the switch to let me enter the values?
Code: [Select]
#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 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
  }
  Serial.begin(9600);
  lcd.begin(20, 4);
}

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);
      encSw(i);
      inputFlags[i] = LOW;
      printScreen();
    }
  }
}

long old = 0;

void inputAction(int input)
{
  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++;
      }
    }
  }
}

void encSw(int input)
{
  if (input == 2)
    //if (digitalRead(53) == LOW)
  {
    long newP = myEnc.read() / 4;
    {
      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]);
}

 Could someone adivse me where I'm going wrong with this part?

 Dizzwold.

dizzwold

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: LCD Menu with Keys Encoder
« Reply #7 on: December 02, 2017, 01:03:37 PM »
Hi All,

 I've re thought this thinking I need the switch to toggle / latch.

 I've also put all the relevent code back into the void inputAction.

 This still scrolls through the menu's, but I still can't get access to change the value's.

 Can anyone help?
Code: [Select]
#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]);
}

 Dizzwold.