// Include Libraries
#include "Arduino.h"
#include "LiquidCrystal_PCF8574.h"
#include "Encoder.h"
#include "Button.h"
// Pin Definitions
#define ROTARYENCI_PIN_CLK 2
#define ROTARYENCI_PIN_D 3
#define ROTARYENCI_PIN_S1 4
#define LCD_ADDRESS 0x27
//#define LCD_ADDRESS 0x27 0x3F
#define LCD_ROWS 4
#define LCD_COLUMNS 20
#define SCROLL_DELAY 150
#define BACKLIGHT 255
long rotaryEncIOldPosition = 0;
// object initialization
LiquidCrystal_PCF8574 lcd;
Encoder rotaryEncI(ROTARYENCI_PIN_D,ROTARYENCI_PIN_CLK);
Button rotaryEncIButton(ROTARYENCI_PIN_S1);
// define vars for testing menu
const int timeout = 10000; //define timeout of 10 sec
char menuOption = 0;
long time0;
// Setup the essentials for your circuit to work. It runs first every time your circuit is powered with electricity.
void setup()
{
Serial.begin(9600);
while (!Serial) ; // wait for serial port to connect. Needed for native USB
Serial.println("start");
// initialize the lcd
lcd.begin(LCD_COLUMNS, LCD_ROWS, LCD_ADDRESS, BACKLIGHT);
rotaryEncIButton.init();
pinMode(ROTARYENCI_PIN_S1, INPUT_PULLUP);
menuOption = menu();
}
// Main logic of your circuit. It defines the interaction between the components you selected. After setup, it runs over and over again, in an eternal loop.
void loop()
{
if(menuOption == '1') {
// LCD Display 20x4 I2C - Test Code
// The LCD Screen will display the text of your choice.
lcd.clear(); // Clear LCD screen.
lcd.selectLine(2); // Set cursor at the begining of line 2
lcd.print(" Testing LCD IC2 "); // Print print String to LCD on first line
lcd.selectLine(3); // Set cursor at the begining of line 3
lcd.print(" V1 "); // Print print String to LCD on second line
delay(10000);
lcd.clear();
lcd.selectLine(1);
lcd.print("Menu system 2");
lcd.selectLine(2);
lcd.print("Hello there");
delay(10000);
lcd.clear();
}
else if(menuOption == '2') {
// Rotary Encoder Module - Test Code
//Read encoder new position
long rotaryEncINewPosition = rotaryEncI.read();
bool rotaryEncIButtonVal = rotaryEncIButton.onPress();
if (rotaryEncINewPosition != rotaryEncIOldPosition || rotaryEncIButtonVal) {
rotaryEncIOldPosition = rotaryEncINewPosition;
Serial.print(F("Pos: "));
Serial.print(rotaryEncINewPosition);
Serial.print(F("\tButton: "));
Serial.println(rotaryEncIButtonVal);
}
}
else if(menuOption == '3') {
int Verticle_Item;
int Horizontal_Item;
int Button_Press = 0;
int Counter;
long rotaryEncINewPosition = rotaryEncI.read();
bool rotaryEncIButtonVal = rotaryEncIButton.onPress();
long ButtonNew;
if (rotaryEncINewPosition != rotaryEncIOldPosition || rotaryEncIButtonVal) {
rotaryEncIOldPosition = rotaryEncINewPosition;
Serial.print(F("Pos: "));
Serial.print(rotaryEncINewPosition);
Serial.print(F("\tButton: ----> "));
Serial.println(rotaryEncIButtonVal);
if (ButtonNew != rotaryEncIButtonVal) {
rotaryEncIButtonVal = ButtonNew;
Serial.println("Different");
Counter = Counter + 1;
}
Counter = Counter + rotaryEncIButtonVal;
Serial.print(F("Counter ----> "));
Serial.println(Counter);
if (rotaryEncINewPosition >= 0 and rotaryEncINewPosition <= 4){ //If its at possition one and the button is pressed.
if (Counter <= 257){
lcd.clear();
lcd.selectLine(1);
lcd.print("> P1_Item_1_TEST2");
}
else if (Counter = 258){
lcd.clear();
lcd.selectLine(1);
lcd.print("> P2_Item_1_TEST2");
}
else if(Counter = 259){
lcd.clear();
lcd.selectLine(1);
lcd.print("> P3_Item_1_TEST3");
}
}
if (rotaryEncINewPosition > 4 and rotaryEncINewPosition <= 8){ //if at possition 2
if (Counter = 256){
lcd.clear();
lcd.selectLine(2);
lcd.print("> P1_Item_2");
}
else if (Counter = 258){
lcd.clear();
lcd.selectLine(2);
lcd.print("> P2_Item_2");
}
else if(Counter = 259){
lcd.clear();
lcd.selectLine(2);
lcd.print("> P3_Item_2");
}
}
}
if (millis() - time0 > timeout){
menuOption = menu();
}
}
}
// Menu function for selecting the components to be tested
// Follow serial monitor for instrcutions
char menu()
{
Serial.println(F("\nWhich component would you like to test?"));
Serial.println(F("(1) LCD Display 20x4 I2C"));
Serial.println(F("(2) Rotary Encoder Module"));
Serial.println(F("(3) Menu Test"));
while (!Serial.available());
// Read data from serial monitor if received
while (Serial.available())
{
char c = Serial.read();
if (isAlphaNumeric(c))
{
if(c == '1')
Serial.println(F("Now Testing LCD Display 20x4 I2C"));
else if(c == '2')
Serial.println(F("Now Testing Rotary Encoder Module"));
else if(c == '3')
Serial.println(F("Now Testing Menu System"));
else
{
Serial.println(F("illegal input!"));
return 0;
}
time0 = millis();
return c;//c menu counter, copy that code
}
}
}