Go Down

Topic: LCD Encoder Menu (Read 1 time) previous topic - next topic

dizzwold

Hi dougp,

 To be honest, I can't answer that.

 The encoder library I'm using is from the PJRC website;
https://www.pjrc.com/teensy/td_libs_Encoder.html

 I'm not skilled enough to read and fully understand the actual library, and assumed that the library automatically places ISR's on both pin's a and b. Allthough I could be wrong.

 Without being disrespectful to Paul who wrote the library, to a novice, such as myself there's not a great deal of information, but enough for me to get results.

 May be I should be looking to use a different library?

 Dizzwold.
I'm not a student or a lecturer. I'm a hobbyist.

dizzwold

Hi,

 For those that were interested regarding rotating the encoder fast and this having some effect on changing the LED status.

 I've soldered a 1206 0.47uf capacitor between the ground pin and pin a and another again between the ground pin and pin b.

 Unfortunately this hasn't solved the problem. It has helped to stablise it and happen's less.

 Dizzwold.
I'm not a student or a lecturer. I'm a hobbyist.

BulldogLowell

Unfortunately this hasn't solved the problem. It has helped to stablise it and happen's less.
what happens less?


dizzwold

Hi,

Quote
what happens less?
Sorry, this come's from previous post's #14, also I've had this thread moved by moderator as I had wrongly posted in Project Guidance.

 When the encoder is rotated fast, for some reason this is effecting the LED state when only the buit in switch should change the LED state.

 I thought it could be as I had no hardware debounce only software debounce.

 Dizzwold.
I'm not a student or a lecturer. I'm a hobbyist.

dougp

If you were to substiture enCount for IRQcounter in my snippet - not the ISR - and define arbitrary values for DIR_CW & DIR_CCW (+1, -1?) it would function correctly.  Essentially, a +1 value value given to encoderCount substitutes for the original pushbutton up keypress.  Likewise -1 for down.
So two neutrinos went into a bar.  Nothing happened.  They were just passing through.

dizzwold

Hi dougp,

 I'm not sure I fully understand you?
Code: [Select]
#define DIR_CW +1
#define DIR_CCW -1
#include <Encoder.h>

Encoder myEnc(20, 21);
int IRQActivity = myEnc.read();
volatile int encCount = 0;

void setup()

  Serial.begin(9600);
}

void loop()
{
 int encoderTurned = 0;  // Reset previous turned value.
  if (IRQActivity != encCount) {
    if (encCount > IRQActivity) encoderTurned = DIR_CW;
    if (encCount < IRQActivity) encoderTurned = DIR_CCW;
    IRQActivity = encCount; // reset one-shot value to be ready for next IRQ
  }
    Serial.println(encCount);
}


 Dizzwold.
I'm not a student or a lecturer. I'm a hobbyist.

dizzwold

