Gonna dump my script here incase someone needs to see it.
//20x4 LCD
#include <LiquidCrystal_I2C.h> //SDA = A4, SCL = A5 Arduino https://www.arduino.cc/reference/en/libraries/liquidcrystal-i2c/
LiquidCrystal_I2C lcd(0x27, 20, 4);
///New ROTMETHOD
#define LEFT 2
#define RIGHT 3
#define PUSH 4
///New ROTMETHOD
int menuCounter;
int menu1_Value = 0; //value within menu 1
int menu2_Value = 0; //value within menu 2
int menu2_StoredValue = 0; // STORED VALUE FOR COMPARRISSON
int menu3_Value = 0; //value within menu 3
int menu3_StoredValue = 0; // STORED VALUE FOR COMPARRISSON
char menu4_Value; //value within menu 4
bool menu1_selected = false; //enable/disable to change the value of menu item
bool menu2_selected = false;
bool menu3_selected = false;
bool menu4_selected = false;
//Note: if a menu is selected ">" becomes "X".
bool ColourRepeat = true;
bool refreshLCD = true; //refreshes values
bool refreshSelection = false; //refreshes selection (> / X)
// HE Sensor code
// HE Sensor code
#define DETECT 5 // pin 5 for sensor (D5)
#define ACTION 8 // pin 8 for action to do someting (A1)
// HE Sensor code
// HE Sensor code
const int redRELAY_PIN = 11; // relays state = NO = LOW = OFF
const int greyRELAY_PIN = 12; //
///New ROTMETHOD
uint8_t lrmem = 3;
int lrsum = 0;
int num = 0; //RotEnc Rotation Count.
int8_t res; //RotEnc Movement Detection
bool buttonPushed = false;
///New ROTMETHOD
bool RUNNINGSTATE = false;
unsigned long previousMillis = 0;
const long interval = 1000;
int8_t rotary() ///New ROTMETHOD
{
static int8_t TRANS[] = {0,-1,1,14,1,0,14,-1,-1,14,0,1,14,1,-1,0};
int8_t l, r;
l = digitalRead(LEFT);
r = digitalRead(RIGHT);
lrmem = ((lrmem & 0x03) << 2) + 2*l + r;
lrsum = lrsum + TRANS[lrmem];
/* encoder not in the neutral state */
if(lrsum % 4 != 0) return(0);
/* encoder in the neutral state */
if (lrsum == 4) /* encoder in the neutral state - clockwise rotation*/
{
lrsum=0;
return(1);
}
if (lrsum == -4) /* encoder in the neutral state - anti-clockwise rotation*/
{
lrsum=0;
return(-1);
}
/* lrsum > 0 if the impossible transition */
lrsum=0;
return(0);
}///New ROTMETHOD
void setup()
{
///New ROTMETHOD
pinMode(LEFT, INPUT_PULLUP);
pinMode(RIGHT, INPUT_PULLUP);
pinMode(PUSH, INPUT_PULLUP);
Serial.begin(9600);
Serial.println(menuCounter, DEC);
///New ROTMETHOD
//------------------------------------------------------
lcd.init();//lcd.begin(); /// not known in library lcd.init();/// // initialize the lcd
lcd.backlight();
//------------------------------------------------------
lcd.setCursor(0, 0); //Defining positon to write from first row, first column .
lcd.print("Menu demo");
lcd.setCursor(0, 1); //Second row, first column
lcd.print("Rotary encoder");
lcd.setCursor(0, 2); //Second row, first column
lcd.print("Improved version");
delay(50); //wait 1 sec
lcd.clear(); //clear the whole LCD
printLCD(); //print the static parts on the screen
//------------------------------------------------------
//attachInterrupt(digitalPinToInterrupt(RotaryCLK), rotate, CHANGE); //CLK pin is an interrupt pin
//attachInterrupt(digitalPinToInterrupt(PushButton), pushButton, FALLING); //PushButton pin is an interrupt pin //DOUNCE? USE RISING?
// HE Sensor code
// HE Sensor code
/// Serial.println("Hall Module Test"); ///TESTING///
pinMode(DETECT, INPUT);//define detect input pin
pinMode(ACTION, OUTPUT);//define ACTION output pin
// HE Sensor code
// HE Sensor code
pinMode(redRELAY_PIN, OUTPUT);
pinMode(greyRELAY_PIN, OUTPUT);
digitalWrite(greyRELAY_PIN, HIGH);//RepeatNotColourChanger
digitalWrite(redRELAY_PIN, HIGH);
}
void loop()
{
///New ROTMETHOD
RotationCheck ();
///New ROTMETHOD
unsigned long currentMillis = millis();
if (buttonPushed == true)
{
pushButton();
buttonPushed=false;
}
if (menu4_selected == true) ///MINUS COUNT TRIGGER
{
//delay(500);
if (currentMillis - previousMillis >= interval) {/////RUNNING BLINK
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (RUNNINGSTATE == true) {
lcd.setCursor(9, 3); //4th line, 2nd block
lcd.print("Running"); //text
RUNNINGSTATE = false;
} else {
lcd.setCursor(9, 3); //4th line, 2nd block
lcd.print(" "); //blank previous
RUNNINGSTATE = true;
}
}
if (menu1_Value != 0)
{
// HE Sensor code
// HE Sensor code
int HEreading = digitalRead(DETECT);// read Hall sensor
if ( HEreading == HIGH)
{
menu1_Value = menu1_Value - 1;
if (menu2_Value != 0) //Menu2 value storage
{
if (menu2_StoredValue == 0)
{
menu2_StoredValue = menu2_Value;
}
menu2_Value = menu2_Value - 1;
}
if (menu2_Value < 0)
{
menu2_Value = menu2_StoredValue;
}
//////////////////////////////////////////////////////////
if (menu3_Value != 0) //Menu3 value storage
{
if (menu3_StoredValue == 0)
{
menu3_StoredValue = menu3_Value;
}
menu3_Value = menu3_Value - 1;
}
if (menu3_Value < 0)
{
menu3_Value = menu3_StoredValue;
}
delay(150); ////DELAY FOR LIMITING -1 COUNT
HEreading = LOW;
}
// HE Sensor code
// HE Sensor code
refreshLCD = true;
//delay(500);
}
if (menu1_Value == 0)///RELAY TRIGGER
{
digitalWrite(redRELAY_PIN, HIGH); //StopNotColourChanger
digitalWrite(greyRELAY_PIN, LOW);
menu4_selected = false;
lcd.setCursor(9, 3); //4th line, 2nd block
lcd.print("Complete"); //text
refreshLCD = true;
}
if (menu2_Value == 0 && menu2_StoredValue > 0 && menu1_Value != 0)///RELAY TRIGGER
{
digitalWrite(redRELAY_PIN, HIGH); //StopNotColourChanger
digitalWrite(greyRELAY_PIN, LOW);
lcd.setCursor(9, 3); //4th line, 2nd block
lcd.print("Stopped"); //text
refreshLCD = true;
}
if (menu3_Value == 0 && menu3_StoredValue > 0 && menu1_Value != 0)///RELAY TRIGGER
{
if (ColourRepeat == true)
{
Serial.println("REPEAT COLOUR CHANGER");
digitalWrite(redRELAY_PIN, LOW); //RepeatColourChanger
digitalWrite(greyRELAY_PIN, HIGH);
}
if (ColourRepeat == false)
{
Serial.println("STOP COLOUR CHANGER");
digitalWrite(redRELAY_PIN, LOW); //StopColourChanger
digitalWrite(greyRELAY_PIN, LOW);
}
refreshLCD = true;
}
if (menu1_Value != 0 && menu2_Value == 0 && menu3_Value == 0) // All 0
{
}
if (menu1_Value == 0 && menu2_Value != 0 && menu3_Value != 0) // All 0
{
}
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
if (menu1_Value == 0 && menu2_Value == 0 && menu3_Value == 0) // All 0
{
}
if (menu1_Value != 0 && menu2_Value != 0 && menu3_Value != 0) // All 0
{
}
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
if (menu1_Value != 0 && menu2_Value != 0 && menu3_Value == 0) // All 0
{
}
if (menu1_Value == 0 && menu2_Value == 0 && menu3_Value != 0) // All 0
{
}
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
if (menu1_Value == 0 && menu2_Value != 0 && menu3_Value == 0) // All 0
{
}
if (menu1_Value != 0 && menu2_Value == 0 && menu3_Value != 0) // All 0
{
}
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
if (menu1_Value != 0 && menu2_Value != 0 && menu3_Value == 0) // All 0
{
}
if (menu1_Value == 0 && menu2_Value == 0 && menu3_Value != 0) // All 0
{
}
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
}
if (refreshLCD == true) //If we are allowed to update the LCD ...
{
updateLCD(); // ... we update the LCD ...
//... also, if one of the menus are already selected...
if (menu1_selected == true || menu2_selected == true || menu3_selected == true || menu4_selected == true)
{
// do nothing
}
else
{
updateCursorPosition(); //update the position
}
refreshLCD = false; //reset the variable - wait for a new trigger
}
if (refreshSelection == true) //if the selection is changed
{
updateSelection(); //update the selection on the LCD
refreshSelection = false; // reset the variable - wait for a new trigger
}
}
void rotate() ///INTERRUPT FUNCTION
{
//-----MENU1--------------------------------------------------------------
if (menu1_selected == true)
{
if (res == 1) //If RotEncDir Moved CW
{
if (menu1_Value < 9999) //we do not go above 9999
{
menu1_Value++;
}
else
{
menu1_Value = 0;
}
}
if (res == -1) //If RotEncDir Moved CCW
{
if (menu1_Value < 1) //we do not go below 0
{
menu1_Value = 9999;
}
else
{
// Encoder is rotating B direction so decrease
menu1_Value--;
}
}
refreshLCD = true;
}
//---MENU2---------------------------------------------------------------
else if (menu2_selected == true)
{
if (res == 1) //If RotEncDir Moved CW
{
if (menu2_Value < 9999) //we do not go above 9999
{
menu2_Value++;
}
else
{
menu2_Value = 0;
}
}
if (res == -1) //If RotEncDir Moved CCW
{
if (menu2_Value < 1) //we do not go below 0
{
menu2_Value = 9999;
}
else
{
// Encoder is rotating in B direction, so decrease
menu2_Value--;
}
}
refreshLCD = true;
}
//---MENU3--------------------------------------------------------------- X stop at ColourChanger
else if (menu3_selected == true)
{
if (res == 1) //If RotEncDir Moved CCW
{
if (menu3_Value < 9999) //we do not go above 9999
{
menu3_Value++;
}
else
{
menu3_Value = 0;
}
}
if (res == -1) //If RotEncDir Moved CCW
{
if (menu3_Value < 1) //we do not go below 0
{
menu3_Value = 9999;
}
else
{
// Encoder is rotating B direction so decrease
menu3_Value--;
}
}
refreshLCD = true;
}
//---MENU4----------------------------------------------------------------START/PAUSE = HALL EFFECT TRIGGER + DECREMENT
else if (menu4_selected == true)
{
refreshLCD = true;
}
else //MENU COUNTER----------------------------------------------------------------------------
{
if (res!=0) //If RotEnc Moved
{
if (res == 1) //If RotEnc Moved
{
if (menuCounter < 3) //we do not go above 3
{
menuCounter++;
}
else
{
menuCounter = 3;
}
}
if (res == -1) //If RotEnc Moved
{
if (menuCounter < 1) //we do not go below 0
{
menuCounter = 0;
}
else
{
// Encoder is rotating CW so decrease
menuCounter--;
}
}
}
refreshLCD = true;
}
}
void pushButton() ///INTERRUPT FUNCTION
{
if (menu3_selected == true) ///COLOUR REPEATING/STOP Function
{
ColourRepeater();
}
switch (menuCounter)
{
case 0:
menu1_selected = !menu1_selected; //we change the status of the variable to the opposite
break;
case 1:
menu2_selected = !menu2_selected;
break;
case 2:
menu3_selected = !menu3_selected;
break;
case 3:
menu4_selected = !menu4_selected;
break;
}
refreshLCD = true; //Refresh LCD after changing the value of the menu
refreshSelection = true; //refresh the selection ("X")
}
void printLCD() ///MAINMENU NAMING
{
//These are the values which are not changing the operation
lcd.setCursor(1, 0); //1st line, 2nd block
lcd.print("Rows"); //text
//----------------------
lcd.setCursor(1, 1); //2nd line, 2nd block
lcd.print("Stop"); //text
//----------------------
lcd.setCursor(1, 2); //3rd line, 2nd block
lcd.print("Colour"); //text
//----------------------
lcd.setCursor(1, 3); //4th line, 2nd block
lcd.print("Start"); //text
//----------------------
lcd.setCursor(13, 0); //1st line, 14th block
lcd.print("cnt: "); //counts - text
//----------------------
lcd.setCursor(14, 2); //4th line, 2nd block C.C. REPEAT OPTION
lcd.print("RPT:"); //blank previous
}
void updateLCD()
{
lcd.setCursor(17, 0); //1st line, 18th block
lcd.print(menuCounter); //counter (0 to 3)
lcd.setCursor(9, 0); //1st line, 10th block
if (menu1_Value < 1000) { //hundred
lcd.print ("0");
}
if (menu1_Value < 100) { //ten
lcd.print ("0");
}
if (menu1_Value < 10) { //one
lcd.print ("0");
}
lcd.print(menu1_Value); //print the value of menu1_Value variable
lcd.setCursor(9, 1);
if (menu2_Value < 1000) { //hundred
lcd.print ("0");
}
if (menu2_Value < 100) { //ten
lcd.print ("0");
}
if (menu2_Value < 10) { //one
lcd.print ("0");
}
lcd.print(menu2_Value); //
lcd.setCursor(9, 2);
if (menu3_Value < 1000) { //hundred
lcd.print ("0");
}
if (menu3_Value < 100) { //ten
lcd.print ("0");
}
if (menu3_Value < 10) { //one
lcd.print ("0");
}
lcd.print(menu3_Value); //
if (ColourRepeat == false)
{
lcd.setCursor(18, 2); //4th line, 2nd block C.C. REPEAT OPTION
lcd.print(" "); //blank previous
lcd.setCursor(18, 2); //4th line, 2nd block C.C. REPEAT OPTION
lcd.print("N"); //blank previous
}
if (ColourRepeat == true)
{
lcd.setCursor(18, 2); //4th line, 2nd block C.C. REPEAT OPTION
lcd.print(" "); //blank previous
lcd.setCursor(18, 2); //4th line, 2nd block C.C. REPEAT OPTION
lcd.print("Y"); //blank previous
}
//lcd.setCursor(9, 3);
// lcd.print(menu4_Value); //
}
void updateCursorPosition()
{
//Clear display's ">" parts
lcd.setCursor(0, 0); //1st line, 1st block
lcd.print(" "); //erase by printing a space
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print(" ");
lcd.setCursor(0, 3);
lcd.print(" ");
//Place cursor to the new position
switch (menuCounter) //this checks the value of the counter (0, 1, 2 or 3)
{
case 0:
lcd.setCursor(0, 0); //1st line, 1st block
lcd.print(">");
break;
//-------------------------------
case 1:
lcd.setCursor(0, 1); //2nd line, 1st block
lcd.print(">");
break;
//-------------------------------
case 2:
lcd.setCursor(0, 2); //3rd line, 1st block
lcd.print(">");
break;
//-------------------------------
case 3:
lcd.setCursor(0, 3); //4th line, 1st block
lcd.print(">");
break;
}
}
void updateSelection()
{
//When a menu is selected ">" becomes "X"
if (menu1_selected == true)
{
lcd.setCursor(0, 0); //1st line, 1st block
lcd.print("X");
}
//-------------------
if (menu2_selected == true)
{
lcd.setCursor(0, 1); //2nd line, 1st block
lcd.print("X");
}
//-------------------
if (menu3_selected == true)
{
lcd.setCursor(0, 2); //3rd line, 1st block
lcd.print("X");
}
//-------------------
if (menu4_selected == true)
{
lcd.setCursor(0, 3); //4th line, 1st block
lcd.print("X");
}
}
void RotationCheck (){ //Checks if Rot encoder Has moved
///New ROTMETHOD
res = rotary(); // res = -1 CCW / 0 NONE / +1 CW
if (res!=0)
{
rotate();
num = num + res;
//Serial.println(num);
if (res == 1)
{
}
if (res == -1)
{
}
}
if (digitalRead(PUSH) == 0)
{
buttonPushed = true;
delay(250);
}
///New ROTMETHOD
}
void ColourRepeater() ///COLOUR REPEATING/STOP Function
{
if (ColourRepeat == false)
{
Serial.println("ColourRepeat = Y");
//delay(500);
//buttonPushed == false;
ColourRepeat = true;
updateLCD();
}
else if (ColourRepeat == true)
{
Serial.println("ColourRepeat = N");
//delay(500);
//buttonPushed == false;
ColourRepeat = false;
updateLCD();
}
}