// Enumeration for screen states
enum ScreenState { LIST_SCREEN, CONNECTING_SCREEN };
ScreenState currentScreen = LIST_SCREEN;
// Current index for list display
int currentIndex = 0;
// Variables to store readings and joystick state
int yValue = 0;
int xValue = 0;
String mainDirection = "Center";
bool isCentered = true;
// Timing variables for holding in a single direction
unsigned long holdStartTime = 0;
const unsigned long holdIncrementInterval = 500; // 500 milliseconds
// Variables for button handling
bool lastButtonState = HIGH; // Assume button is not pressed initially
bool buttonPressed = false;
unsigned long lastDebounceTime = 0;
const unsigned long debounceDelay = 50; // 50 milliseconds debounce
// Variable to store selected device
String selectedDevice = "";
// Function prototypes
void displayMessage();
void displaySearchingMessage();
void displayDeviceFound();
void displayList(const String list[], int listSize, int currentIndex);
void showConnectingScreen();
void showListScreen();
void setup() {
// Initialize Serial for debugging (optional)
Serial.begin(115200); // Initialize Serial for debugging (if needed)
// Initialize the LCD
lcd.init();
lcd.backlight();
// Set button pin as input with pull-up resistor
pinMode(buttonPin, INPUT_PULLUP);
// Display initial messages
displayMessage(); // Display "ECOROOTS" and scrolling message
displaySearchingMessage(); // Display "SEARCHING ENVIRONMENT FOR DEVICES" and scrolling message
displayDeviceFound(); // Display "1 DEVICE FOUND"
// After initial messages, display the device list
showListScreen();
}
void loop() {
// Read the current button state
int reading = digitalRead(buttonPin);
// Check for button state change (debounce)
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// If the button state has changed
if (reading != buttonPressed) {
buttonPressed = reading;
code
Assistant
// If the button is pressed (assuming LOW when pressed)
if (buttonPressed == LOW) {
if (currentScreen == LIST_SCREEN) {
// Save the first line device
selectedDevice = devices[currentIndex];
// Switch to CONNECTING_SCREEN
currentScreen = CONNECTING_SCREEN;
showConnectingScreen();
}
// You can add functionality for button press on CONNECTING_SCREEN if needed
}
}
}
lastButtonState = reading; // Save the current reading for next loop
// Read analog values from Y and X axes
yValue = analogRead(vryPin);
xValue = analogRead(vrxPin);
// Determine current direction based on Y and X directions
String currentDirection = "Center";
if (yDirection != "Center") {
currentDirection = yDirection;
} else if (xDirection != "Center") {
currentDirection = xDirection;
}
// Handle actions based on the current screen
if (currentScreen == LIST_SCREEN) {
// Handle joystick navigation for list screen
if (currentDirection == "Center") {
isCentered = true;
mainDirection = "Center";
holdStartTime = 0; // Reset the hold time when joystick is centered
}
else if (isCentered) {
// Update main direction if joystick was previously centered
mainDirection = currentDirection;
isCentered = false;
code
// Handle the main direction
if (mainDirection == "Up") {
if (currentIndex > 0){
currentIndex--;
displayList(devices, listSize, currentIndex);
}
}
else if (mainDirection == "Down") {
if (currentIndex < listSize - 2){
currentIndex++;
displayList(devices, listSize, currentIndex);
}
}
// Left and Right directions can be handled if needed
// Start hold timer
holdStartTime = millis();
}
else if (mainDirection == currentDirection && holdStartTime > 0) {
// Check if 500 milliseconds have passed since the last increment
if (millis() - holdStartTime >= holdIncrementInterval) {
// Handle the main direction
if (mainDirection == "Up") {
if (currentIndex > 0){
currentIndex--;
displayList(devices, listSize, currentIndex);
}
}
else if (mainDirection == "Down") {
if (currentIndex < listSize - 2){
currentIndex++;
displayList(devices, listSize, currentIndex);
}
}
// Reset hold timer to count every 500 milliseconds
holdStartTime = millis();
}
}
}
else if (currentScreen == CONNECTING_SCREEN) {
// In CONNECTING_SCREEN, check if joystick is moved left to return to LIST_SCREEN
if (currentDirection == "Left") {
currentScreen = LIST_SCREEN;
showListScreen();
}
// You can add other interactions for CONNECTING_SCREEN if needed
}
// Add a small delay to stabilize readings
delay(75);
}
// Function to display the initial "ECOROOTS" message
void displayMessage() {
lcd.clear();
// Display "ECOROOTS" centered on the first row
lcd.setCursor((16 - 8) / 2, 0); // (16 - 8)/2 centers "ECOROOTS" (8 chars) on a 16-char display
lcd.print("ECOROOTS");
String message = "ROOTING SUSTAINABILITY INTO THE FUTURE";
int scrollLength = message.length() - 16;
if (scrollLength <= 0) {
// Message fits on screen, no scrolling needed
lcd.setCursor((16 - message.length()) / 2, 1);
lcd.print(message);
delay(5000);
} else {
int totalScrollSteps = scrollLength + 1; // To account for full scrolling
int delayTime = 5000 / totalScrollSteps; // Time per scroll step
// Function to display the "SEARCHING ENVIRONMENT FOR DEVICES" message
void displaySearchingMessage() {
lcd.clear();
String message = "SEARCHING ENVIRONMENT FOR DEVICES";
int scrollLength = message.length() - 16;
if (scrollLength <= 0) {
// Message fits on screen, no scrolling needed
lcd.setCursor((16 - message.length()) / 2, 0);
lcd.print(message);
delay(5000);
} else {
int totalScrollSteps = scrollLength + 1; // To account for full scrolling
int delayTime = 5000 / totalScrollSteps; // Time per scroll step
// Function to display "1 DEVICE FOUND"
void displayDeviceFound() {
lcd.clear();
// Display "1 DEVICE FOUND" centered on the first row
lcd.setCursor((16 - 13) / 2, 0); // 13 characters for "1 DEVICE FOUND"
lcd.print("1 DEVICE FOUND");
delay(1000); // Display it for 1 second
}
// Function to display the list of devices based on currentIndex (Left-Aligned)
void displayList(const String list[], int listSize, int currentIndex) {
lcd.clear();
// Display first item on the first line (left-aligned)
String firstItem = list[currentIndex];
if (firstItem.length() > 16) {
firstItem = firstItem.substring(0, 16); // Truncate if longer than 16 chars
}
lcd.setCursor(0, 0);
lcd.print(firstItem);
// Display second item on the second line (left-aligned)
if (currentIndex + 1 < listSize) {
String secondItem = list[currentIndex + 1];
if (secondItem.length() > 16) {
secondItem = secondItem.substring(0, 16); // Truncate if longer than 16 chars
}
lcd.setCursor(0, 1);
lcd.print(secondItem);
} else {
lcd.setCursor(0, 1);
lcd.print(" "); // Clear second line if no item
}
}
// Function to show the connecting screen
void showConnectingScreen() {
lcd.clear();
// Display "CONNECTING TO" on the first line
String connectingMsg = "CONNECTING TO";
lcd.setCursor(0, 0);
lcd.print(connectingMsg);
// Display "{DEVICE SELECTED}" on the second line, left-aligned
String deviceMsg = selectedDevice;
if (deviceMsg.length() > 16) {
deviceMsg = deviceMsg.substring(0, 16); // Truncate if longer than 16 chars
}
lcd.setCursor(0, 1);
lcd.print(deviceMsg);
// Optionally, you can add more functionality here, such as attempting to connect
}
// Function to show the list screen
void showListScreen() {
displayList(devices, listSize, currentIndex);
}
in tha bove text create a funciton that checks every line displayed on the LCD Screen if its length is greater than 16 charecters, and then scrolls it from left to right every two seconds as long as the screen isnt changed
To implement a scrolling feature for lines longer than 16 characters on your 16x2 LCD, you'll need to create a function that manages the scrolling state for each line. This function will check the length of each line, determine if scrolling is necessary, and then scroll the text from left to right every two seconds if required.
Here's how you can achieve this:
Define a Structure to Manage Scrolling State:
Create a ScrollState structure to keep track of the scrolling state for each line, including the full text, current scroll position, scroll direction, and the last time the line was scrolled.
cpp
struct ScrollState {
String fullText; // The complete text to display
int scrollPos; // Current scroll position
bool scrollLeft; // Direction of scroll: true = left, false = right
unsigned long lastScrollTime; // Timestamp of the last scroll action
};
// Initialize scroll states for both lines
ScrollState scrollLine1;
ScrollState scrollLine2;
Implement the Scrolling Function:
Create a scrollText function that takes two strings (one for each LCD line) and manages their scrolling based on their lengths.
cpp
void scrollText(const String &line1, const String &line2) {
// Handle Line 1
if (scrollLine1.fullText != line1) {
scrollLine1.fullText = line1;
scrollLine1.scrollPos = 0;
scrollLine1.scrollLeft = true; // Start by scrolling left
scrollLine1.lastScrollTime = millis();
}
if (scrollLine1.fullText.length() > 16) {
unsigned long currentMillis = millis();
if (currentMillis - scrollLine1.lastScrollTime >= 2000) { // 2 seconds interval
if (scrollLine1.scrollLeft) {
if (scrollLine1.scrollPos < scrollLine1.fullText.length() - 16) {
scrollLine1.scrollPos++;
} else {
scrollLine1.scrollLeft = false; // Change direction to right
}
} else {
if (scrollLine1.scrollPos > 0) {
scrollLine1.scrollPos--;
} else {
scrollLine1.scrollLeft = true; // Change direction to left
}
}
scrollLine1.lastScrollTime = currentMillis;
}
// Ensure scrollPos stays within valid range
scrollLine1.scrollPos = constrain(scrollLine1.scrollPos, 0, scrollLine1.fullText.length() - 16);
// Display the scrolling portion of the text
lcd.setCursor(0, 0);
lcd.print(scrollLine1.fullText.substring(scrollLine1.scrollPos, scrollLine1.scrollPos + 16));
} else {
// If text fits, display normally
lcd.setCursor(0, 0);
lcd.print(" "); // Clear the line
lcd.setCursor(0, 0);
lcd.print(scrollLine1.fullText);
}
// Handle Line 2
if (scrollLine2.fullText != line2) {
scrollLine2.fullText = line2;
scrollLine2.scrollPos = 0;
scrollLine2.scrollLeft = true; // Start by scrolling left
scrollLine2.lastScrollTime = millis();
}
if (scrollLine2.fullText.length() > 16) {
unsigned long currentMillis = millis();
if (currentMillis - scrollLine2.lastScrollTime >= 2000) { // 2 seconds interval
if (scrollLine2.scrollLeft) {
if (scrollLine2.scrollPos < scrollLine2.fullText.length() - 16) {
scrollLine2.scrollPos++;
} else {
scrollLine2.scrollLeft = false; // Change direction to right
}
} else {
if (scrollLine2.scrollPos > 0) {
scrollLine2.scrollPos--;
} else {
scrollLine2.scrollLeft = true; // Change direction to left
}
}
scrollLine2.lastScrollTime = currentMillis;
}
// Ensure scrollPos stays within valid range
scrollLine2.scrollPos = constrain(scrollLine2.scrollPos, 0, scrollLine2.fullText.length() - 16);
// Display the scrolling portion of the text
lcd.setCursor(0, 1);
lcd.print(scrollLine2.fullText.substring(scrollLine2.scrollPos, scrollLine2.scrollPos + 16));
} else {
// If text fits, display normally
lcd.setCursor(0, 1);
lcd.print(" "); // Clear the line
lcd.setCursor(0, 1);
lcd.print(scrollLine2.fullText);
}
}
Modify Display Functions to Use scrollText:
Instead of directly writing to the LCD in your display functions, call the scrollText function with the desired strings. This ensures that the scrolling logic is consistently applied across all display updates.
Example: Updating the displayList Function
cpp
// Function to display the list of devices using scrolling if needed
void displayList(const String list[], int listSize, int currentIndex) {
String firstItem = list[currentIndex];
String secondItem = (currentIndex + 1 < listSize) ? list[currentIndex + 1] : "";
// Truncate text if necessary (optional, since scrolling handles longer texts)
if (firstItem.length() > 32) { // Assuming maximum displayable length with scrolling
firstItem = firstItem.substring(0, 32);
}
if (secondItem.length() > 32) { // Similarly for the second line
secondItem = secondItem.substring(0, 32);
}
// Use scrollText to handle scrolling
scrollText(firstItem, secondItem);
}
Example: Updating the showConnectingScreen Function
cpp
// Function to show the connecting screen using scrolling if needed
void showConnectingScreen() {
String connectingMsg = "CONNECTING TO";
String deviceMsg = selectedDevice;
// Truncate text if necessary (optional)
if (connectingMsg.length() > 32) {
connectingMsg = connectingMsg.substring(0, 32);
}
if (deviceMsg.length() > 32) {
deviceMsg = deviceMsg.substring(0, 32);
}
// Use scrollText to handle scrolling
scrollText(connectingMsg, deviceMsg);
}
Integrate scrollText into the loop Function:
Ensure that the scrollText function is called regularly within your loop to manage scrolling irrespective of other actions. This allows continuous scrolling without blocking other functionalities.
cpp
Update Other Display Functions Similarly:
Ensure that all other display functions (e.g., displayMessage, displaySearchingMessage, displayDeviceFound) utilize the scrollText function instead of directly writing to the LCD. This will maintain consistency in how text is displayed and scrolled.
Example: Updating displayDeviceFound Function
cpp
// Function to display "1 DEVICE FOUND" using scrolling if needed
void displayDeviceFound() {
String deviceFoundMsg = "1 DEVICE FOUND";
// No need to truncate as it's shorter than 32 characters
scrollText(deviceFoundMsg, ""); // Second line is empty
delay(1000); // Display it for 1 second
}
Complete Updated Code:
Here's how the updated sections of your code will look with the scrolling functionality integrated:
cpp
Additional Considerations:
Non-Blocking Code: The scrollText function uses millis() to manage timing without blocking the main loop. This ensures that the rest of your code (e.g., handling joystick input) continues to run smoothly.
Direction Handling: The scrolling alternates between left and right directions every time the end or beginning of the text is reached, providing a back-and-forth scrolling effect.
Text Truncation: While the scrolling function handles texts longer than 16 characters, it's still a good practice to ensure that texts aren't excessively long to prevent unpredictable behavior.
By integrating this scrollText function and modifying your display functions accordingly, your LCD will now intelligently scroll any line that exceeds 16 characters, enhancing the readability and user experience of your device selection interface.
void loop() {
// ... [Existing code for handling buttons and joystick]
// Read analog values from Y and X axes
yValue = analogRead(vryPin);
xValue = analogRead(vrxPin);
// Determine Y-axis direction
String yDirection = "";
if (yValue > 800) { // Adjusted threshold
yDirection = "Up";
} else if (yValue < 200) { // Adjusted threshold
yDirection = "Down";
} else {
yDirection = "Center";
}
// Determine X-axis direction
String xDirection = "";
if (xValue > 800) { // Adjusted threshold
xDirection = "Right";
} else if (xValue < 200) { // Adjusted threshold
xDirection = "Left";
} else {
xDirection = "Center";
}
// Determine current direction based on Y and X directions
String currentDirection = "Center";
if (yDirection != "Center") {
currentDirection = yDirection;
} else if (xDirection != "Center") {
currentDirection = xDirection;
}
// Handle actions based on the current screen
if (currentScreen == LIST_SCREEN) {
// Handle joystick navigation for list screen
if (currentDirection == "Center") {
isCentered = true;
mainDirection = "Center";
holdStartTime = 0; // Reset the hold time when joystick is centered
}
else if (isCentered) {
// Update main direction if joystick was previously centered
mainDirection = currentDirection;
isCentered = false;
// Handle the main direction
if (mainDirection == "Up") {
if (currentIndex > 0){
currentIndex--;
}
}
else if (mainDirection == "Down") {
if (currentIndex < listSize - 2){
currentIndex++;
}
}
// Left and Right directions can be handled if needed
// Start hold timer and display the updated list
holdStartTime = millis();
displayList(devices, listSize, currentIndex);
}
else if (mainDirection == currentDirection && holdStartTime > 0) {
// Check if 500 milliseconds have passed since the last increment
if (millis() - holdStartTime >= holdIncrementInterval) {
// Handle the main direction
if (mainDirection == "Up") {
if (currentIndex > 0){
currentIndex--;
}
}
else if (mainDirection == "Down") {
if (currentIndex < listSize - 2){
currentIndex++;
}
}
// Reset hold timer and display the updated list
holdStartTime = millis();
displayList(devices, listSize, currentIndex);
}
}
}
else if (currentScreen == CONNECTING_SCREEN) {
// In CONNECTING_SCREEN, check if joystick is moved left to return to LIST_SCREEN
if (currentDirection == "Left") {
currentScreen = LIST_SCREEN;
showListScreen();
}
// You can add other interactions for CONNECTING_SCREEN if needed
}
// Add a small delay to stabilize readings
delay(75);
}
#include <Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Initialize the LCD: address 0x27, 16 characters, 2 lines
LiquidCrystal_I2C lcd(0x27, 16, 2); // Adjust the I2C address if needed
// Define pins for joystick module
const int vryPin = A0; // Y-axis
const int vrxPin = A1; // X-axis
const int buttonPin = 2; // Button
// Device list
const int listSize = 15;
const String devices[listSize] = {
"DEVICE 1",
"DEVICE 2",
"DEVICE 3",
"DEVICE 4",
"DEVICE 5",
"DEVICE 6",
"DEVICE 7",
"DEVICE 8",
"DEVICE 9",
"DEVICE 10",
"DEVICE 11",
"DEVICE 12",
"DEVICE 13",
"DEVICE 14",
"DEVICE 15"
};
// Enumeration for screen states
enum ScreenState { LIST_SCREEN, CONNECTING_SCREEN };
ScreenState currentScreen = LIST_SCREEN;
// Current index for list display
int currentIndex = 0;
// Variables to store readings and joystick state
int yValue = 0;
int xValue = 0;
String mainDirection = "Center";
bool isCentered = true;
// Timing variables for holding in a single direction
unsigned long holdStartTime = 0;
const unsigned long holdIncrementInterval = 500; // 500 milliseconds
// Variables for button handling
bool lastButtonState = HIGH; // Assume button is not pressed initially
bool buttonPressed = false;
unsigned long lastDebounceTime = 0;
const unsigned long debounceDelay = 50; // 50 milliseconds debounce
// Variable to store selected device
String selectedDevice = "";
// Structure to manage scrolling state
struct ScrollState {
String fullText; // The complete text to display
int scrollPos; // Current scroll position
bool scrollLeft; // Direction of scroll: true = left, false = right
unsigned long lastScrollTime; // Timestamp of the last scroll action
};
// Initialize scroll states for both lines
ScrollState scrollLine1;
ScrollState scrollLine2;
// Function prototypes
void displayMessage();
void displaySearchingMessage();
void displayDeviceFound();
void displayList(const String list[], int listSize, int currentIndex);
void showConnectingScreen();
void showListScreen();
void scrollText(const String &line1, const String &line2); // New scrolling function
void setup() {
// Initialize Serial for debugging (optional)
Serial.begin(115200); // Initialize Serial for debugging (if needed)
// Initialize the LCD
lcd.init();
lcd.backlight();
// Set button pin as input with pull-up resistor
pinMode(buttonPin, INPUT_PULLUP);
// Display initial messages
displayMessage(); // Display "ECOROOTS" and scrolling message
displaySearchingMessage(); // Display "SEARCHING ENVIRONMENT FOR DEVICES" and scrolling message
displayDeviceFound(); // Display "1 DEVICE FOUND"
// After initial messages, display the device list
showListScreen();
}
void loop() {
// Read the current button state
int reading = digitalRead(buttonPin);
// Check for button state change (debounce)
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// If the button state has changed
if (reading != buttonPressed) {
buttonPressed = reading;
// If the button is pressed (assuming LOW when pressed)
if (buttonPressed == LOW) {
if (currentScreen == LIST_SCREEN) {
// Save the first line device
selectedDevice = devices[currentIndex];
// Switch to CONNECTING_SCREEN
currentScreen = CONNECTING_SCREEN;
showConnectingScreen();
}
// You can add functionality for button press on CONNECTING_SCREEN if needed
}
}
}
lastButtonState = reading; // Save the current reading for next loop
// Read analog values from Y and X axes
yValue = analogRead(vryPin);
xValue = analogRead(vrxPin);
// Determine Y-axis direction
String yDirection = "";
if (yValue > 800) { // Adjusted threshold
yDirection = "Up";
} else if (yValue < 200) { // Adjusted threshold
yDirection = "Down";
} else {
yDirection = "Center";
}
// Determine X-axis direction
String xDirection = "";
if (xValue > 800) { // Adjusted threshold
xDirection = "Right";
} else if (xValue < 200) { // Adjusted threshold
xDirection = "Left";
} else {
xDirection = "Center";
}
// Determine current direction based on Y and X directions
String currentDirection = "Center";
if (yDirection != "Center") {
currentDirection = yDirection;
} else if (xDirection != "Center") {
currentDirection = xDirection;
}
// Handle actions based on the current screen
if (currentScreen == LIST_SCREEN) {
// Handle joystick navigation for list screen
if (currentDirection == "Center") {
isCentered = true;
mainDirection = "Center";
holdStartTime = 0; // Reset the hold time when joystick is centered
}
else if (isCentered) {
// Update main direction if joystick was previously centered
mainDirection = currentDirection;
isCentered = false;
// Handle the main direction
if (mainDirection == "Up") {
if (currentIndex > 0){
currentIndex--;
}
}
else if (mainDirection == "Down") {
if (currentIndex < listSize - 2){
currentIndex++;
}
}
// Left and Right directions can be handled if needed
// Start hold timer and display the updated list
holdStartTime = millis();
displayList(devices, listSize, currentIndex);
}
else if (mainDirection == currentDirection && holdStartTime > 0) {
// Check if 500 milliseconds have passed since the last increment
if (millis() - holdStartTime >= holdIncrementInterval) {
// Handle the main direction
if (mainDirection == "Up") {
if (currentIndex > 0){
currentIndex--;
}
}
else if (mainDirection == "Down") {
if (currentIndex < listSize - 2){
currentIndex++;
}
}
// Reset hold timer and display the updated list
holdStartTime = millis();
displayList(devices, listSize, currentIndex);
}
}
}
else if (currentScreen == CONNECTING_SCREEN) {
// In CONNECTING_SCREEN, check if joystick is moved left to return to LIST_SCREEN
if (currentDirection == "Left") {
currentScreen = LIST_SCREEN;
showListScreen();
}
// You can add other interactions for CONNECTING_SCREEN if needed
}
// Add a small delay to stabilize readings
delay(75);
}
// Function to display the initial "ECOROOTS" message
void displayMessage() {
lcd.clear();
// Display "ECOROOTS" centered on the first row
lcd.setCursor((16 - 8) / 2, 0); // (16 - 8)/2 centers "ECOROOTS" (8 chars) on a 16-char display
lcd.print("ECOROOTS");
String message = "ROOTING SUSTAINABILITY INTO THE FUTURE";
int scrollLength = message.length() - 16;
if (scrollLength <= 0) {
// Message fits on screen, no scrolling needed
lcd.setCursor((16 - message.length()) / 2, 1);
lcd.print(message);
delay(5000);
} else {
int totalScrollSteps = scrollLength + 1; // To account for full scrolling
int delayTime = 5000 / totalScrollSteps; // Time per scroll step
for (int pos = 0; pos <= scrollLength; pos++) {
lcd.setCursor(0, 1);
lcd.print(message.substring(pos, pos + 16));
delay(delayTime);
}
}
}
// Function to display the "SEARCHING ENVIRONMENT FOR DEVICES" message
void displaySearchingMessage() {
lcd.clear();
String message = "SEARCHING ENVIRONMENT FOR DEVICES";
int scrollLength = message.length() - 16;
if (scrollLength <= 0) {
// Message fits on screen, no scrolling needed
lcd.setCursor((16 - message.length()) / 2, 0);
lcd.print(message);
delay(5000);
} else {
int totalScrollSteps = scrollLength + 1; // To account for full scrolling
int delayTime = 5000 / totalScrollSteps; // Time per scroll step
for (int pos = 0; pos <= scrollLength; pos++) {
lcd.setCursor(0, 0);
lcd.print(message.substring(pos, pos + 16));
delay(delayTime);
}
}
}
// Function to display "1 DEVICE FOUND" using scrolling if needed
void displayDeviceFound() {
String deviceFoundMsg = "1 DEVICE FOUND";
scrollText(deviceFoundMsg, ""); // Second line is empty
delay(1000); // Display it for 1 second
}
// Function to display the list of devices using scrolling if needed
void displayList(const String list[], int listSize, int currentIndex) {
String firstItem = list[currentIndex];
String secondItem = (currentIndex + 1 < listSize) ? list[currentIndex + 1] : "";
scrollText(firstItem, secondItem);
}
// Function to show the connecting screen using scrolling if needed
void showConnectingScreen() {
String connectingMsg = "CONNECTING TO";
String deviceMsg = selectedDevice;
scrollText(connectingMsg, deviceMsg);
}
// Function to show the list screen
void showListScreen() {
displayList(devices, listSize, currentIndex);
}
// Function to update scrolling text on the LCD
void scrollText(const String &line1, const String &line2) {
// Handle Line 1
if (scrollLine1.fullText != line1) {
scrollLine1.fullText = line1;
scrollLine1.scrollPos = 0;
scrollLine1.scrollLeft = true; // Start by scrolling left
scrollLine1.lastScrollTime = millis();
}
if (scrollLine1.fullText.length() > 16) {
unsigned long currentMillis = millis();
if (currentMillis - scrollLine1.lastScrollTime >= 2000) { // 2 seconds interval
if (scrollLine1.scrollLeft) {
if (scrollLine1.scrollPos < scrollLine1.fullText.length() - 16) {
scrollLine1.scrollPos++;
} else {
scrollLine1.scrollLeft = false; // Change direction to right
}
} else {
if (scrollLine1.scrollPos > 0) {
scrollLine1.scrollPos--;
} else {
scrollLine1.scrollLeft = true; // Change direction to left
}
}
scrollLine1.lastScrollTime = currentMillis;
}
// Ensure scrollPos stays within valid range
scrollLine1.scrollPos = constrain(scrollLine1.scrollPos, 0, max(scrollLine1.fullText.length() - 16, 0));
// Display the scrolling portion of the text
lcd.setCursor(0, 0);
if (scrollLine1.scrollPos + 16 <= scrollLine1.fullText.length()) {
lcd.print(scrollLine1.fullText.substring(scrollLine1.scrollPos, scrollLine1.scrollPos + 16));
} else {
// If remaining characters are less than 16, pad with spaces
String displayStr = scrollLine1.fullText.substring(scrollLine1.scrollPos);
displayStr += " "; // Add spaces to fill the line
lcd.print(displayStr.substring(0, 16));
}
} else {
// If text fits, display normally
lcd.setCursor(0, 0);
lcd.print(" "); // Clear the line
lcd.setCursor(0, 0);
lcd.print(scrollLine1.fullText);
}
// Handle Line 2
if (scrollLine2.fullText != line2) {
scrollLine2.fullText = line2;
scrollLine2.scrollPos = 0;
scrollLine2.scrollLeft = true; // Start by scrolling left
scrollLine2.lastScrollTime = millis();
}
if (scrollLine2.fullText.length() > 16) {
unsigned long currentMillis = millis();
if (currentMillis - scrollLine2.lastScrollTime >= 2000) { // 2 seconds interval
if (scrollLine2.scrollLeft) {
if (scrollLine2.scrollPos < scrollLine2.fullText.length() - 16) {
scrollLine2.scrollPos++;
} else {
scrollLine2.scrollLeft = false; // Change direction to right
}
} else {
if (scrollLine2.scrollPos > 0) {
scrollLine2.scrollPos--;
} else {
scrollLine2.scrollLeft = true; // Change direction to left
}
}
scrollLine2.lastScrollTime = currentMillis;
}
// Ensure scrollPos stays within valid range
scrollLine2.scrollPos = constrain(scrollLine2.scrollPos, 0, max(scrollLine2.fullText.length() - 16, 0));
// Display the scrolling portion of the text
lcd.setCursor(0, 1);
if (scrollLine2.scrollPos + 16 <= scrollLine2.fullText.length()) {
lcd.print(scrollLine2.fullText.substring(scrollLine2.scrollPos, scrollLine2.scrollPos + 16));
} else {
// If remaining characters are less than 16, pad with spaces
String displayStr = scrollLine2.fullText.substring(scrollLine2.scrollPos);
displayStr += " "; // Add spaces to fill the line
lcd.print(displayStr.substring(0, 16));
}
} else {
// If text fits, display normally
lcd.setCursor(0, 1);
lcd.print(" "); // Clear the line
lcd.setCursor(0, 1);
lcd.print(scrollLine2.fullText);
}
}