/// Include the LiquidCrystal library
#include <LiquidCrystal.h>
// Set up pins for the LCD display (Pin LCD = Pin arduino)
const int RS = 8;
const int EN = 9;
const int d4 = 4;
const int d5 = 5;
const int d6 = 6;
const int d7 = 7;
// const int pin_BL = 10; // arduino pin wired to LCD backlight circuit
// Initialize the library with the numbers of the interface pins
LiquidCrystal lcd(RS, EN, d4, d5, d6, d7);
// define custom characters for arrows
byte upArrow[8] = {
B00100,
B01110,
B11111,
B00100,
B00100,
B00100,
B00100,
B00100};
byte downArrow[8] = {
B00100,
B00100,
B00100,
B00100,
B00100,
B11111,
B01110,
B00100};
// define some values used by the panel and buttons
int lcd_key = 0;
int adc_key_in = 0;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
// Test Short and long press
unsigned long currentMillis = 0;
const unsigned long SlowDelay = 150; // Délai entre 2 appuis
const unsigned long FastDelay = 1000; // Délai entre 2 incrémentation lors d'un appui
const unsigned long FastStep = 1000; // pas d'incrémentation lors d'un appui long
unsigned long LastAction = 0;
// Define constant for the relay pin
const int RELAY = 3;
// Define variables for the menu state
int menu = 0; // 0: main menu, 1: start test, 2: stop test, 3: configure test
int maxActuations = 10; // default value for the maximum number of actuations
int currentActuations = 0; // current number of actuations
// Define variable for the relay state
bool relayOn = false;
// Msg Coniguration
char *messagePadded = " LEFT:Config SELECT:Start/Pause ";
void setup()
{
lcd.createChar(0, upArrow); // Create UpArrow
lcd.createChar(1, downArrow); // Create downArrow
// Set up the LCD's number of columns and rows:
lcd.begin(16, 2);
lcd.setCursor(0, 0);
// Set the relay pin as output
pinMode(RELAY, OUTPUT);
// Turn off the relay
digitalWrite(RELAY, LOW);
// Display the main menu
lcd.print("Main Menu");
delay(1000);
for (int letter = 0; letter <= strlen(messagePadded) - 16; letter++) // From 0 to upto n-16 characters supply to below function
{
showLetters(0, letter);
}
}
void showLetters(int printStart, int startLetter)
{
lcd.setCursor(printStart, 1);
for (int letter = startLetter; letter <= startLetter + 15; letter++) // Print only 16 chars in Line #2 starting 'startLetter'
{
lcd.print(messagePadded[letter]);
}
lcd.print(" ");
delay(250);
}
// function for reading the LCD puttons -- taken from LCD shield test code written by Mark Bramwell, July 2010
int read_LCD_buttons()
{
adc_key_in = analogRead(0); // read the value from the sensor
// my buttons when read are centered at these valies: 0, 144, 329, 504, 741
// we add approx 50 to those values and check to see if we are close
if (adc_key_in > 1000)
return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
// For V1.1 us this threshold
if (adc_key_in < 50)
return btnRIGHT;
if (adc_key_in < 250)
return btnUP;
if (adc_key_in < 450)
return btnDOWN;
if (adc_key_in < 650)
return btnLEFT;
if (adc_key_in < 850)
return btnSELECT;
return btnNONE; // when all others fail, return this...
}
void loop()
{
// Read user inputs from the keyboard
lcd_key = read_LCD_buttons(); // read the buttons
unsigned long currentMillis = millis(); // Test Short and long press
// Check which button is pressed and change the menu state accordingly
switch (lcd_key) // Select button
{
case btnSELECT:
{
if (menu == 0)
{ // Main menu -> Start test
menu = 1;
lcd.begin(16, 2);
lcd.print("Starting test...");
delay(1000); // Wait for a second
lcd.clear();
lcd.print("Test in progress");
}
else if (menu == 1)
{ // Start test -> Stop test
menu = 2;
lcd.clear();
lcd.print("Stopping test...");
delay(1000); // Wait for a second
lcd.clear();
lcd.print("Test stopped");
}
else if (menu == 2)
{ // Stop test -> Main menu
menu = 0;
lcd.clear();
lcd.print("Main Menu");
for (int letter = 0; letter <= strlen(messagePadded) - 16; letter++) // From 0 to upto n-16 characters supply to below function
{
showLetters(0, letter);
}
}
else if (menu == 3)
{ // Configure test -> Main menu
menu = 0;
lcd.clear();
lcd.print("Main Menu");
for (int letter = 0; letter <= strlen(messagePadded) - 16; letter++) // From 0 to upto n-16 characters supply to below function
{
showLetters(0, letter);
}
}
break;
}
case btnLEFT:
{ // Left button
if (menu == 0)
{ // Main menu -> Configure test
menu = 3;
lcd.clear();
lcd.print("Configure test");
// Print use of arrow keys
lcd.setCursor(0, 1);
lcd.write(byte(0)); // print up arrow
lcd.print(":+1000 ");
// lcd.setCursor(1,1); // set cursor to second column of first row
lcd.write(byte(1)); // print down arrow
lcd.print(":-1000");
delay(500);
lcd.clear();
lcd.write(byte(0)); // print up arrow
lcd.print(":+1000 ");
// lcd.setCursor(1,1); // set cursor to second column of first row
lcd.write(byte(1)); // print down arrow
lcd.print(":-1000");
}
break;
}
case btnUP:
{ // Up button
if (menu == 3)
{ // Configure test -> Increase max actuations by one
if ((currentMillis - LastAction) > FastDelay)
{
maxActuations = maxActuations + FastStep;
lcd.setCursor(0, 1); // Move to the second row
lcd.print("Max: ");
lcd.print(maxActuations);
// delay(150);
LastAction = currentMillis;
}
if ((currentMillis - LastAction) > SlowDelay)
{
maxActuations++;
lcd.setCursor(0, 1); // Move to the second row
lcd.print("Max: ");
lcd.print(maxActuations);
// delay(150);
LastAction = currentMillis;
}
}
break;
}
case btnDOWN:
{ // Down button
if (menu == 3)
{ // Configure test -> Decrease max actuations by one
if ((currentMillis - LastAction) > FastDelay)
{
maxActuations = maxActuations - FastStep;
lcd.setCursor(0, 1); // Move to the second row
lcd.print("Max: ");
lcd.print(maxActuations);
// delay(150);
LastAction = currentMillis;
}
if ((currentMillis - LastAction) > SlowDelay)
{
maxActuations--;
lcd.setCursor(0, 1); // Move to the second row
lcd.print("Max: ");
lcd.print(maxActuations);
// delay(150);
LastAction = currentMillis;
}
if (maxActuations < 1)
maxActuations = 1; // Prevent negative values
lcd.setCursor(0, 1); // Move to the second row
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print("Max: ");
lcd.print(maxActuations);
delay(150);
}
break;
}
case btnRIGHT:
{ // Right button
if (menu == 3)
{ // Configure test -> Reset max actuations to default value
maxActuations = 10;
lcd.setCursor(0, 1); // Move to the second row
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print("Max: ");
lcd.print(maxActuations);
}
break;
}
case btnNONE:
{
// if no button was pressed, do nothing
break;
}
}
// Check if the test is in progress
if (menu == 1)
{
// Check if the current actuations are less than the max actuations
if (currentActuations < maxActuations)
{
// Toggle the relay state
relayOn = !relayOn;
// Set the relay pin accordingly
digitalWrite(RELAY, relayOn ? HIGH : LOW);
// Increment the current actuations if the relay is on
if (relayOn)
currentActuations++;
// Display the current and max actuations on the LCD
lcd.setCursor(0, 1); // Move to the second row
lcd.print("Cur: ");
lcd.print(currentActuations);
lcd.print(" Max: ");
lcd.print(maxActuations);
// Wait for half a second
delay(500);
}
else
{ // Test is done
// Turn off the relay
digitalWrite(RELAY, LOW);
// Display the test result on the LCD
lcd.clear();
lcd.print("Test completed");
lcd.setCursor(0, 1); // Move to the second row
lcd.print("Press SELECT");
// Reset the current actuations
currentActuations = 0;
menu = 2;
}
}
}