To achieve continuous left-to-right scrolling of your "CONNECTING TO {DEVICE}" message on a 16x2 LCD every two seconds, you can modify your showConnectingScreen function as follows. This implementation combines both lines into a single scrolling message and continuously scrolls it across the display.
Here's a complete example using the LiquidCrystal library commonly used with Arduino:
#include <LiquidCrystal.h>
// Initialize the library with the numbers of the interface pins
// (RS, E, D4, D5, D6, D7)
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// Example selected device
String selectedDevice = "DEVICE_NAME_ABC123";
void setup() {
// Set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Call the function to show the connecting screen
showConnectingScreen();
}
void loop() {
// Your main code would run here
// The showConnectingScreen function runs indefinitely in this example
}
// Function to show the connecting screen with scrolling text
void showConnectingScreen() {
// Combine both messages into one string with some spacing for smooth scrolling
String message = "CONNECTING TO " + selectedDevice + " "; // Added spaces for padding
// Ensure the message isn't excessively long
if (message.length() < 32) {
// Pad the message if it's shorter than two full LCD lines
message += String(" ").substring(0, 32 - message.length());
}
while (true) { // Continuous loop for scrolling
// Scroll the message from left to right
for (int position = 0; position <= message.length() - 16; position++) {
// Display a substring of the message on the first line
lcd.setCursor(0, 0);
lcd.print(message.substring(position, position + 16));
// Optionally, you can display additional information on the second line
// For example, a static status message:
lcd.setCursor(0, 1);
lcd.print("Please wait... ");
delay(2000); // Wait for 2 seconds before scrolling
}
// To make it continuous, you can loop the message
// Restart the position if at the end
}
}
Explanation of the Code:
-
Library and LCD Initialization:
- The
LiquidCrystal library is included and initialized with the appropriate pin numbers. Make sure these match your hardware setup.
-
Selected Device:
- The
selectedDevice variable holds the name of the device you're connecting to. Ensure this variable is set appropriately elsewhere in your code.
-
Combining Messages:
- The
message variable concatenates "CONNECTING TO " with the selectedDevice. Spaces are added at the end to provide padding, ensuring smooth scrolling.
-
Message Length Handling:
- If the combined message is shorter than twice the LCD width (32 characters for a 16x2 LCD), it is padded with additional spaces to fill the display and allow continuous scrolling without abrupt stops.
-
Scrolling Loop:
- A
while(true) loop ensures that the scrolling continues indefinitely.
- Inside the loop, a
for loop iterates through each character position of the message, displaying a 16-character substring on the first line of the LCD.
- The
setCursor and print functions handle the display of the substring.
- You can optionally display a static message or additional information on the second line.
- The
delay(2000) function pauses the loop for 2 seconds between each scroll step. You can adjust this delay to make the scrolling faster or slower.
-
Notes:
- Non-Blocking Approach: The above implementation uses a blocking
while(true) loop, which means no other code in the loop() function will execute while the scrolling is active. If your application requires other tasks to run simultaneously, consider implementing a non-blocking approach using millis() to manage timing without halting the program.
- Looping the Message: After scrolling through the entire message, the loop restarts, allowing the message to scroll continuously. Ensure your message length and padding are set to prevent any visual glitches.
Non-Blocking Scrolling (Advanced)
If you need other parts of your code to run while the message is scrolling, you can implement a non-blocking approach using the millis() function. Here's how you can modify the function:
unsigned long previousMillis = 0;
const long interval = 2000; // 2 seconds
int position = 0;
void showConnectingScreen() {
// Combine both messages into one string with some spacing for smooth scrolling
String message = "CONNECTING TO " + selectedDevice + " ";
// Ensure the message isn't excessively long
if (message.length() < 32) {
// Pad the message if it's shorter than two full LCD lines
message += String(" ").substring(0, 32 - message.length());
}
// Store the message globally or in a scope accessible to your loop
// For example:
globalMessage = message;
}
String globalMessage = "";
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// Scroll the message
if (globalMessage.length() > 16) {
lcd.setCursor(0, 0);
lcd.print(globalMessage.substring(position, position + 16));
// Optionally, display additional information on the second line
lcd.setCursor(0, 1);
lcd.print("Please wait... ");
position++;
// Reset position if end of message is reached
if (position > globalMessage.length() - 16) {
position = 0;
}
}
}
// Other non-blocking code can run here
}
Conclusion
By implementing the above code, your LCD will display a continuously scrolling "CONNECTING TO {DEVICE}" message, updating every two seconds. You can customize the messages, scrolling speed, and additional functionalities as needed for your specific application.