Hi,

 In the current sketch, scrolling through the menu's seems to be working fine, with the exception of when rotating the encoder very fast, which sometimes change's the LED state.

 So I've omitted / commented-out the menu scroll to try and fix the "if (input == 2)", pin 53 the itergrated switch of the encoder.

 This has shown me that my toggle status isn't working as it should. It will toggle the LED on and off, but the toggle is not working on the input of the switch. The sketch in it's current state will scroll through the value's / parameter's, but I only whish to do this when the intergrated switch has been toggled.
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 = 10;
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 dizzwold = 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)
{
  long newP = myEnc.read();
  /*{
    if (input == 0)
    {
      if (newP < old)
      {
        old = newP;
        if (currentScreen == 0 && 1)
        {
          currentScreen = numOfScreens - 1;
        }
        else
        {
          currentScreen--;
        }
      }
    }
    else if (input == 1)
    {
      if (newP > old)
      {
        old = newP;
        if (currentScreen == numOfScreens - 1)
        {
          currentScreen = 0;
        }
        else
        {
          currentScreen++;
        }
      }
    }*/

     if (input == 2);
    {
      dizzwold = ! dizzwold;
      digitalWrite(led, dizzwold);
      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 theory I would like to scroll through the menu's, as I can do, then input 2 / pin 53 to toggle in then out to changing the value's.

 Can I ask for your advice on how to get "input == 2" to toggle correctly?

 Dizzwold.
I'm not a student or a lecturer. I'm a hobbyist.

BulldogLowell

Why not just set a timeout that limits how fast you can update the display?

lock out the pulses from the encoder. and allow the LCD screen time to refresh

dougp

Going by the example sketch provided for the library you're using, no debouncing is needed.  At least, none is shown in the example.

Sooo, since the encoder library is doing the debouncing - this may seem radical - I'd dump the input debouncing loop.  Likewise for resolveInputFlags().  You'll still need to debounce the pushbutton and any other switch inputs but the encoder can be handled separately.

Per the example a presumably debounced value is returned in long newP = myEnc.read();.  Replacing your variable names in the snippet I posted could yield something like:

Code: [Select]
/* This code stands alone or in a function
   It is no longer a part of inputAction
*/
long newP = myEnc.read();  //  check for encoder rotation
encoderTurned = 0;  // Reset previous turned value.
  if (long oldP != newP) {
    if (newP > oldP) encoderTurned = DIR_CW;
    if (newP < oldP) encoderTurned = DIR_CCW;
    oldP = newP; // set oldP equal to newP so next rotation can be detected
  }

Now for the code within if(input == 2). This is separated and stands alone in the main code or within a function.  The test if(input == 2) goes away and the only remaining function of the revised code that was within if(input == 2) is to toggle dizzwold.  The LED code can be left in for debugging if desired.

What remains of inputAction could look like this: (untested)
Code: [Select]
void inputAction() {
  if (dizzwold == 0) // adjust screen numbers
  {
    if (encoderTurned == DIR_CW)
    {
      currentScreen++;
    }
    else if (encoderTurned == DIR_CCW)
    {
      currentScreen--;
    }
  }
  else if (dizzwold == 1)  //  adjust parameters
    /*
        parameter adjustment code goes here
    */

  }

And, finally inputAction is dormant until called by something like this:

Code: [Select]
if(encoderTurned != 0){
  inputAction();  // when encoder rotation puts a non-zero value in encoderTurned
                  // call inputAction to respond by adjusting screen or parameter
}


So two neutrinos went into a bar.  Nothing happened.  They were just passing through.

dizzwold

Hi,

Quote
Why not just set a timeout that limits how fast you can update the display?
@BulldogLowell, that's a very good idea.

 @dougp, I've commented out the software debounce as suggested and applied some of your example code. This works to a fashion. When the encoder is rotated slowly it will count in + or - 1, but any faster it counts in 2's, ie 1,3,5,7,9 etc. So it may need a debounce.

 If I re-apply the software debounce it takes many turns of the encoder for it to count.

 Here's the current sketch;
Code: [Select]
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#include <Encoder.h>
Encoder myEnc(20, 21);
#define DIR_CW +1
#define DIR_CCW -1

//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 = 10;
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 dizzwold = false;
int parameters[numOfScreens];
long oldP = 0;
int encoderTurned = 0 / 4;

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

void loop() {
  enc();
  setInputFlags();
  resolveInputFlags();
}

void enc() {
  long newP = myEnc.read();  //  check for encoder rotation
  encoderTurned = 0;  // Reset previous turned value.
  if ( oldP != newP) {
    if (newP > oldP) encoderTurned = DIR_CW;
    if (newP < oldP) encoderTurned = DIR_CCW;
    oldP = newP; // set oldP equal to newP so next rotation can be detected
  }
}

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 (dizzwold == 0) // adjust screen numbers
  {
    if (encoderTurned == DIR_CW)
    {
      currentScreen++;
    }
    else if (encoderTurned == DIR_CCW)
    {
      currentScreen--;
    }
  }
}

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.
I'm not a student or a lecturer. I'm a hobbyist.

dougp

Where is the
Code: [Select]
else if (dizzwold == 1)
// etc.
part of inputAction?
So two neutrinos went into a bar.  Nothing happened.  They were just passing through.

dizzwold

Hi,

 I've now added the rest of the code and attempted to add the encoder switch to toggle between scrolling through the menu's or changing the values.

 I've removed input 2 from the array and gone with a button on pin 53 and digital read.

 Again I don't have the switch toggling correctly. The LED will toggle correctly. The menu's will scroll and so will the value's with each toggle, but only while the switch is pressed and held pressed in each toggle state.

 Also the menu's and value's count in 2s, 2, 4, 6, 8 etc instead of 1, 2, 3, 4, 5, 6 etc.
Code: [Select]
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#include <Encoder.h>
Encoder myEnc(20, 21);
#define DIR_CW +1
#define DIR_CCW -1

//Input & Button Logic
const int numOfInputs = 2;
const int inputPins[numOfInputs] = {20, 21};
int inputState[numOfInputs];
int lastInputState[numOfInputs] = {LOW, LOW};
bool inputFlags[numOfInputs] = {LOW, LOW};
//long lastDebounceTime[numOfInputs] = {0, 0};
//long debounceDelay = 10;
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 dizzwold = false;
int parameters[numOfScreens];
long oldP = 0;
int encoderTurned = 0;

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

void loop() {
  enc();
  setInputFlags();
  resolveInputFlags();
  if (digitalRead(button) == true)
  {
    dizzwold = !dizzwold;
    digitalWrite(led, dizzwold);
    Serial.print(dizzwold);
  }
  while (digitalRead(button) == true);
  delay(50); // keeps a small delay
}

void enc() {
  long newP = myEnc.read();  //  check for encoder rotation
  encoderTurned = 0;  // Reset previous turned value.
  if ( oldP != newP) {
    if (newP > oldP) encoderTurned = DIR_CW;
    if (newP < oldP) encoderTurned = DIR_CCW;
    oldP = newP; // set oldP equal to newP so next rotation can be detected
  }
}

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 (dizzwold == 0) // adjust screen numbers
  {
    if (encoderTurned == DIR_CW)
    {
      currentScreen++;
    }
    else if (encoderTurned == DIR_CCW)
    {
      currentScreen--;
    }
  }
  else if (dizzwold == 1)  //  adjust parameters
  {
    if (encoderTurned == DIR_CW)
    {
      parameterChange(0);
    }
    else if (encoderTurned == DIR_CCW)
    {
      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.
I'm not a student or a lecturer. I'm a hobbyist.

dizzwold

Hi,

 I've figured out the toggle switch issue. I hade true when I needed false!

 Now I need to figure why it is counting irraticaly.
Code: [Select]
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#include <Encoder.h>
Encoder myEnc(20, 21);
#define DIR_CW +1
#define DIR_CCW -1

//Input & Button Logic
const int numOfInputs = 2;
const int inputPins[numOfInputs] = {20, 21};
int inputState[numOfInputs];
int lastInputState[numOfInputs] = {LOW, LOW};
bool inputFlags[numOfInputs] = {LOW, LOW};
//long lastDebounceTime[numOfInputs] = {0, 0};
//long debounceDelay = 10;
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 lastbutton = false;
int led = 13;
int dizzwold = false;
int parameters[numOfScreens];
long oldP = 0;
int encoderTurned = 0;

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

void loop() {
  enc();
  setInputFlags();
  resolveInputFlags();
  if (digitalRead(button) == false)
  {
    dizzwold = !dizzwold;
    digitalWrite(led, dizzwold);
    Serial.print(dizzwold);
  }
  while (digitalRead(button) == false);
  delay(50); // keeps a small delay
}

void enc() {
  long newP = myEnc.read();  //  check for encoder rotation
  encoderTurned = 0;  // Reset previous turned value.
  if ( oldP != newP) {
    if (newP > oldP) encoderTurned = DIR_CW;
    if (newP < oldP) encoderTurned = DIR_CCW;
    oldP = newP; // set oldP equal to newP so next rotation can be detected
  }
}

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 (dizzwold == 0) // adjust screen numbers
  {
    if (encoderTurned == DIR_CW)
    {
      currentScreen++;
    }
    else if (encoderTurned == DIR_CCW)
    {
      currentScreen--;
    }
  }
  else if (dizzwold == 1)  //  adjust parameters
  {
    if (encoderTurned == DIR_CW)
    {
      parameterChange(0);
    }
    else if (encoderTurned == DIR_CCW)
    {
      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.
I'm not a student or a lecturer. I'm a hobbyist.

dougp

Try this:

Comment out calls to  setInputFlags() and resolveInputFlags().

After call to enc() call inputAction() if encoder has turned.
So two neutrinos went into a bar.  Nothing happened.  They were just passing through.

dizzwold

Hi dougp,

 If I've done this correctly, all in the void loop, sorry but no joy just a blank screen.
Code: [Select]
void loop() {
  enc();
  for (int i = 0; i < numOfInputs; i ++)
  {
    if (encoderTurned != 0);
    {
      inputAction(i);
    }
  }
  //setInputFlags();
  //resolveInputFlags();
  if (digitalRead(button) == false)
  {
    dizzwold = !dizzwold;
    digitalWrite(led, dizzwold);
    Serial.print(dizzwold);
  }
  while (digitalRead(button) == false);
  delay(50); // keeps a small delay
}


 With adding "inputAction();" I cot a compile error stating too few arguments (int), so I added the for i loop.

 Here's the full sketch;
Code: [Select]
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#include <Encoder.h>
Encoder myEnc(20, 21);
#define DIR_CW +1
#define DIR_CCW -1

//Input & Button Logic
const int numOfInputs = 2;
const int inputPins[numOfInputs] = {20, 21};
int inputState[numOfInputs];
int lastInputState[numOfInputs] = {LOW, LOW};
bool inputFlags[numOfInputs] = {LOW, LOW};
//long lastDebounceTime[numOfInputs] = {0, 0};
//long debounceDelay = 10;
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 lastbutton = false;
int led = 13;
int dizzwold = false;
int parameters[numOfScreens];
long oldP = 0;
int encoderTurned = 0;

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

void loop() {
  enc();
  for (int i = 0; i < numOfInputs; i ++)
  {
    if (encoderTurned != 0);
    {
      inputAction(i);
    }
  }
  //setInputFlags();
  //resolveInputFlags();
  if (digitalRead(button) == false)
  {
    dizzwold = !dizzwold;
    digitalWrite(led, dizzwold);
    Serial.print(dizzwold);
  }
  while (digitalRead(button) == false);
  delay(50); // keeps a small delay
}

void enc() {
  long newP = myEnc.read();  //  check for encoder rotation
  encoderTurned = 0;  // Reset previous turned value.
  if ( oldP != newP) {
    if (newP > oldP) encoderTurned = DIR_CW;
    if (newP < oldP) encoderTurned = DIR_CCW;
    oldP = newP; // set oldP equal to newP so next rotation can be detected
  }
}

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 (dizzwold == 0) // adjust screen numbers
  {
    if (encoderTurned == DIR_CW)
    {
      if (currentScreen == numOfScreens - 1)
      {
        currentScreen = 0;
      }
      else
      {
        currentScreen++;
      }
    }
    else if (encoderTurned == DIR_CCW)
    {
      if (currentScreen == 0 && 1)
      {
        currentScreen = numOfScreens - 1;
      }
      currentScreen--;
    }
  }



  else if (dizzwold == 1)  //  adjust parameters
  {
    if (encoderTurned == DIR_CW)
    {
      parameterChange(0);
    }
    else if (encoderTurned == DIR_CCW)
    {
      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.
I'm not a student or a lecturer. I'm a hobbyist.

Go Up