/*
LCD I²C HELLO WORLD
MADE BY BRAYLEN HAMMOND
YOU MAY USE THE CODE AND DIAGRAM TO HELP YOU WITH YOUR LCD.
THIS CODE DOESNT APPLY TO THE NORMAL LCD, IT ONLY
APPLIES TO THE I2C LCD. YOU CAN DO THIS ON WOKWI OR
ON YOUR ACTUAL ARDUINO. THIS PROJECT IS ONLY
TESTED THE ARDUINO UNO, YOU CAN TRY ON OTHER
ARDUINOES TO SEE IF IT WORKS.
GO TO MY YOUTUBE CHANNEL FOR MORE HELP WITH CODING AN ARDUINO. Please Subscribe, it's free and it'll help out a ton.
https://www.youtube.com/channel/UCccMHKRai1dwgxwFg-w8hZA
Have fun, be creative and enjoy the help I do for you
*/
// Add the Liquid Crystal I2C library
#include <LiquidCrystal_I2C.h>
// Add the lcd
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define buttonUP 2
#define buttonDOWN 3
#define buttonSAVE 4
#define buttonENTER 5
unsigned long backlight_tm = 0;
struct Default_variable {
uint8_t device_IP[4] = {192, 168, 0, 186}; // RTU device static IPv4
uint8_t gateway[4] = {192, 168, 0, 1}; // RTU device gateway
uint8_t subnet[4] = {255, 255, 255, 0}; // RTU device subnet mask
uint8_t dns_server[4] = {192, 168, 0, 1}; // RTU device DNS server
uint16_t net_timeout = 500; // 500 ms network timeout for event update
uint16_t debounce_tm = 200; // Routine time for timer is debouncing time in milliseconds
} default_var, running_var;
enum CONFIGURATION {
ENABLE,
DISABLE
};
bool setup_mode = DISABLE;
enum MODE {
IP_MODE = 0,
GATEWAY_MODE = 1,
SUBNET_MODE = 2,
DNS_MODE = 3,
};
uint8_t mode_pos = IP_MODE;
enum IP_OCTET {
FIRST_OCTET = 0,
SECOND_OCTET = 4,
THIRD_OCTET = 8,
FOURTH_OCTET = 12
};
uint8_t cursor_pos = FIRST_OCTET;
uint8_t *first_octet = &running_var.device_IP[0];
uint8_t *second_octet = &running_var.device_IP[1];
uint8_t *third_octet = &running_var.device_IP[2];
uint8_t *fourth_octet = &running_var.device_IP[3];
bool show_display = false;
void setup() {
// Initalise the LCD
lcd.init();
// Turn on the LCD backlight
lcd.backlight();
// Put text on the LCD
lcd.print("Hello World!");
lcd.backlight();
pinMode(buttonUP, INPUT_PULLUP);
pinMode(buttonDOWN, INPUT_PULLUP);
pinMode(buttonENTER, INPUT_PULLUP);
pinMode(buttonSAVE, INPUT_PULLUP);
// Clear the LCD screen
lcd.clear();
//cursor blinking
lcd.noBlink();
lcd.setCursor(0, 1); // Set the cursor to the first column, Second row
lcd.print(*first_octet);
lcd.print(".");
lcd.print(*second_octet);
lcd.print(".");
lcd.print(*third_octet);
lcd.print(".");
lcd.print(*fourth_octet);
Serial.begin(115200);
}
void loop() {
if (setup_mode == ENABLE) {
lcd.setCursor(cursor_pos, 1); // Set the cursor to the first column, Second row
if (cursor_pos == FIRST_OCTET) {
if (*first_octet < 10) {
lcd.print(" ");
lcd.print(*first_octet); // Print the updated number
} else if (*first_octet < 100) {
lcd.print(" ");
lcd.print(*first_octet); // Print the updated number
} else {
lcd.print(*first_octet); // Print the updated number
}
lcd.print("."); // separator
}
if (cursor_pos == SECOND_OCTET) {
if (*second_octet < 10) {
lcd.print(" ");
lcd.print(*second_octet); // Print the updated number
} else if (*second_octet < 100) {
lcd.print(" ");
lcd.print(*second_octet); // Print the updated number
} else {
lcd.print(*second_octet); // Print the updated number
}
lcd.print("."); // separator
}
if (cursor_pos == THIRD_OCTET) {
if (*third_octet < 10) {
lcd.print(" ");
lcd.print(*third_octet); // Print the updated number
} else if (*third_octet < 100) {
lcd.print(" ");
lcd.print(*third_octet); // Print the updated number
} else {
lcd.print(*third_octet); // Print the updated number
}
lcd.print("."); // separator
}
if (cursor_pos == FOURTH_OCTET) {
if (*fourth_octet < 10) {
lcd.print(" ");
lcd.print(*fourth_octet); // Print the updated number
} else if (*fourth_octet < 100) {
lcd.print(" ");
lcd.print(*fourth_octet); // Print the updated number
} else {
lcd.print(*fourth_octet); // Print the updated number
}
}
}
if (digitalRead(buttonUP) == LOW) {
backlight_tm = millis();
lcd.backlight();
if (setup_mode == ENABLE) {
switch (cursor_pos) {
case FIRST_OCTET:
(*first_octet)++;
break;
case SECOND_OCTET:
(*second_octet)++;
break;
case THIRD_OCTET:
(*third_octet)++;
break;
case FOURTH_OCTET:
(*fourth_octet)++;
break;
default:
//do nothing
break;
}
} else {
show_display = true; //turn on display
//change mode selection
switch (mode_pos) {
case DNS_MODE :
mode_pos = SUBNET_MODE ;
first_octet = &running_var.subnet[0];
second_octet = &running_var.subnet[1];
third_octet = &running_var.subnet[2];
fourth_octet = &running_var.subnet[3];
break;
case SUBNET_MODE :
mode_pos = GATEWAY_MODE ;
first_octet = &running_var.gateway[0];
second_octet = &running_var.gateway[1];
third_octet = &running_var.gateway[2];
fourth_octet = &running_var.gateway[3];
break;
case GATEWAY_MODE :
mode_pos = IP_MODE ;
first_octet = &running_var.device_IP[0];
second_octet = &running_var.device_IP[1];
third_octet = &running_var.device_IP[2];
fourth_octet = &running_var.device_IP[3];
break;
case IP_MODE :
mode_pos = DNS_MODE;
first_octet = &running_var.dns_server[0];
second_octet = &running_var.dns_server[1];
third_octet = &running_var.dns_server[2];
fourth_octet = &running_var.dns_server[3];
break;
default:
//do nothing
break;
}
}
}
if (digitalRead(buttonDOWN) == LOW) {
backlight_tm = millis();
lcd.backlight();
if (setup_mode == ENABLE) {
switch (cursor_pos) {
case FIRST_OCTET:
(*first_octet)--;
break;
case SECOND_OCTET:
(*second_octet)--;
break;
case THIRD_OCTET:
(*third_octet)--;
break;
case FOURTH_OCTET:
(*fourth_octet)--;
break;
default:
//do nothing
break;
}
} else {
show_display = true; //turn on display
//change mode selection
switch (mode_pos) {
case IP_MODE :
mode_pos = GATEWAY_MODE;
first_octet = &running_var.gateway[0];
second_octet = &running_var.gateway[1];
third_octet = &running_var.gateway[2];
fourth_octet = &running_var.gateway[3];
break;
case GATEWAY_MODE :
mode_pos = SUBNET_MODE;
first_octet = &running_var.subnet[0];
second_octet = &running_var.subnet[1];
third_octet = &running_var.subnet[2];
fourth_octet = &running_var.subnet[3];
break;
case SUBNET_MODE:
mode_pos = DNS_MODE;
first_octet = &running_var.dns_server[0];
second_octet = &running_var.dns_server[1];
third_octet = &running_var.dns_server[2];
fourth_octet = &running_var.dns_server[3];
break;
case DNS_MODE :
mode_pos = IP_MODE;
first_octet = &running_var.device_IP[0];
second_octet = &running_var.device_IP[1];
third_octet = &running_var.device_IP[2];
fourth_octet = &running_var.device_IP[3];
break;
default:
//do nothing
break;
}
}
}
if (digitalRead(buttonENTER) == LOW) {
backlight_tm = millis();
lcd.backlight();
lcd.blink();
setup_mode = ENABLE;
switch (cursor_pos) {
case FIRST_OCTET:
cursor_pos = SECOND_OCTET;
break;
case SECOND_OCTET:
cursor_pos = THIRD_OCTET;
break;
case THIRD_OCTET:
cursor_pos = FOURTH_OCTET;
break;
case FOURTH_OCTET:
cursor_pos = FIRST_OCTET;
break;
default:
//do nothing
break;
}
delay(500);
}
if (digitalRead(buttonSAVE) == LOW) {
backlight_tm = millis();
lcd.backlight();
lcd.noBlink();
setup_mode = DISABLE;
// Clear the LCD screen
lcd.clear();
switch (mode_pos) {
case IP_MODE :
lcd.setCursor(0, 0); // Set the cursor to the first column, Second row
lcd.print("STATIC_IP:");
lcd.setCursor(0, 1); // Set the cursor to the first column, Second row
lcd.print(*first_octet);
lcd.print(".");
lcd.print(*second_octet);
lcd.print(".");
lcd.print(*third_octet);
lcd.print(".");
lcd.print(*fourth_octet);
break;
case GATEWAY_MODE :
lcd.setCursor(0, 0); // Set the cursor to the first column, Second row
lcd.print("GATEWAY:");
lcd.setCursor(0, 1); // Set the cursor to the first column, Second row
lcd.print(*first_octet);
lcd.print(".");
lcd.print(*second_octet);
lcd.print(".");
lcd.print(*third_octet);
lcd.print(".");
lcd.print(*fourth_octet);
break;
case SUBNET_MODE:
lcd.setCursor(0, 0); // Set the cursor to the first column, Second row
lcd.print("SUBNET:");
lcd.setCursor(0, 1); // Set the cursor to the first column, Second row
lcd.print(*first_octet);
lcd.print(".");
lcd.print(*second_octet);
lcd.print(".");
lcd.print(*third_octet);
lcd.print(".");
lcd.print(*fourth_octet);
break;
case DNS_MODE :
lcd.setCursor(0, 0); // Set the cursor to the first column, Second row
lcd.print("DNS:");
lcd.setCursor(0, 1); // Set the cursor to the first column, Second row
lcd.print(*first_octet);
lcd.print(".");
lcd.print(*second_octet);
lcd.print(".");
lcd.print(*third_octet);
lcd.print(".");
lcd.print(*fourth_octet);
break;
default:
//do nothing
break;
}
}
if (show_display) {
backlight_tm = millis();
lcd.backlight();
show_display = false; //display off
// Clear the LCD screen
lcd.clear();
switch (mode_pos) {
case IP_MODE :
lcd.setCursor(0, 0); // Set the cursor to the first column, Second row
lcd.print("STATIC_IP:");
lcd.setCursor(0, 1); // Set the cursor to the first column, Second row
lcd.print(*first_octet);
lcd.print(".");
lcd.print(*second_octet);
lcd.print(".");
lcd.print(*third_octet);
lcd.print(".");
lcd.print(*fourth_octet);
break;
case GATEWAY_MODE :
lcd.setCursor(0, 0); // Set the cursor to the first column, Second row
lcd.print("GATEWAY:");
lcd.setCursor(0, 1); // Set the cursor to the first column, Second row
lcd.print(*first_octet);
lcd.print(".");
lcd.print(*second_octet);
lcd.print(".");
lcd.print(*third_octet);
lcd.print(".");
lcd.print(*fourth_octet);
break;
case SUBNET_MODE:
lcd.setCursor(0, 0); // Set the cursor to the first column, Second row
lcd.print("SUBNET:");
lcd.setCursor(0, 1); // Set the cursor to the first column, Second row
lcd.print(*first_octet);
lcd.print(".");
lcd.print(*second_octet);
lcd.print(".");
lcd.print(*third_octet);
lcd.print(".");
lcd.print(*fourth_octet);
break;
case DNS_MODE :
lcd.setCursor(0, 0); // Set the cursor to the first column, Second row
lcd.print("DNS:");
lcd.setCursor(0, 1); // Set the cursor to the first column, Second row
lcd.print(*first_octet);
lcd.print(".");
lcd.print(*second_octet);
lcd.print(".");
lcd.print(*third_octet);
lcd.print(".");
lcd.print(*fourth_octet);
break;
default:
//do nothing
break;
}
}
if (millis() - backlight_tm > 1000) {
lcd.noBacklight();
}
Serial.println(mode_pos);
Serial.print(*first_octet);
Serial.print(".");
Serial.print(*second_octet);
Serial.print(".");
Serial.print(*third_octet);
Serial.print(".");
Serial.println(*fourth_octet);
}