#include <LiquidCrystal_I2C.h>
#include <Wire.h>
//Define lcd monitor
LiquidCrystal_I2C lcd(0x27,20,4);
// Variables
int Li = 20;
int Lii = 0;
String readSong = "";
String oldSong = "wesrctfvygbhunjimkl";
String oldTime = "wesrctfvygbhunjimkl";
String data;
String delimiter = "sgxdfgchjkl";
String spotify_closed_key = "kyawesgtlu";
int wherePrint;
int time_x = 15;
int time_y = 3;
bool scrolling = false;
bool connected = false;
//Setup lcd display and serial port
void setup() {
// initializing the LCD.
lcd.init();
// initializing the serial monitor.
Serial.begin(9600);
}
//Split two different data types
String* ProcessData(String sentData){
if (sentData.startsWith(delimiter)){
String time = sentData.substring(11, 16);
String song = sentData.substring(16, sentData.length());
//Can anyone explain me why use String* and not String?
String* songandtime = new String[2];
songandtime[0] = song;
songandtime[1] = time;
return songandtime;
}else{
String* songandtime = new String[2];
songandtime[0] = sentData;
songandtime[1] = "";
return songandtime;
}
}
//Scrolls the text if > 20 characteres
String Scroll_LCD_Left(String StrDisplay){
lcd.setCursor(0, 1);
String result;
String StrProcess = StrDisplay + " " + StrDisplay;
result = StrProcess.substring(Li,Lii);
Li++;
Lii++;
//Only God knows what's happening here
if (Li>(StrProcess.length() - (StrDisplay.length() - 20))){
Li=20;
Lii=0;
}
return result;
}
//Main void loop
void loop() {
//Get data from python server
readSong = Serial.readString();
if (readSong != ""){
//Enabling backlight
lcd.backlight();
String* processedData = ProcessData(readSong);
//Check If time changed
if (processedData[1] != ""){
if (processedData[1] != oldTime){
oldTime = processedData[1];
//lcd.clear();
lcd.setCursor(0, time_y);
lcd.print(" ");
lcd.setCursor(time_x, time_y);
lcd.print(processedData[1]);
}
}
//Test if song changed
if (processedData[0] != ""){
if (processedData[0] != oldSong){
oldSong = processedData[0];
if (processedData[0] == "ServerExitedByUser"){
lcd.noBacklight();
}
if (processedData[0] == spotify_closed_key){
time_x = 7;
time_y = 1;
lcd.clear();
lcd.setCursor(time_x, time_y);
lcd.print(oldTime);
}else{
time_x = 15;
time_y = 3;
//lcd.clear();
lcd.setCursor(0, 1);
lcd.print(" ");
//Print the results
if (processedData[0].length() < 20){
wherePrint = (20 - (processedData[0].length())) / 2;
lcd.setCursor(wherePrint, 1);
lcd.print(processedData[0]);
//Disable text scrolling
scrolling = false;
}else{
data = processedData[0];
Li = 20;
Lii = 0;
//Enable text scrolling
scrolling = true;
}
}
}
}
}
//Text scrolling switch
if (scrolling){
lcd.print(Scroll_LCD_Left(data));
}
}
I'm new in coding with arduino so i need some help here. This is an arduino program i made to display the currently playing song on a lcd display. Unfortunately, after transfering some ammount of data, the arduino freezes and it needs to be unplugged from the pc and plugged in again. After restarting it works fine. It seems like memory gets full or something, can somebody actually help me figure this out.